C Function Parameters: Passing Information to Functions
Learn how to pass information to functions in C using parameters. These parameters function as variables within the function and are defined inside the parentheses following the function name. Discover how to specify multiple parameters by separating them with commas, allowing for flexible and efficient function design in your C programming projects.
C Function Parameters
Parameters and Arguments
In C, you can pass information to functions through parameters. These parameters act like variables inside the function. Parameters are specified inside the parentheses after the function name, and you can add multiple parameters by separating them with commas.
Syntax
Syntax
returnType functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
Example: Passing Parameters
In the example below, the function accepts a string of characters as a parameter. When the function is called, we pass along a name, which the function uses to print "Hello" along with each person's name:
Syntax
void myFunction(char name[]) {
printf("Hello %s\n", name);
}
int main() {
myFunction("Alice");
myFunction("Bob");
myFunction("Claire");
return 0;
}
Output
Hello Alice
Hello Bob
Hello Claire
Multiple Parameters
Inside the function, you can include multiple parameters. Here’s an example:
Syntax
void myFunction(char name[], int age) {
printf("Hello %s. You are %d years old.\n", name, age);
}
int main() {
myFunction("Alice", 25);
myFunction("Bob", 30);
myFunction("Claire", 20);
return 0;
}
Output
Hello Alice. You are 25 years old.
Hello Bob. You are 30 years old.
Hello Claire. You are 20 years old.
Passing Arrays as Parameters
You can also pass arrays to a function. The function will loop through the array elements:
Syntax
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
printf("%d\n", myNumbers[i]);
}
}
int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
Output
10
20
30
40
50
Return Values in C Functions
In C, you can also return values from a function. For this, use a data type (like int
or float
) instead of void
. The function returns a value using the return
keyword:
Syntax
int myFunction(int x) {
return 5 + x;
}
int main() {
printf("Result is: %d", myFunction(7));
return 0;
}
Output
Result is: 12
Store Return Values in Variables
You can store the result of a function in a variable:
Syntax
int myFunction(int x, int y) {
return x + y;
}
int main() {
int result = myFunction(6, 4);
printf("Result is = %d", result);
return 0;
}
Output
Result is = 10
Practical Example: Convert Fahrenheit to Celsius
Here is a practical example that uses a function to convert a temperature from Fahrenheit to Celsius:
Syntax
float toCelsius(float fahrenheit) {
return (5.0 / 9.0) * (fahrenheit - 32.0);
}
int main() {
float f_value = 104.2;
float result = toCelsius(f_value);
printf("Fahrenheit: %.2f\n", f_value);
printf("Convert Fahrenheit to Celsius: %.2f\n", result);
return 0;
}
Output
Fahrenheit: 104.20
Convert Fahrenheit to Celsius: 40.11