Passing arguments to functions
By passing a parameter argument list to a function in C programming, you can provide input data values to the function so that it can perform its user-defined task based on the argument parameter list data values. The arguments or parameter list given to a function in C language can be of many types. Common argument lists include integer, floating-point number, character, double, array or pointer, etc.
So, let’s see how to pass arguments to a function in C language.
Types of function parameters in C language.
Passing by value.
Any C program by default passes the function argument parameter by value. This means that the function gets a copy of the argument’s value, and any modifications to the existing parameter list inside the program at the time of calling the function do not affect the default function parameter argument.
Example of pass by value.
/// function prototype declaration
void modifyValue(int p);
// Function definition declare
void corectedValue(int p) {
p = p + 1;
printf(“\n see the corected value with function – %d”, p);
}
int main() {
int q = 2;
corectedValue(q); // Passing ‘q’ by corectedvalue function
printf(“see the Original value with function call – %d”, q);
return 0;
}
In this call by value function example, p is passed by value to corectedValue(). Changes made to p inside the function do not affect the arguments in main().
Passing by Reference (Using Pointers) in C.
To modify the original variable inside the declared function, you can pass the function variable argument value by function argument parameter list memory address reference using pointers operator with the function. This allows the function to directly access the memory address location of the original variable and modify its value.
Call by reference example.
// Function prototype declaration
void corectedValueByReference(int *p);
// Function definition declaration
void corectedValueByReference(int *p) {
*p = *p + 1;
printf(“\n see the Modified value with function – %d”, *p);
}
int main() {
int q = 2;
corectedValueByReference(&q); // Passing ‘q’ by reference declaration
printf(“\n see the Original value after function call – %d”, q);
return 0;
}
In this call by reference function example, the function argument parameter list is passed by reference using the pointer operator to pass the q argument to corectedValueByReference(). Changes made to the *p argument inside the function directly modify the number in main() .
Passing arrays to C functions.
In C programs array data types are usually passed to functions with pointer operators, even though array notation may be used in function argument parameter variable list declarations. This is because the name of the array data type itself represents a pointer to the first element of the array.
Passing array function example.
// Function prototype declaration
void Arrayelement(int arr[], int value);
// Function definition declaration
void Arrayelement(int arr[], int value) {
for (int p = 0; p < value; p++) {
printf(“%d “, arr[p]);
}
printf(“\n”);
}
int main() {
int integer[] = {5, 4, 3, 2, 1};
int value = sizeof(integer) / use to check sizeof operator to array(integer[0]);
Arrayelement(integer, value); // Passing ‘integer’ array to Arrayelement function
return 0;
}
In this example, an integer array is passed to the Arrayelement() function along with the size of the array as a pointer to its first array element.
Passing Strings to Functions in C.
In C programs, strings data type is usually processed as character array data type format. Any character can be passed to a string function as arrays or as pointers to characters in the argument parameter list.
Example of string processing function in C.
// Function prototype declaration
void displayString(char str1[]);
// Function definition declaration
void displayString(char str1[]) {
printf(“\n lets String passed to function – %s”, str1);
}
int main() {
char text[] = “Welcome to c”;
displayString(text); // Passing the ‘text’ string to the displayString function declaration
return 0;
}
In this example, the text string is passed to the displayString() function as an array of characters.