Parameter Passing Techniques in C
Parameter Passing: In computer programming, sending parameters to functions and procedures is known as parameter passing. It controls how parameters are sent from the calling code to the called function and their values.
- The function that starts a call to another function is referred to as the caller function, often called the calling function or the invoking function. It has the code that causes the called function to run when it is called. The caller function, in other words, "calls" or "invokes" the called function.
- Called Function is the function that is being called or invoked by another function is referred to as the called function, callee function, or invoked function. It is the function that does a certain operation or calculation using the inputs that the calling function has supplied to it. The called function runs its programme and could execute other commands or return a result. And parameters are passed in the functions during calling of the function.
Example:
#include <stdio.h> void called_function() { printf("This is the called function.\n"); } void caller_function() { printf("This is the caller function.\n"); called_function(); printf("Returned from the called function.\n"); } int main() { caller_function(); return 0; }
Output:
In programming languages, there are a number of methods for passing parameters:
1) Pass by Value
The value of the real parameter is transferred to the formal parameter using the pass-by-value approach. The value of the real parameter is unaffected by any changes made to the formal parameter inside the function. Languages like C, Java, and Python utilize this method for transmitting simple data types.
Example: The below C code shows us the Pass-by-Value illustration.
#include <stdio.h> void number(int n) { n = n + 1; printf("Inside the function: %d\n", n); } int main() { int x = 10; printf("Before function call: %d\n", x); number(x); printf("After function call: %d\n", x); }
Output:
Explanation
The code illustrates passing parameters in C using pass by value. It specifies a “number” function with a single integer parameter, “n”. “n” is printed after being increased by “1” inside the function. In the “main” function, the initial value supplied to number does not change. This exemplifies the behaviour of “pass by value”, which allows alterations within the function without changing the original value by passing a copy of the argument's value to the function. The code highlights the difference between the original variable “x” in main and the local variable “n” in number.
2) Pass by Reference: A function or method parameter is supplied to using the parameter passing approach known as "pass by reference," which involves sending a reference to the argument's memory address. This enables the function to immediately access and alter the argument's starting value.
In pass-by-reference:
- Instead of its value, the argument's address is provided to the parameter.
- The original argument will change if the parameter is altered inside the function.
- It is possible for two-way communication and data exchange since the function and the caller code both make reference to the same
memory address.
Example: Here is a C sample code that shows pass by reference in action:
#include <stdio.h> int sum(int* a, int* b) { int sum = *a + *b; return sum; } int main() { int x = 5; int y = 10; printf("Values of x = %d, y = %d\n", x, y); printf("Sum is %d\n", sum(&x, &y)); return 0; }
Output:
- The above C code exemplifies “pass by reference”. It defines a “sum” function with two integer pointers as its inputs. The function computes the sum of the values the pointers are pointing to and then returns the result. Two integer variables named “x” and “y” are defined and given values in the “main” function. After that, the sum function receives as arguments the addresses of “x” and “y”. The “sum” function computes their sum by referencing the values in the memory locations pointed to by the parameters. The estimated total is finally reported in the main function.
- When you wish to change a function argument's initial value and avoid duplicating huge objects or arrays, passing via reference might be helpful.
- In languages that allow it (like C++ and C#), pointers or references are frequently used to pass by reference. For some kinds, such as objects or arrays, pass by reference may be the default behaviour in other languages.
3) Pass-by-Pointer:
Pass-by-pointer is similar to pass-by-reference, except that a pointer to the argument's memory address is passed rather than the argument's actual memory address. Through the pointer, the function has access to and control over the parameter. Particularly in languages like C that do not provide native pass-by-reference, this method is employed.
Here is a pass-by-pointer example using the C programming language:
#include <stdio.h> void decrement(int *n) { (*n)--; // Value is decreased in memory location for variable n } int main() { int num= 5; printf("Before decrement: %d\n", num); decrement(&num); // Pass the memory address of 'num' by using the '&' operator printf("After decrement: %d\n", num); return 0; }
Output:
- An integer variable named “num” is set to the number 5 in the “main” function. The memory address of “num” is given as an argument along with the & operator when using the “decrement” function. This indicates that the memory value of “num” will be changed by the “decrement” function.
- The new value of “num” is displayed to the terminal using printf following a call to the decrement function.
There are some other parameters passing methods:
4) Pass-by-Name: Using this method, the argument is not evaluated when the function is called. Instead, a textual replacement or expansion resembling a macro is supplied in place of the argument phrase. When the function needs a value, it then evaluates the parameter expression. This less popular and more often employed method.
- The argument is not evaluated when calling a function while using pass-by-name. The argument expression is instead given as a textual substitution or expansion like a macro. When the function needs a value, it then evaluates the parameter expression.
Here is a pass-by-value example in the C programming language:
#include <stdio.h> void increment(int num) { num++; // Increment the value of 'num' within the function }int main() { int number = 5; printf("Before increment: %d\n", number); increment(number); // Pass 'number' as an argument printf("After increment: %d\n", number); return 0; }
Output:
- The “increment” function in this illustration has an integer “num” as an argument. When number is provided to “increment” as an argument, the parameter “num” receives a copy of number's value. The initial number will not be impacted by any changes made to “num” inside the “increment” function.
5) Pass-by-Value-Result: It works similarly to pass-by-value in that the argument's value is originally transferred into the parameter. When the function is finished, any modifications made to the parameter are reflected in the initial argument.
- With this method, the argument may be duplicated independently for the function and the caller, and any modifications done inside the function are mirrored in the original parameter.
- Pass-by-value-result is supported in various languages, however it is not a native feature of C. But by explicitly supplying references to the parameters and altering the values subtly via the pointers, you may achieve a same result.
Key Points to remember about Parameter passing:
- The process of sending arguments to functions or methods in programming languages is known as parameter passing.
- Changes to passed arguments are possible when “Pass by reference”. A reference to the variable is supplied, enabling the program to access the variable's memory location and modify its actual value.
- A copy of the arguments are sent to the function in “pass by value”. By doing this, it is ensured that the function cannot alter the parameter's real value.
- It saves both time and space to “pass by reference”. It is more time and space efficient to call by reference since there is no need to make a duplicate of the provided argument.
- In a pass-by-value-result, the argument's value is first copied into the parameter, and after the function's conclusion, the parameter's value is transferred back to the original argument.
- The argument expression is supplied as a textual replacement or macro-like expansion when using the less popular pass-by-name approach. Every time it requires a value, the function evaluates the expression.
- The function's behaviour and its interaction with the original input depend on the parameter passing approach that is used.
- multiple programming languages may have multiple built-in methods for sending parameters, and some languages let you explicitly indicate which method you want to use.