C Function Parameters
In this page:
What are Parameters?
Parameters are the variables listed in a function's definition that receive external data when the function is called, letting the same function body operate on different values each time it runs.
Example: What are Parameters?
#include <stdio.h>
void greet(char name) {
printf("Hello, %c", name);
}
int main() {
greet('J');
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Single Parameter
A function with a single parameter -- like void greet(char name) -- requires the caller to pass exactly one argument whose type matches that parameter's declared type, or the compiler will flag a mismatch.
Example: Single Parameter
#include <stdio.h>
void greet(char name) {
printf("Hello, %c", name);
}
int main() {
greet('A');
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
Separating several parameters with commas, like int add(int a, int b), lets a function accept multiple pieces of input at once, all available inside the function body under their own local names.
Example: Multiple Parameters
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
printf("%d", add(3, 4));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameter Types
C parameters can be declared as any valid data type -- int, float, double, char, and beyond -- and the arguments you supply on a call must line up with those types and their order, since C matches arguments positionally, not by name.
Example: Parameter Types
#include <stdio.h>
void showInfo(int age, float height, char grade) {
printf("%d %.1f %c", age, height, grade);
}
int main() {
showInfo(20, 5.9f, 'A');
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Actual vs. Formal Arguments
The parameters listed in a function's own definition are called formal arguments, while the actual values or variables you pass in when calling it are called actual arguments -- the distinction matters when discussing how data flows into a function.
Example: Actual vs. Formal Arguments
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int x = 3, y = 4;
printf("%d", add(x, y));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: