C Function Parameters and Arguments
Updated 7 min read Aimerscrew
A function that does exactly the same thing every time is useful, but limited. C function parameters let you hand a function some input, so one function can handle many situations. In this lesson, you'll learn how to give a function parameters, how arguments fill them, why C passes copies of your values, and how const protects a parameter from accidental changes.
🌍 Real-life example
When you order a pizza, the kitchen follows the same recipe every time, but you fill in the details: "a 12-inch pizza with 2 toppings". The order form has blank fields for size and toppings (those are the parameters), and what you write in them, 12 and 2, are the arguments.
What are function parameters in C?
A parameter is a variable declared inside the parentheses of a function definition. It acts as a placeholder for a value the caller will supply. Each parameter has a type and a name, just like any variable.
#include <stdio.h>
void order_pizza(int inches, int toppings) {
printf("Pizza: %d-inch with %d topping(s)\n", inches, toppings);
}
int main(void) {
order_pizza(12, 2);
order_pizza(16, 4);
order_pizza(10, 0);
return 0;
}
Pizza: 12-inch with 2 topping(s)
Pizza: 16-inch with 4 topping(s)
Pizza: 10-inch with 0 topping(s)
One function, three different pizzas. Inside the body, inches and toppings are ordinary variables that already hold the values from the call.
Parameters vs arguments: what's the difference?
People often mix up these two words. The difference is simply where they appear:
| Parameter | Argument | |
|---|---|---|
| Where | In the function definition | In the function call |
| What it is | A variable that receives a value | The actual value sent in |
| Example | int inches in void order_pizza(int inches, ...) | 12 in order_pizza(12, 2) |
| Also called | Formal parameter | Actual parameter |
A simple way to remember: parameters are the placeholders, arguments are the actual values.
Functions with multiple parameters
Separate parameters with commas. Each one needs its own type, even if the types are the same. Arguments are matched to parameters by position: the first argument goes to the first parameter, the second to the second, and so on.
#include <stdio.h>
void print_line(int length, char symbol) {
for (int i = 0; i < length; i++) {
printf("%c", symbol);
}
printf("\n");
}
int main(void) {
print_line(10, '-');
print_line(5, '*');
print_line(3, '#');
return 0;
}
----------
*****
###
⚠️ Be careful
You can't share a type between parameters the way you can with variables. void print_line(int length, symbol) is wrong; write the type before every parameter: (int length, char symbol).
Arguments can be variables and expressions
An argument doesn't have to be a literal number. Any expression of the right type works: a variable, a calculation, even the result of another function. C works out the value first, then passes it in.
#include <stdio.h>
void print_seats(int row, int seat) {
printf("Your seat: row %d, seat %d\n", row, seat);
}
int main(void) {
int booked_row = 14;
int first_seat = 3;
print_seats(booked_row, first_seat);
print_seats(booked_row, first_seat + 1);
return 0;
}
Your seat: row 14, seat 3
Your seat: row 14, seat 4
The names of the arguments (booked_row) don't need to match the names of the parameters (row). Only the position matters.
What does pass by value mean in C?
When you call a function, C copies each argument's value into the matching parameter. The function then works on its own copy. This is called pass by value, and it has an important result: changing a parameter inside a function does not change the caller's variable.
#include <stdio.h>
void add_bonus(int score) {
score = score + 50;
printf("Inside add_bonus: score = %d\n", score);
}
int main(void) {
int score = 100;
add_bonus(score);
printf("Back in main: score = %d\n", score);
return 0;
}
Inside add_bonus: score = 150
Back in main: score = 100
Even though both variables are called score, they are two different boxes in memory. add_bonus changed its own copy to 150; the original in main stayed 100.
📝 Note
So how does a function change a caller's variable? It gets the variable's address instead of its value. That technique uses pointers, which you'll learn in pass by reference in C. For now, the usual way to get a result out of a function is to return a value.
What is a const parameter?
Sometimes a function should only read a parameter, never change it. Adding const before the type makes the parameter read-only inside the function. If you try to assign to it, the compiler stops you.
#include <stdio.h>
void print_price(const int cents) {
printf("$%d.%02d\n", cents / 100, cents % 100);
}
int main(void) {
print_price(1999);
print_price(705);
return 0;
}
$19.99
$7.05
The price is stored in cents to avoid decimal rounding. cents / 100 gives the dollars (integer division), cents % 100 gives the remaining cents, and %02d pads them to two digits, so 5 prints as 05.
If you add a line like cents = 0; inside print_price, GCC refuses to compile and reports an error about the assignment of read-only parameter 'cents'. That's exactly what you want: a mistake caught before the program runs.
💡 Tip
Because of pass by value, const on a simple int or double parameter protects only the function's own copy. It's a promise to yourself, not to the caller. const becomes much more important later with pointers and arrays, where it protects the caller's data.
Real-world example: a thermostat schedule
Parameters can have different types. This smart thermostat function takes an hour (an int) and a target temperature (a double):
#include <stdio.h>
void set_temperature(int hour, double celsius) {
if (celsius < 10.0 || celsius > 30.0) {
printf("%02d:00 %.1f C is out of range, ignored\n", hour, celsius);
} else {
printf("%02d:00 heat to %.1f C\n", hour, celsius);
}
}
int main(void) {
set_temperature(7, 21.5);
set_temperature(9, 18.0);
set_temperature(18, 35.0);
set_temperature(23, 16.5);
return 0;
}
07:00 heat to 21.5 C
09:00 heat to 18.0 C
18:00 35.0 C is out of range, ignored
23:00 heat to 16.5 C
The function checks its input before acting on it. Validating parameters like this is a good habit, because the caller might pass anything.
Common mistakes with function parameters
1. Passing the wrong number of arguments
order_pizza(12); /* the function expects two arguments */
GCC stops with an error saying there are too few arguments to function 'order_pizza' (or too many, if you pass extra ones). Count the parameters and pass exactly that many.
2. Swapping the order of arguments
order_pizza(2, 12); /* compiles, but orders a 2-inch pizza with 12 toppings! */
Both parameters are int, so the compiler can't spot the mix-up. Arguments are matched by position, not by name. Always check the order in the definition.
3. Expecting the function to change your variable
As the add_bonus example showed, assigning to a parameter only changes the copy. If you need a new value back, return it (next lesson).
4. Writing empty parentheses for "no parameters"
In C (before the C23 standard), void greet() does not mean "no parameters"; it means "parameters not specified", so the compiler won't complain if someone calls greet(5). Write void greet(void) to say clearly that the function takes nothing.
Practice exercise
🏋️ Try it yourself
Write a function print_rectangle that takes a width, a height and a char to draw with, and prints a filled rectangle using nested loops. Call it with (6, 2, '#') and then (3, 3, 'o'), with an empty line between the two shapes.
Show solution
#include <stdio.h>
void print_rectangle(int width, int height, char symbol) {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
printf("%c", symbol);
}
printf("\n");
}
}
int main(void) {
print_rectangle(6, 2, '#');
printf("\n");
print_rectangle(3, 3, 'o');
return 0;
}
######
######
ooo
ooo
ooo
What's next?
Your functions can now take input. Next, learn how a function hands a result back to its caller with C function return values.
Key takeaways
- Parameters are the variables in a function definition; arguments are the values passed in a call.
- Each parameter needs its own type, and parameters are separated by commas.
- Arguments are matched to parameters by position, not by name.
- C passes arguments by value: the function gets a copy, so changing a parameter never changes the caller's variable.
- A const parameter is read-only inside the function; assigning to it is a compile error.
- Calling with too few or too many arguments is an error; use (void) for functions that take no parameters.
Frequently asked questions
What is the difference between a parameter and an argument in C?
A parameter is the variable declared in the function definition, such as int size. An argument is the actual value given in the call, such as 12 in order(12). Arguments fill parameters by position.
Does C pass arguments by value or by reference?
C always passes arguments by value: each parameter receives a copy. To let a function change a caller's variable, you pass the variable's address using a pointer, which simulates pass by reference.
Why does changing a parameter not change my variable in C?
Because the parameter is a separate copy of your value. The function modifies its copy, and the copy disappears when the function returns, leaving your original variable unchanged.
What does const mean on a function parameter?
It makes the parameter read-only inside the function. Any attempt to assign to it causes a compile error, which helps prevent accidental changes.
How many parameters can a C function have?
The C standard requires compilers to support at least 127 parameters, but in practice most functions use a few. If you need many, grouping them in a struct is usually cleaner.