Function Call In C
“Calling a function in C to carry out a specific sequence of instructions is called the function call process.” It entails calling the function with arguments and having the function return a value. The function name and the parameter list are the two parts that make up a function call.
The function name is commonly written in brackets after the parameter list in a function call in C. For instance, the message "Hello world!" is printed to the console when the following code runs the printf() method with the two inputs "Hello world" plus the newline character:
printf("Hello_world!\n");
The initial portion of the function call, the function name, is used to specify which function needs to be executed. The method in this illustration is called printf().
The values supplied to the function are contained in the second component of the function call, the argument list. "Hello world" and "\n" are the two arguments provided to the printf() method in this example.
The parameters in the parameter list are examined and supplied to the function when the function is called. The function then uses the contents of the parameters to carry out its job before returning the result, which is often the outcome of the function's operations. The printf() method prints the string "Hello world!" to the console in the abovementioned example.
Various methods for passing parameters to a function
1. Call by value in C
In C, calling a function by value is one way to send parameters to it. The value of the actual argument is replicated in the formal argument in call-by-value. As a result, any modifications made to the formal parameter won't be reflected in the actual parameter.
For instance, the following code defines the function swap(), which takes two integer arguments and switches their values.
#include <stdio.h>
void swap_fun(int x,int y){
int temp=x;
x=y;
y=temp;
}
int main() {
int a = 6;
int b = 7;
swap_fun(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output:
![Function Call In C](https://static.tutorialandexample.com/c/function-call-in-c1.png)
As you can see, a and b's, values have not been switched. It is so that the swap_fun() function can only work with the copies of the values of a and b sent as formal parameters, a and b. A and b still have their original values.
Call-by-value benefits
The use of call by value in C has several benefits. Its ease of application is one benefit. Its efficiency, which copies the data of the actual parameter to the formal parameter, is another benefit. When the actual parameter is vast, this can be crucial.
Negative aspects of call by value
Additionally, there are significant drawbacks when calling a function by value in C. One drawback is that passing large or intricate data structures to functions might take time and effort. Another drawback is that changing the parameter's actual value from within the function may be challenging.
Use Cases for Call by Value
When the following criteria are accurate, calling by value is a wise move:
- The actual parameter is a straightforward data type, like an integer or float.
- The parameter itself is a simple one.
- The actual parameter's value does not need to be changed from inside the function.
When to Avoid Using "Call by Value"
When the following circumstances exist, calling by value is not a wise move:
- A large or complicated data structure serves as the parameter itself.
- The data of the actual argument must be changed from within the code.
2. Call by reference in C
In C, calling a function by reference is one way to send parameters to it. The address of the actual parameter is supplied to the function when calling by reference. As a result, any modifications to the formal variable will be displayed as the actual value.
For instance, the following code defines the function swap(), which takes two integer arguments and switches their values.
#include <stdio.h>
void swap_fun(int* x,int* y){
int temp=*x;
*x=*y;
*y=temp;
}
int main() {
int a = 6;
int b = 7;
swap_fun(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output:
![Function Call In C](https://static.tutorialandexample.com/c/function-call-in-c2.png)
a and b's values have been switched in the above code. Because the references of the variables a and b serve as the fundamental parameters for the swap_fun() function, this is the case. The swap_fun() function modifies the values of the parameters a and b when it alters their values.
Call by reference benefits
The use of call by reference in C has several benefits. One benefit is that you can change the parameter's actual value from inside the function. It can pass large or complicated data structures to a function, an additional benefit.
Issues with Call by Reference
Additionally, there are significant drawbacks to calling by reference in C. Its potential for being more challenging to comprehend than call by value is a drawback. Call-by-reference usage has the additional drawback of making debugging code more challenging.
Use Cases for Call by Reference
When the conditions below are satisfied, calling by reference is wise.
- The actual parameter's value must be changed inside the function.
- A vast or intricate data structure serves as the actual parameter.
How and When to Avoid Call by Reference
When the requirements below are actual, calling by reference could be better.
- The actual parameter's value does not need to be changed inside the function.
- The parameter itself is a straightforward data type.
What are return values?
A function's return value is the value it gives back when it has completed its operation. Any value, including an integer, double, float, or string, can be the return value of a function.
You utilize the return statement to have a function return a value. One expression is accepted as an argument by the return statement, and the function returns the outcome of that expression.
The function that follows, for instance, returns the result of equation 11 + 16:
#include <stdio.h>
int test(int x,int y){
int z=x+y;
}
int main() {
int a = 11;
int b = 16;
int sum=test(a,b);
printf("sum=%d",sum);
return 0;
}
Output:
![Function Call In C](https://static.tutorialandexample.com/c/function-call-in-c3.png)
Information can be passed back to the calling method via return values. For instance, a function that determines the area of a circle can give back that value. The calling function might then use this value to show the user the area.
Errors can also be indicated via return values. For instance, if a document cannot be opened, a function that examines the file might return a negative number. The calling function might then use this value to give the user an error message.
Return values are a practical C feature that you may utilize to make your code easier to comprehend and maintain.
The declaration of return values
The function prototype declares the return values. A function's name, the type of the function's return value, and the types of its parameters are all specified in the function prototype, which declares the function.
A function prototype has the following syntax:
return_type name_of_the_function(list_of_paremeters);
Where:
The function's return value type is indicated by return_type.
The function's name is name_of_the_function.
The function's parameters are listed in list_of_paremeters.
For example:
int multiply(int a, int b);
This prototype informs the compiler that the multiply() function accepts integer parameters and returns an integer value.
Any value, including an int, a float, or a string, maybe the return value. The function prototype must provide a type specification for the return value.
The function's return type must be void in cases where the function isn't returning a value.
An instance when the function does not return any value:
void greet(void);
The above functions take no parameters and return nothing.
How do return values function?
Return values function by controlling the calling function from the called function. The one employed to call the function is then given the outcome of the return statement.
A string, float, integer, or other type of value can all be returned with the return command. The type of the resultant value must match the type of the parameter used to invoke the function.
For instance, the following code calls the multiply function, and the function returns the result, which is stored in the result.
int res = multiply(a,b);
The function's value is undefined if the return statement is omitted. It implies the variable's value will be random when calling the function.
Return values are a useful C feature that you may utilize to make your code easier to comprehend and maintain. They can control execution flow, return data to the caller function, and highlight failures.
An illustration of a return statement in action:
#include <stdio.h>
int multiply(int x, int y){
int z = x*y;
return z;
}
int main() {
int a = 11;
int b = 16;
int result=multiply(a,b);
printf("product=%d",result);
return 0;
}
Output:
![Function Call In C](https://static.tutorialandexample.com/c/function-call-in-c4.png)