6.1. Parameter-Passing Mechanisms — Programming Languages (2024)

6.1.1. Call By Value vs. Call By Reference

Author’s Note: All of the visualizations of parameter-passing methodsin this were developed by University of Wisconsin Oshkosh CS majorCory Sanin. His work on these has greatly improved the originalversion of the module.

Parameter-passing techniques may be broken down as follows:

  • Eager evaluation (applicative order) techniques. What these methods have in common is that the arguments passed in for a function’s parameters are evaluated before the function is called.

    • Call-by-value

    • Call-by-reference

    • Call-by-copy-restore (also known as value-result or copy-in-copy-out)

  • Lazy evaluation (normal order) techniques. What these methods have in common is that the evaluation of the arguments passed in for a function’s parameters is delayed until the argument is actually used in the execution of the function.

    • Macro expansion

    • Call-by-name

    • Call-by-need

The difference between call-by-value and call-by-reference isexemplified by the difference between denoted values in ourinterpreters for SLang 1 and SLang 2. That is, in call-by-value, theargument for a function parameter is a copy of the value of theargument whereas, in call-by-reference, the function is given theaddress of the argument. Given the address, the function has thecapability of modifying the argument.

To see how call-by-value works, step through a few sample programsusing the slide show generator below. Once you’re confident that youunderstand each step, test yourself with the proficiency exercise thatfollows.

Test yourself on call-by-value by completing the following proficiencyexercise.

In comparison to call-by-value, call-by-reference is illustrated bythe following slide show generator. Again step through a few of thegenerated slide shows until you’re ready for the proficiency exercisethat follows.

Test yourself on call-by-reference by completing the following proficiencyexercise.

Now that you’ve seen the difference between call-by-value andcall-by-reference, we will end this section with a problem that will helpyou review the difference between call by value and call byreference in the language C++, where the presence of an ampersand infront of the parameter’s name is used to indicate call-by-referencesemantics. To earn credit for it, you must complete this randomizedproblem correctly three times in a row.

6.1.2. Copy-Restore

In copy-restore parameter passing, the function is still given theaddress of the argument, as it was in call-by-reference. However, theprotocol for this technique dictates that the function make a copy ofthe argument before executing the function body. This copy is thenworked with in the function body. When the function body hascompleted, the protocol for copy-restore dictates that the copy of theargument be “restored into” the original argument using the address ofthe argument, hence potentially modifying that argument. Note thatalthough the original argument is modified, the timing of when themodification occurs is slightly different from what it was undercall-by-reference semantics. In the Ada programming language, theprogrammer could choose to use copy-restore semantics by designating aparameter as an in-out parameter. Although C++ does not offercopy-restore as a parameter-passing technique, we can simulate it inthe following C++ code.

#include <iostream>using namespace std;void by_value(int a, int b) { a = b; b = 6;}void by_reference(int &a, int &b) { a = b; b = 6;}void by_copy_restore(int &a, int &b) { int copya, copyb; copya = a; // copy-in phase copyb = b; copya = copyb; // function proper copyb = 6; a = copya; // copy-out phase b = copyb;}int main() { int x,y; x = 4; y = 5; by_value(x, y); cout << "Call-by-value semantics: " << x << " " << y << endl; x = 4; y = 5; by_reference(x, y); cout << "Call-by-reference semantics: " << x << " " << y << endl; x = 4; y = 5; by_copy_restore(x, y); cout << "Call-by-copy-restore semantics: " << x << " " << y << endl;}

As you’ve done with by-value and by-reference, use the followingslide show generator to step through a few examples of the copy-restoremethod and then test yourself by working on the proficiency exercisethat follows.

Author’s Note: In the slide show above, the pointers from r and sback to the arguments of the function call exist, and should be shown,as soon as the function is invoked and throughout the execution of thefunction call.

So, as you can tell from the C++ code above, in call-by-copy-restore,a function parameter corresponds to two values, both a pointer to thecorresponding argument and a copy of the value of the argument. First,the copy of the argument’s value is made. Then, the body of thefunction only uses the copy during its execution. Finally, during therestore phase just before the function returns, the local copy of theargument (i.e., its final value, once the function’s execution hascompleted) is copied back into the argument.

Note that, when there are more than one parameter, the restore phasetakes place for each parameter from left to right in the function’ssignature. This order is required by the specification of thisparameter-passing mechanism.

Can you think of scenarios in which the left-to-right order of the restorephase matters?

Now, test yourself with a copy-restore proficiency exercise.

We’ve now covered the three parameter-passing methods that use eagerevaluation of function arguments.

Before moving on, make sure that you understand why these three methodsindeed use eager evaluation.

Now, to compare and contrast these three methods, figure out what theoutput of the program in the next practice problem would be undercall by value, call by reference, and call bycopy-restore. Doing this will clarify the subtle differences amongthese three methods. To earn credit for the following problem, youmust complete it correctly for the randomized program it generatesthree times in a row.

6.1.3. Macro Expansion

Call-by-value, call-by-reference, and call-by-copy-restore all useeager evaluation: The arguments of a function call are evaluatedimmediately, that is, even before the body of the function is executed.

The remaining three parameter-passing mechanisms use lazy evaluation: Thearguments of a function call are passed without being evaluated to the function.Then, during the execution of the function’s body, the parameters areevaluated only when, and as often as, they are needed.

The first lazy-evaluation technique we will discuss is macro-expansion.

Steps involved in macro-expansion are:

  1. No evaluation: The literal text of each argument in the macro call is substituted for the corresponding formal parameter everywhere in the macro’s body.

  2. No evaluation: The body of the macro’s code resulting from Step 1 is textually substituted for the macro call in the caller program.

  3. Evaluation: The body of the macro is executed in the caller’s environment. That is, because of the textual substitution of the macro’s code in the caller program, the scope of the variables involved is determined on the basis of where the macro is called from rather than where the definition of the macro appears in the program. You will see this in the second step of the following slide show, where the code resulting from Step 1 and Step 2 above is presented side-by-side with the original code.

Once you have gone through enough example slide shows to fully understandthe details of each step in macro-style parameter passing, testyourself with the following proficiency exercise.

We conclude this section on macro-expansion parameter passing byconsidering the use of macros in C++, where a parameter like a orb in the example below must be wrapped in parentheses when it isactually used in the body of the macro. Try to determine the outputof the main program in each example.

#include <iostream>using namespace std;#define by_macro( a, b ) { (a) = (b); (b) = 6; } // Note parens around use of parameterint main(){ int x,y; x = 4; y = 5; by_macro(x, y); cout << "Call-by-macro semantics: " << x << " " << y << endl;}
#include <iostream>using namespace std;#define by_macro( a, b ) \ { (a) = (a) + (b); (b) = (a) - (b); (a) = (a) - (b); } // Again parens wrap use of paramint main(){ int x,y; cout << "\nNo aliasing" << endl << endl; x = 4; y = 5; by_macro(x, y); cout << "Call-by-macro semantics: " << x << " " << y << endl; int z; cout << endl << endl << "With aliasing" << endl << endl; z = 4; by_macro(z, z); cout << "Call-by-macro semantics: " << z << endl;}

Implementation of macro-expansion in C++

The implementation of macro-expansion suggested by the 3-step processdescribed previously is to perform a double textual substitution. Forexample, the C++ pre-processor performs this double substitution, andthen the compiler processes the resulting code, never seeing the macrocall. Of course, no function call is executed at run-time either.

Because the body of the macro is spliced intothe caller’s code after the arguments have been substituted(without being evaluated) for the parameters, the whole body of the macro isexecuted in the caller’s environment. This allows us to usemacro-expansion to simulate dynamic scoping, as illustrated in thefollowing code.

#include <iostream>using namespace std;int n = 6;#define dynamic_scoping { cout << n << endl; }void static_scoping() { cout << n << endl; }void test_dynamic() { int n = 5; cout << "Using dynamic scoping --> "; dynamic_scoping;}void test_static() { int n = 5; cout << "Using static scoping --> "; static_scoping();}int main() { test_dynamic(); test_static();}

The following problem will help you review the differences among call byreference, call by copy-restore, and call by macro. To earncredit, you must complete this randomized problem correctlythree times in a row.

6.1.4. Call By Name

In macro expansion, the body of the macro is spliced into the caller’scode after the actual parameters have been substituted (without beingevaluated) for the formal parameters. Therefore, the whole body of themacro is executed in the caller’s context (i.e., the caller’s environment).

In call-by-name, no code is spliced into the caller’s code. Instead,the body of the function is executed in its own context, but theactual parameters, which are substituted for the formal parameters,are evaluated in the caller’s context.

Call-by-name differs from macro expansion in that only the parametersare evaluated in the caller’s context, not the whole body of thefunction. Step through a few slide shows of some call-by-nameexamples to study the ramifications of this change. When you areconfident that you understand the subtleties involved, try theproficiency exercise that follows.

Author’s Note: In the slide show above, the arrows from theparameters to the arguments are NOT actual pointers but rather a wayto depict the fact that each parameter has a way (which we’ll describeunder the name ‘thunk’ in the next section) to refer back to thearguments in the caller’s environment.

Now it is time for you to do a proficiency exercise to see how wellyou understand call-by-name. When you do this proficiency exercise,each assignment statement will require two steps. In the first stepcorresponding to an assignment statement, you will have to compute thevalue on the right-hand side and then click the location where thatvalue will be stored. In the second step, you will have to click on apotentially new arrow destination resulting from the computation andassignment that comprised your answer for the first step.

The following problem will help you review the differences among call bycopy-restore, call by macro, and call-by-name. To earn creditfor it, you must complete this randomized problem correctly threetimes in a row.

6.1.5. Comprehensive Review of the Five Methods Studied So Far

In the next section, we will examine call-by-name versus call-by-needin the context of a specific example known as a lazylist. However, before proceeding, test your comprehensiveunderstanding of all five techniques studied so far: call-by-value, call-by-reference,call-by-copy-restore, call-by-macro,and call-by-name. To earn credit for it, you must complete thisrandomized problem correctly three times in a row.

6.1. Parameter-Passing Mechanisms — Programming Languages (2024)

FAQs

What is parameter passing in programming language? ›

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.

What is the default parameter passing mechanism answer? ›

The default parameter passing mechanism is called call by value. It is used in C programming to pass arguments.

What is the parameter passing mechanism in C++? ›

Parameter Passing in C++

There are three parameter-passing modes in C++: by value, by pointer, and by reference. When a parameter is passed by value, the changes made to the parameter within the function do not affect the value of the actual argument used in the function call.

What is the mechanism for passing parameters in Python? ›

Mechanism for Passing Parameters

All scalar values are passed to and from Python by value, meaning a copy of the variable is made, and changes to the variable on one side will not affect the other side's value. String arrays, lists, tuples, and dictionaries are passed to and from by value.

What are the 3 ways to pass parameters? ›

Parameter Passing Techniques

There are a number of different ways a programming language can pass parameters: Pass-by-value. Pass-by-reference. Pass-by-value-result.

What is parameter passing with example? ›

In C, there are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case, A is called the “caller function” and B is called the “called function or callee function”.

What are parameter passing mechanisms? ›

In the context of computer programming, parameter passing refers to the process of sending data, known as arguments or actual parameters, to a function or method when it is called. These arguments are then used by the function or method to perform its tasks.

What is the default parameter passing mechanism in C language? ›

Parameter passing is the mechanism used to pass parameters to a procedure (subroutine) or function.In C-Language the Default Parameter Passing Mechanism is called Call By Value where we pass the value of the actual parameter.

Which mechanism is used by a function to pass parameter? ›

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

What are the parameter passing methods in C programming? ›

A Parameter is the symbolic name for "data" that goes into a function. There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

What is parameter passing in Java? ›

Parameter passing in Java refers to the mechanism of transferring data between methods or functions. Java supports two types of parameters passing techniques. Call-by-value. Call-by-reference.

What is actual parameter in C++? ›

Actual parameters are the values that are passed to the function during a function call. They are also known as arguments. Actual parameters are used to provide the values to the formal parameters of the function. Actual parameters can be of any data type such as int, float, char, etc.

What are the parameter passing methods in Golang? ›

Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference.

What are the 4 types of arguments in Python? ›

Function arguments are values passed to a function when it is called. On the other hand, parameters are the variables inside the function's parentheses. There are four types of function arguments in Python: required arguments, default arguments, keyword arguments, and arbitrary arguments.

What is parameter in Python with example? ›

Parameters in python are variables — placeholders for the actual values the function needs. When the function is called, these values are passed in as arguments. For example, the arguments passed into the function .

What are the two types of parameter passing? ›

There are two ways to pass parameters in C: Pass by Value, Pass by Reference.
  • Pass by Value. Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. ...
  • Pass by Reference. A reference parameter "refers" to the original data in the calling function.

What is parameter and how it is passed to function? ›

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: js. function example(parameter) { console.

What is meant by passing a parameter by value? ›

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.

What is parameter in language learning? ›

So, in sum, the word “parameter” is simply the name given to those abstract features of grammar (a) that govern many different observable structures and (b) that vary from language to language.

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6482

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.