C Function Parameters and Arguments
A function in C encapsulates a set of instructions that perform a specific task. Parameters and arguments allow you to pass data into these functions, making your code modular and reusable. In this tutorial, you’ll learn what parameters and arguments are and see several examples to understand how they work.
Understanding Parameters vs. Arguments
Parameters are variables listed in the function’s definition that act as placeholders for the values the function will receive. Arguments are the actual values you pass to the function when calling it.
Example 1: Simple Function with Parameters and Arguments
This example demonstrates a function that adds two numbers. The parameters int a and int b in the function definition serve as placeholders, and the arguments 10 and 20 are passed when the function is called.
main.c
#include <stdio.h>
// Function definition with parameters a and b
int add(int a, int b) {
return a + b;
}
int main() {
// Calling the function with arguments 10 and 20
int result = add(10, 20);
printf("Sum: %d\n", result);
return 0;
}
Explanation:
- The
addfunction is defined with two parameters,int aandint b, which will hold the values passed to the function. - When the function is called in
main()with the arguments10and20, these values replace the parametersaandbrespectively. - The function returns the sum of the two numbers, which is stored in the variable
result. printf()is used to display the result.
Output:
Sum: 30
Example 2: Demonstrating Pass by Value
This example illustrates that in C, function parameters are passed by value. Changes made to the parameter inside the function do not affect the original argument.
main.c
#include <stdio.h>
// Function that attempts to double the value
void doubleValue(int num) {
num = num * 2;
printf("Inside function: %d\n", num);
}
int main() {
int value = 15;
printf("Before function call: %d\n", value);
doubleValue(value);
printf("After function call: %d\n", value);
return 0;
}
Explanation:
- The
doubleValuefunction takes one parameterint numand attempts to double its value. - Inside
doubleValue, the parameternumis modified, but this change is local to the function. - In
main(), the original variablevalueremains unchanged after the function call, demonstrating that C uses pass by value.
Output:
Before function call: 15
Inside function: 30
After function call: 15
Example 3: Function with Multiple Parameters
This example shows a function that takes multiple parameters of different types to display a formatted message.
main.c
#include <stdio.h>
// Function that prints a formatted greeting message
void greet(const char name[], int age) {
printf("Hello %s, you are %d years old.\n", name, age);
}
int main() {
// Calling the greet function with a string and an integer argument
greet("Arjun", 28);
return 0;
}
Explanation:
- The
greetfunction is defined with two parameters:const char name[](a string) andint age(an integer). - The
constkeyword ensures that the stringnameis not modified within the function. - In
main(), the function is called with the arguments"Arjun"and28, which are passed to the parametersnameandagerespectively. - The
printf()function insidegreetdisplays the formatted greeting message.
Output:
Hello Arjun, you are 28 years old.
Conclusion
In this tutorial, we explored the concepts of function parameters and arguments in C. We learned that:
- Parameters are variables defined in a function’s signature that receive values.
- Arguments are the actual values passed to the function when it is called.
- Changes to parameters inside a function do not affect the original arguments (pass by value).
- You can define functions with multiple parameters to handle various data types and tasks.
