C Function Parameters
In this page:
return_type function_name(type param1, type param2) {
// body
}
function_name(argument1, argument2);
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 standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
// Print formatted text to the screen
printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
greet('J');
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
// Print formatted text to the screen
printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
greet('A');
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(3, 4));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
void showInfo(int age, float height, char grade) {
// Print formatted text to the screen
printf("%d %.1f %c", age, height, grade);
}
// Program execution starts in main()
int main() {
showInfo(20, 5.9f, 'A');
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
int x = 3, y = 4;
// Print formatted text to the screen
printf("%d", add(x, y));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Passing arguments in the wrong order, which compiles but gives wrong results when the types match.
- Expecting a function to change the caller's variable when it received a copy.
- Passing an array and using
sizeofon the parameter, which gives the size of a pointer.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: