Hello, Function!

A basic C language process is passing a value to a function. If you’re an old hand, you might say, “Yeah, well, so?” For a beginning programmer, however, functions and their arguments can be a confusing and intimidating ordeal.

A function in C can:

  • Accept a value
  • Return a value
  • A combination of both or neither

A function that neither accepts nor returns a value is a void function. I call it a function that doesn’t funct. That label doesn’t imply that the function is unnecessary or that your programming skills are weak when you create a void function. In fact, it doesn’t mean anything. I frequently use void functions to store repetitious code in my programs. Here’s an example of such a function:

void pany(void)
{
    printf("Press Enter to continue:");
    getchar();
}

The pany() function displays a prompt and waits for the Enter key to be pressed. It requires no arguments and returns no values, which is why the word void is used in its declaration.

Values passed to a function are referred to as arguments. Functions can accept any quantity of arguments, from zero (void) to whatever is practical. The quantity depends on what the function does. All arguments must be declared as a specific variable type. The arguments are also given names for reference within the function. For example:

void guess(int tries)

The guess() function has a single argument, an int value. The variable tries is used within the function to represent the int value passed to the function. For example, elsewhere in the code you may see:

guess(3);

The value 3 is “passed” to the guess() function. Then, within that function, the value is referenced by variable tries.

What can be confusing, especially for a beginner, is that the function could also be called by using a variable. For example:

guess(attempt);

Above, the int variable attempt is used to call the guess() function. The value of attempt is passed to the function — the value only, not the variable name. Inside the function, the value is referenced by using the tries variable.

Remember from my C programming books, variables are local to their functions. Unless attempt is a global variable, you can’t use that variable within another function.

A function in the C language returns only one value. That value is specified as the function’s variable type. For example:

int whatever(void);

Above, the whatever() function returns an int value. The function has no arguments.

char middle(char *text);

Above, the middle() function returns a char value. The function accepts a single string as an argument, a char pointer.

Functions in C cannot return more than a single value. When you absolutely need such a function (and I can’t think of any examples off the top of my head) you must use global variables.

What about functions that return a structure variable? When that happens, the structure must be defined as a global variable, otherwise it can’t be used outside of the function. Beyond that, functions in the C language return only a single value, or no value at all.

Leave a Reply