Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Call by value
3.
Call by reference
4.
Other Techniques of Parameter Passing
5.
Pass by Result
6.
Pass by Value-Result
7.
Pass by name 
8.
Frequently Asked Questions
8.1.
What methods allow the passed parameter data to be changed?
8.2.
Which method passes a copy of the parameter to the function?
8.3.
Which method’s approach is time and space-efficient?
8.4.
Which method employs the in-mode semantics technique?
8.5.
Algol programming language utilizes which parameter passing technique?
9.
Conclusion
Last Updated: Mar 27, 2024

Parameter Passing Techniques

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

There are several techniques for passing parameter data into and out of methods and functions. Assume that function B() is called from another function A. (). In this scenario, A is referred to as the "caller function," whereas B is referred to as the "called function or callee function." Furthermore, the arguments that A sends to B are referred to as actual arguments, whereas B's parameters are referred to as formal arguments. When a function is called, the calling function must send certain values to the called functions.

There are two ways by which we can pass the parameters to the functions:

  • Call by value
  • Call by reference
     

Also see: C Static Function, Difference between argument and parameter

Call by value

  • The variables' values are supplied to the called function by the calling function.
  • If any parameter value in the called function has to be changed, the change will be reflected solely in the called function.
  • This occurs because all modifications are done to the copy of the variables rather than the originals.
     

This technique employs in-mode semantics. Changes to formal parameters are not returned to the caller. Any changes to the formal parameter variable within the called function or method affect just the separate storage location and are not reflected in the real parameter in the calling environment. This approach is also known as call by value.

#include <stdio.h>
  
void funExample(int a, int b)
{
    a += b;
    printf("In func, a = %d b = %d\n", a, b);
}

int main(void)
{
    int x = 5, y = 7;
  
    funExample(x, y);
    printf("In main, x = %d y = %d\n", x, y);
    return 0;
}


Output:

In funExample, a = 12 b = 7

In main, x = 5 y = 7

Also See, procedure call in compiler design“ and  Short int in C Programming
 


Call by reference

  • The addresses of the variables are given from the calling function to the called function in this case.
  • The address used within the function is used to obtain the parameter used in the call.
  • Any modifications made to the parameters have an effect on the passed argument.
  • Argument pointers are supplied to functions exactly like any other value when passing a value to a reference.
     

This method employs in/out-mode semantics. Changes to formal parameters are returned to the caller through parameter passing. Any changes to the formal parameter are reflected in the calling environment because the formal parameter obtains a reference (or pointer) to the real data. This approach is also known as a call-by-reference method. This approach is both time and space-efficient.
 

#include <stdio.h>
  
void swapNum(int* i, int* j)
{
    int temp = *i;
    *i = *j;
    *j = temp;
}
  
int main(void)
{
    int a = 15, b = 100;
  
    swapNum(&a, &b);
  
    printf("a is %d and b is %d\n", a, b);
    return 0;
}


Output:

a is 100 and b is 15


You can also read about C dynamic array.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Other Techniques of Parameter Passing

These are old techniques used in prior programming languages such as Pascal, Algol, and Fortran. These tactics are ineffective in high-level languages.

Pass by Result

This approach makes use of out-of-date semantics. Just before control is transferred to the caller, the value of the formal parameter is returned to the actual parameter. This method is sometimes referred to as call-by-result. In most cases, a copy is utilized to implement the pass-by-result technique.

Pass by Value-Result

This technique employs in/out-mode semantics. It combines the concepts of Pass-by-Value and Pass-by-Result. Just before control is transferred to the caller, the value of the formal parameter is returned to the actual parameter. This approach is also known as call by value-result.

Pass by name 

This method is utilized in programming languages like Algol. In this technique, the symbolic "name" of a variable is provided, allowing it to be accessed as well as updated.

Example:
To double the value of C[j], use the following technique and pass its name (not its value).
 

procedure double(x);
  real x;
begin 
  x:=x*2
End;

 

Pass-by-name has the effect of textually substituting the argument in a procedure call with the matching parameter in the procedure body.
 

Pass-by-Name Mechanism Implications: Each time the formal parameter is supplied, the argument expression is re-evaluated. The procedure has the ability to alter the values of variables used in the argument expression, hence changing the expression's value.

Frequently Asked Questions

What methods allow the passed parameter data to be changed?

Call by reference allows passed parameters to be changed. A reference to the variable is passed, which allows the program to access the memory location of the variable so that it can change its actual value of it.

Which method passes a copy of the parameter to the function?

In Call by Value, a copy of the parameters is passed to the function. This makes sure that the function cannot change the actual value of the parameter.

Which method’s approach is time and space-efficient?

Call by reference is time and space-efficient. Call by reference doesn’t create a copy of the passed argument, which reduces the additional overhead, in turn making it time and space-efficient.

Which method employs the in-mode semantics technique?

Call by Value employs the in-mode semantics technique. A read-only variable is usually supplied as in-mode. 

Algol programming language utilizes which parameter passing technique?

Pass by name is utilized by the ALGOL programming language. ALGOL is an abbreviation for the algorithmic language. It is a portable computer language family for scientific calculations that has had a significant effect on other languages. Furthermore, for nearly 30 years, the ACM utilized ALGOL as the standard approach for generating algorithms in textbooks and academics.

Conclusion

In this article, we looked at various parameter passing techniques and also some others as well, which were used in old programming languages. If you would like to learn more, check out Introduction to Function and Function Parameter in C,  and other articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Nested Functions in C
Next article
Power Function in C
Live masterclass