C Function Parameters
Functions become truly useful when they can work on different data each time they are called. Information goes into a function through parameters. A parameter is a variable listed in the function's parentheses; the actual value you supply in a call is called an argument.
#include <stdio.h>
void greet(const char *name, int age) { // name and age are parameters
printf("Hi %s, you are %d years old.\n", name, age);
}
int main(void) {
greet("Liam", 3); // "Liam" and 3 are arguments
greet("Jenny", 14);
greet("Anja", 30);
return 0;
}Hi Liam, you are 3 years old.
Hi Jenny, you are 14 years old.
Hi Anja, you are 30 years old.
Each parameter has its own type and name, separated by commas. Arguments are matched to parameters by position: the first argument goes into the first parameter, and so on. The number and types of arguments must fit the declaration.
Pass by value
C always passes arguments by value: the function receives a copy of each argument. Changing a parameter inside the function has no effect on the caller's variable:
#include <stdio.h>
void try_to_reset(int n) {
n = 0;
printf("inside: n = %d\n", n);
}
int main(void) {
int count = 25;
try_to_reset(count);
printf("outside: count = %d\n", count);
return 0;
}inside: n = 0
outside: count = 25
This makes functions safe — they cannot surprise you by changing your variables. When you do want a function to change something, pass a pointer to it.
Changing the caller's variables with pointers
By passing an address, the function gets a copy of the address, which still points to the original variable. This is also how a function can hand back more than one result:
#include <stdio.h>
void divide(int dividend, int divisor, int *quotient, int *remainder) {
*quotient = dividend / divisor;
*remainder = dividend % divisor;
}
void add_bonus(int *score, int bonus) {
*score += bonus;
}
int main(void) {
int q, r;
divide(47, 5, &q, &r);
printf("47 / 5 = %d remainder %d\n", q, r);
int score = 80;
add_bonus(&score, 15);
printf("score with bonus: %d\n", score);
return 0;
}47 / 5 = 9 remainder 2
score with bonus: 95
Array parameters
An array argument is passed as a pointer to its first element, so the function can read and modify the caller's array. It cannot find out the array length on its own, so the length is passed as an extra parameter:
#include <stdio.h>
double average(const int values[], int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += values[i];
}
return (double) sum / count;
}
void fill(int values[], int count, int start) {
for (int i = 0; i < count; i++) {
values[i] = start + i;
}
}
int main(void) {
int ages[] = {21, 34, 19, 42, 28};
printf("average age: %.1f\n", average(ages, 5));
int seq[6];
fill(seq, 6, 100);
for (int i = 0; i < 6; i++) printf("%d ", seq[i]);
printf("\n");
return 0;
}average age: 28.8
100 101 102 103 104 105
The const in average documents (and enforces) that the function will not modify the array.
Parameter forms at a glance
| Declaration | The function receives | Can it change the caller's data? |
|---|---|---|
void f(int x) |
a copy of the value | no |
void f(int *x) |
the address of a variable | yes, through *x |
void f(const int *x) |
the address, read-only | no |
void f(int arr[], int n) |
the address of the first element | yes, its elements |
void f(const char *s) |
the address of a string | no |
void f(void) |
nothing | — |
Returning values vs. output parameters
Prefer returning a value when a function produces a single result — int area(int w, int h) reads better than void area(int w, int h, int *out). Use pointer parameters when there are several results, or when the function also needs to report success or failure:
#include <stdio.h>
#include <stdbool.h>
bool safe_divide(double a, double b, double *result) {
if (b == 0) {
return false; // report failure, leave *result untouched
}
*result = a / b;
return true;
}
int main(void) {
double r;
if (safe_divide(10, 4, &r)) printf("10 / 4 = %.2f\n", r);
if (!safe_divide(1, 0, &r)) printf("cannot divide by zero\n");
return 0;
}10 / 4 = 2.50
cannot divide by zero
Tip: A function with many parameters (say, more than four or five) is hard to call correctly. Group related values into a
struct(see C Structures) and pass that instead.
Test yourself
In void show(int n) called as show(x), what happens to x if the function sets n = 99?
- x becomes 99
- x is unchanged
- x becomes 0
- Compile error
How should a function receive an int it needs to modify for the caller?
- int n
- const int n
- int *n
- int n[]
Why do functions that take an array usually also take a length parameter?
- For speed
- The function only receives a pointer and cannot determine the length itself
- C requires two parameters
- To make a copy of the array