Passing in values to functions

This is a discussion on Passing in values to functions within the C Programming forums, part of the General Programming Boards category; I remember that there was a way to pass a value into a function sometimes, but not always. If you ...

Passing in values to functions

I remember that there was a way to pass a value into a function sometimes, but not always. If you didn't pass the value into the function, then either that variable doesn't exist for the lifetime of the function or the variable is automatically set to a predefined value.

For example, I have a function prototype

Code:

printCommand(Line *head, int lineNumber)

And sometimes I want to call this function without the int being called. Also, is it possible to instead send a string to the function as well?

The C language does not support sending/returning arrays by value, but you may pass them in by reference - i.e. A pointer to the first char in the array

And sometimes I want to call this function without the int being called

If you are referring to overloading the function, the answer is no. What you can do is use a pointer to pass the int into the function and when you want to not use the int pass a NULL into the function -> Your function could test for this and behave differently if the int was a NULL pointer

If you are referring to overloading the function, the answer is no. What you can do is use a pointer to pass the int into the function and when you want to not use the int pass a NULL into the function -> Your function could test for this and behave differently if the int was a NULL pointer

Oh I see. Is that how some common library functions such as printf and scanf work?

They use a "variable argument list" using the macros defined in stdarg.h. That might be what you want, but you'd have to explain exactly what you're trying to accomplish to get the best advice.

Well, each node in my linked list is a line of text, and I'm required to - depending on user input - print out either a specific line number (hence the int lineNumber) or also if they input '+' or '-' to print out the next/previous line respectively.
I've settled with changing the function prototype to

Code:

printCommand(Line *head, int lineNumber)

because I think it'll keep things more clear in the end if I just converted the strings to numbers if they are numbers, because I'm going to also be taking in a lot more different types of character input down the track.

Well, each node in my linked list is a line of text, and I'm required to - depending on user input - print out either a specific line number (hence the int lineNumber) or also if they input '+' or '-' to print out the next/previous line respectively.