Functions get data through parameters. You pass in values (called arguments), and the function works with its own copies. Let's break down how this works.
Parameters vs Arguments
The parameter is the variable in the function definition. The argument is the actual value you pass when calling the function. Two sides of the same handshake.
#include
void printInfo(char name[], int age) {
printf("%s is %d years old\n", name, age);
}
int main() {
printInfo("Alice", 25);
printInfo("Bob", 30);
return 0;
}
Try it Yourself →
Pass by Value
C uses pass by value. The function gets a copy of the argument, not the original. Changing the parameter inside the function won't affect the original variable.
#include
void changeValue(int x) {
x = 100;
printf("Inside function: %d\n", x);
}
int main() {
int num = 5;
changeValue(num);
printf("Outside function: %d\n", num);
return 0;
}
Try it Yourself →
Multiple Parameters
You can pass as many parameters as you need. Separate them with commas in both the definition and the call.
#include
double average(double a, double b, double c) {
return (a + b + c) / 3.0;
}
int main() {
double avg = average(10.5, 20.0, 15.5);
printf("Average: %.2f\n", avg);
return 0;
}
Try it Yourself →