How to Send Values to Functions in C Programming

By Dan Gookin

In C programming, the key way to make a function funct is to give it something to chew on — some data. The process is referred to as passing an argument to a function, where the term argument is used in C programming to refer to an option or a value. It comes from the mathematical term for variables in a function, so no bickering is anticipated.

Arguments are specified in the function’s parentheses. An example is the puts() function, which accepts text as an argument, as in

puts("You probably shouldn’t have chosen that option.");

The fgets() function swallows three arguments at once:

fgets(buffer,27,stdio);

Arguments can be variables or immediate values, and multiple arguments are separated by commas. The number and type of values that a function requires must be specified when the function is written and for its prototype as well. Passing a Value to a Function illustrates an example.

PASSING A VALUE TO A FUNCTION

#include <stdio.h>
void graph(int count);
int main()
{
 int value;
 value = 2;
 while(value<=64)
 {
 graph(value);
 printf("Value is %dn",value);
 value = value * 2;
 }
 return(0);
}
void graph(int count)
{
 int x;
 for(x=0;x<count;x=x+1)
 putchar('*');
 putchar('n');
}

When a function consumes an argument, you must clearly tell the compiler what type of argument is required. In Passing a Value to a Function, both the prototype at Line 3 and the graph() function’s definition at Line 20 state that the argument must be an int. The variable count is used as the int argument, which then serves as the variable’s name inside the function.

The graph() function is called in Line 13, in the midst of the while loop. It’s called using the value variable. That’s okay; the variable you pass to a function doesn’t have to match the variable name used inside the function. Only the variable type must match, and both count and value are int types.

The graph() function, from Line 20 through Line 27, displays a row of asterisks. The length of the row (in characters) is determined by the value sent to the function.

Exercise 1: Fire up a new project using the source code from Passing a Value to a Function. Save the project as ex1006. Build it. Can you guess what the output might look like before running it?

Functions don’t necessarily need to consume variables. The graph() function from Passing a Value to a Function can gobble any int value, including an immediate value or a constant.

Exercise 2: Edit the source code from Exercise 1, changing Line 13 so that the graph() function is passed a constant value of 64. Build and run.

It’s possible to pass a string to a function. A string is really an array, and it requires special C language magic to pass the array to a function.

C offers no limit on how many arguments a function can handle. As long as you properly declare the arguments as specific types and separate them all with commas, you can stack ’em up like commuters on a morning train, similar to this prototype:

void railway(int engine, int boxcar, int caboose);

In the preceding line, the railway() function is prototyped. It requires three int arguments: engine, boxcar, and caboose. The function must be passed three arguments, as shown in the prototype.

Exercise: 3: Modify the source code from Passing a Value to a Function so that the graph() function accepts two arguments; the second is the character to display.