Parameter Passing Techniques In C/C++

Parameter Passing Techniques In C/C++

Caller Function (A): The caller function, also known as the calling function or the client function, is the function that initiates the call to another function. Function A is the caller function because it is calling function B.Called Function or Callee Function (B): The called function, also known as the callee function or the target function, is the function that is invoked or executed when called by another function. Function B is called a function because it is invoked by function A. Actual Arguments: Actual arguments, also referred to as arguments or parameters, are the values or expressions that are passed to a function when it is called. These values are specific to the particular function call and are used as input for the function’s computation. In your example, the arguments sent from function A to function B are the actual arguments.

Terminology

  1. Formal Parameter: A formal parameter is like a variable declaration within the function or method’s prototype or definition. It specifies the name of the parameter and its data type. It serves as a placeholder for the actual value that will be passed when the function or method is called.
  2. Actual Parameter: An actual parameter, on the other hand, is the real value or expression provided in the function or method call, which corresponds to a formal parameter. It is the concrete data that gets passed into the function or method when it is invoked.
  3. Modes:
    • IN Mode: When a parameter is passed as IN, it means the caller is providing information to the callee. The callee can use this information but cannot modify the original value in the caller.
    • OUT Mode: When a parameter is passed as OUT, it means the callee can write or modify the value of this parameter, and those modifications will be reflected in the caller’s environment.
    • IN/OUT Mode: This combines both IN and OUT modes. The caller provides an initial value to the callee, and the callee can both use and modify the value, which will then be visible to the caller after the function or method call.

These concepts are often used in programming to control how data is passed between functions or methods, whether data is read-only, write-only, or both.

Important Methods of Parameter Passing

1. Pass By Value

In this method, when you pass a parameter to a function or method, it’s like making a photocopy of a document. The function works with the photocopy (the formal parameter), not the original document (the actual parameter) from the caller. Any changes made to the photocopy don’t affect the original document. So, modifications to the formal parameter inside the function only affect its own copy and don’t impact the original value in the caller’s environment. This is why it’s called “call by value” because you’re passing the value, not the original variable itself.

Example of Pass by Value


// C program to illustrate
// call by value
#include <stdio.h>
 
void func(int a, int b)
{
    a += b;
    printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
    int x = 5, y = 7;
 
    // Passing parameters
    func(x, y);
    printf("In main, x = %d y = %d\n", x, y);
    return 0;
}

Output

In func, a = 12 b = 7
In main, x = 5 y = 7

Disadvantages Of Pass By Value

  1. Inefficiency in Storage Allocation: When you pass data by value, a copy of that data is created. This can be problematic for objects or arrays because making copies of large data structures consumes a significant amount of memory and can slow down your program. Imagine copying a large dataset every time you pass it to a function – it’s not efficient in terms of memory usage.
  2. Costly Copy Semantics: Copying objects or arrays can be costly in terms of time and memory. It takes time to duplicate the data, and if the data is large, it can lead to performance issues. Additionally, if you have multiple functions or methods that need to work on the same data, making copies every time can lead to redundancy and waste of resources.

2. Pass by reference(aliasing)

In this method, when you pass a parameter, it’s like giving the function a direct map to the original data. Any changes made to the map (the formal parameter) affect the actual data (the actual parameter) in the caller’s environment. It’s like multiple people looking at the same map and drawing on it together. This approach is called “call by reference” because you’re passing a reference to the original data, making it efficient in both time and space since you’re not creating unnecessary copies.


// C program to illustrate
// call by reference
#include <stdio.h>
 
void swapnum(int* i, int* j)
{
    int temp = *i;
    *i = *j;
    *j = temp;
}
 
int main(void)
{
    int a = 10, b = 20;
 
    // passing parameters
    swapnum(&a, &b);
 
    printf("a is %d and b is %d\n", a, b);
    return 0;
}

Output

a is 20 and b is 10

Disadvantages of Pass by reference

Programming involves numerous scenarios and can be challenging due to their complexity. Code can be intricate, debugging isn’t always easy, and maintaining projects can be tricky. Clear documentation and teamwork help mitigate these issues.

Other Methods of Parameter Passing

These techniques are older and were used in earlier programming languages like Pascal, Algol, and Fortran. These techniques won’t be applicable in high-level languages.

1. Pass by Result

In this method, known as “out-mode semantics” or “call by the result,” the formal parameter’s value is sent back to the actual parameter just before the control returns to the caller. This is often implemented by making a copy of the value.

2. Pass by Value-Result

This method uses “in/out-mode semantics,” which is a mix of pass-by-value and pass-by-result. Just before the control returns to the caller, the formal parameter’s value is sent back to the actual parameter. It’s also known as “call by value-result.”

3. Pass by Name

This method is commonly used in programming languages like Algol. In this technique, the variable’s symbolic “name” is passed as a parameter, allowing it to be both accessed and updated within a function or method. This method enables direct manipulation of the original variable using its name.

Example

procedure double(x);
  real x;
begin 
  x:=x*2
end;
  1. Re-evaluation of Argument: With pass-by-name, the argument expression is re-evaluated each time the formal parameter is passed. This means that the value of the argument is recalculated whenever it’s accessed within the procedure.
  2. Variable Value Changes: Since the argument expression is re-evaluated, the procedure can modify the values of variables used in that expression. Consequently, this can alter the overall value of the expression. It allows the procedure to change the variables used in the argument expression and, as a result, modify the expression’s final value.

FAQ- Parameter Passing Techniques In C/C++

Q1. What are the parameter-passing techniques in C++?

Ans.
In C++, when a parameter is passed “by value,” a copy of the actual argument’s value is created for use within the function. Any changes made to this copy inside the function do not affect the original value of the actual argument that was used in the function call. In other words, modifications to the parameter inside the function are local and do not impact the value outside the function.

Q2. What are the default parameters in C++?

Ans. Default arguments in C++ allow you to assign default values to function parameters. These default values are specified at the trailing end (right side) of the parameter list. When calling the function, if an argument for a parameter with a default value is not provided, the default value is used. Default arguments enhance code reusability, simplify function calls, and make programs more effective by providing sensible default behavior when needed.

Q3. What does & mean in C++ parameters?

Ans. The ampersand symbol & is referred to as a reference declarator in addition to being the address operator in C ++.


Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment