What are Function Arguments in C?
In C, functions are blocks of code designed to perform specific tasks. To make functions more flexible and reusable, you can pass data to them using arguments. These arguments are specified within the parentheses of a function call and correspond to parameters defined in the function’s declaration.​
There are two types of function arguments in C:
- Actual Parameters – These are the values or variables passed to the function when it is called.
- Formal Parameters – These are the variables defined in the function declaration, which receive the values of the actual parameters.
Example:
#include <stdio.h> void greet(char name[]) { printf("Hello, %s!\n", name); } int main() { greet("Sanfoundry"); return 0; }
Output:
Hello, Sanfoundry!
This program defines a function greet that takes a string and prints a hello message. In main, it calls greet with the word “Sanfoundry”. The output is: Hello, Sanfoundry!. It shows how to pass and print strings in C.
Types of Function Arguments
1. No Arguments
Functions can be defined without parameters and called without arguments.​
Example:
#include <stdio.h> // Function to greet the user void greetUser() { printf("Welcome to C Bootcamp!\n"); } int main() { greetUser(); // Calling the function return 0; }
2. Fixed Number of Arguments
Functions can have a fixed number of parameters, and you must provide corresponding arguments when calling them.​
Example:
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int sum = add(5, 3); printf("Sum: %d\n", sum); return 0; }
3. Variable Number of Arguments (Variadic Functions)
C allows functions to accept a variable number of arguments using the stdarg.h library. This is useful for functions like printf.​
Example:
#include <stdio.h> #include <stdarg.h> double average(int count, ...) { va_list args; double sum = 0; va_start(args, count); for (int i = 0; i < count; i++) { sum += va_arg(args, int); } va_end(args); return sum / count; } int main() { printf("Average: %.2f\n", average(3, 10, 20, 30)); return 0; }
This C program shows how to use a function with a variable number of arguments. The average function takes a count and then reads that many integers using va_list. It adds them up and returns the average. In main, the function is called with three numbers: 10, 20, and 30. The program prints the result as the average of those numbers, which is 20.00.
Passing Arguments: By Value vs. By Reference
Passing by Value
In C, arguments are passed by value by default. This means the function receives a copy of the argument, and changes made inside the function do not affect the original variable.​
Example:
#include <stdio.h> void byValue(int a) { a = a + 5; // Changes will not affect the original variable printf("Inside byValue function: a = %d\n", a); } int main() { int num = 10; byValue(num); printf("In main function: num = %d\n", num); // num will still be 10 return 0; }
Output:
Inside byValue function: a = 15 In main function: num = 10
This program shows how passing by value works in C. The byValue function takes a copy of the variable num. Any changes made to a inside the function do not affect the original num. So, num stays 10 in the main function, even after calling byValue.
Passing by Reference
To allow a function to modify the original variable, you can pass a pointer to the variable, effectively passing by reference.​
Example:
#include <stdio.h> #include <stdio.h> void increment(int *num) { (*num)++; } int main() { int a = 5; increment(&a); printf("a: %d\n", a); return 0; }
Output:
a: 6
This program shows how to use a pointer to modify a variable inside a function. The increment function takes a pointer and increases the value it points to by 1. In main, it passes the address of a to the function. After the call, a becomes 6.
Benefits of Using Function Arguments
- Reusability: You can write a function once and use it many times with different data. Just pass new values as arguments to get different results. This saves time and reduces code repetition.
- Modularity: Arguments help break your program into smaller parts. Each function does one job and gets the data it needs through its arguments.
- Flexibility: Functions become more flexible when they use arguments. You can use the same function in many situations just by changing the input values.
- Better Data Flow: Arguments let you pass data directly into a function. This keeps your code clear and avoids using too many global variables.
- Memory Efficiency: Passing only the needed data helps save memory. It avoids making extra copies, which is useful in systems with limited resources.
- Scalability: Using arguments makes your code easier to grow. You can add new features without changing much of the existing code.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check C Books
- Watch Advanced C Programming Videos
- Check Computer Science Books