Passing arguments to a function in “C”
In this section, i am going to write how to pass arguments to the function in “C” programming.
There are mainly two ways to pass the arguments from the calling function to called function. They are
- Pass by Value
- Pass by Reference
In the first case the value is passed directly to the called function, and the address is passed to the function in the second case.
Pass by Value:
Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.
Program to find the addition of Two numbers by passing value:
Output:
Enter the value for a and b:
5
6
The sum of 5 and 6 is : 11
Pass by Reference:
When pass by reference technique is used, the address of the data item is passed to the called function. Using “&” operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don’t need to return anything to calling function.
Output:
Enter the value for a and b:
5
6
The sum of 5 and 6 is : 11
I’d been honored to get a call from a friend as he
found the important guidelines shared in your site. Examining your
blog post is a real wonderful experience. Thanks again for taking into account readers at all like me, and I desire
for you the best of achievements for a professional in this discipline.