C Function Parameters
Function parameters are named variables listed inside a function’s parentheses. They let a function receive values from the code that calls it.
The values passed into a function call are called arguments. Parameters make one function useful with many different inputs.
Parameters And Arguments
A parameter is written in the function definition with a type and a name. An argument is the actual value supplied when the function is called.
#include <stdio.h>
void greetUser(char name[])
{
printf("Hello, %s!\n", name);
}
int main(void)
{
greetUser("Maya");
greetUser("Leo");
return 0;
}
Output:
Hello, Maya!
Hello, Leo!
In void greetUser(char name[]), name is the parameter. In greetUser("Maya"), "Maya" is the argument. Each call gives the function a different value for name.
Multiple Parameters
A function can have more than one parameter. Separate parameters with commas, and include the type for each one.
#include <stdio.h>
int addNumbers(int a, int b)
{
return a + b;
}
int main(void)
{
int first = addNumbers(4, 6);
int second = addNumbers(10, 25);
printf("First sum: %d\n", first);
printf("Second sum: %d\n", second);
return 0;
}
Output:
First sum: 10
Second sum: 35
The arguments are matched to parameters by position. In addNumbers(4, 6), 4 is copied into a, and 6 is copied into b.
Order Matters
Because arguments are matched by position, changing their order can change the result. This is especially important when the parameters have different meanings.
#include <stdio.h>
void showScore(char player[], int score)
{
printf("%s scored %d points.\n", player, score);
}
int main(void)
{
showScore("Ari", 18);
showScore("Nia", 24);
return 0;
}
Output:
Ari scored 18 points.
Nia scored 24 points.
The function expects a string first and an integer second. The call must follow that same order: showScore("Ari", 18);.
Using Prototypes With Parameters
If a function is defined after main, write a function declaration, or prototype, before main. The prototype must include the return type and parameter types.
#include <stdio.h>
int multiply(int left, int right);
int main(void)
{
printf("Product: %d\n", multiply(7, 3));
return 0;
}
int multiply(int left, int right)
{
return left * right;
}
Output:
Product: 21
The prototype int multiply(int left, int right); tells the compiler that multiply takes two int parameters and returns an int. In prototypes, parameter names are optional, but including them can make the code easier to read.
Parameters Are Local Copies
For ordinary values such as int, C passes a copy of the argument into the function. Changing the parameter inside the function does not change the original variable in main.
#include <stdio.h>
void addFive(int number)
{
number = number + 5;
printf("Inside function: %d\n", number);
}
int main(void)
{
int value = 10;
addFive(value);
printf("In main: %d\n", value);
return 0;
}
Output:
Inside function: 15
In main: 10
The parameter number receives a copy of value. The function can change its own copy, but value in main stays the same.
Quick Rules
- Write each parameter as
type name, such asint count. - Separate multiple parameters with commas.
- Pass one argument for each parameter when calling the function.
- Keep the argument order the same as the parameter order.
- Use
voidin the parentheses when a function takes no parameters.
The key idea is that parameters let functions work with values supplied by the caller. Next, you will build on this by learning more about returning values from functions.
