In C programming, function arguments (also known as parameters) are values passed to a function when it is called. These arguments provide the necessary information that the function needs to perform its task.
Types of Function Arguments
1. Pass by value: A copy of the actual argument is passed to the function. Changes made to the parameter inside the function do not affect the original argument.
2. Pass by reference (using pointers): The address of the actual argument is passed to the function, allowing it to modify the value of the argument in the calling function.
1. Pass by Value in C
Pass by value is a method of passing arguments to a function in C where a copy of the actual argument’s value is passed to the function. As a result, changes made to the parameter inside the function do not affect the original value of the argument in the calling function. The function operates on the copy, and any modifications to the parameter are local to the function.
#include <stdio.h>
void numberOfSquare(int num) {
num = num * num; // Modify the local copy of 'num'
printf("Value inside function: %d\n", num);
}
int main() {
int number = 5;
// Function call with pass by value (passing the actual argument)
numberOfSquare(number);
// 'number' in main remains unchanged
printf("Value in main after function call: %d\n", number);
return 0;
}
Output:
Value in main after function call: 5
Explanation:
- The function numberOfSquare is defined to take an integer num as its argument.
- In the main function, the variable number is initialized to 5.
- The function numberOfSquare is called with number as the argument. Since C passes arguments by value, the value 5 is passed to the function as a copy, not the actual variable.
- Inside the function, the copy of num is modified (it is square of number), but this change does not affect the original number in the main function.
- After the function call, number in main still holds its original value (5).
2. Pass by Reference in C
Pass by reference in C is a technique where instead of passing a copy of the argument (as in pass by value), the memory address (reference) of the argument is passed to the function. This allows the function to directly modify the value of the argument in the calling function.
Example: Pass by reference using pointers
#include <stdio.h>
void numberOfSquare(int *num) {
*num = *num * *num;
printf("Value inside function: %d\n", *num);
}
int main() {
int number = 5;
// Function call with pass by reference (passing the address of number)
numberOfSquare(&number);
// 'number' in main is changed
printf("Value in main after function call: %d\n", number);
return 0;
}
Output:
Value in main after function call: 25
Explanation:
- The function numberOfSquare takes an integer pointer (int *num) as its argument.
- Inside the main function, the variable number is initialized to 5.
- The function numberOfSquare is called with the address of the variable number (using the & operator). This is equivalent to passing the variable by reference.
- Inside the function, the pointer num points to the memory location of number. Using the dereference operator (*), we modify the value of number directly.
- After the function call, the value of number in main is updated because the function worked directly with the memory address of number.
C Function Arguments – Questions and Answers
Q 1: What are function arguments in C?
Ans: Values passed to a function when it is called.
Q 2: What is the difference between parameters and arguments?
Ans: Parameters are variables in the function definition; arguments are values passed during the call.
Q 3: How many arguments can a function take?
Ans: Any number, depending on the function definition.
Q 4: What is call by value in C?
Ans: A method where a copy of the variable is passed to the function.
Q 5: How does C support call by reference?
Ans: By passing the address of variables using pointers.
C Function Arguments – Objective Questions (MCQs)
Q1. Which of the following is true about function arguments in C?
Q2. How many types of function arguments are there in C?
Q3. What is passed in “pass by value”?
Q4. Which of the following is a valid function call?
int add(int x, int y);
Q5. What happens if the number of arguments in the function call does not match the definition?