PARAMETER PASSING METHODS
Parameter-passing methods are the ways in which parameters are transmitted to and/or from called subprograms.
Parameter passing depends on model of subprogram.There are two models for parameter passing-
1. Semantics Models of Parameter Passing
2. Implementation Models of Parameter Passing
1. Semantics Models of Parameter Passing: Formal parameters are characterized by one of three distinct semantics models:
- They can receive data from the corresponding actual parameter, called in mode.
- They can transmit data to the actual parameter, called out mode.
- They can do both, called inout mode.
2. Implementation Models of Parameter Passing: This model consist the following ways of parameter passing.
- Pass by value
- Pass by reference
- Pass-by-Result
- Pass-by-Value-Result
- Pass-by-Name
1. Pass by value: Value of actual parameter in read only mode is transmitted to formal parameters.
2. Pass by reference: Reference/address of actual parameter is transmitted to formal parameters.
3. Pass-by-Result: When a parameter is passed by result, no value is transmitted to the subprogram.
4. Pass-by-Value-Result: Pass-by-value-result is an implementation model for inout-mode parameters. Pass-by-value-result is sometimes called pass-by-copy, because the actual
parameter is copied to the formal parameter at subprogram entry and then
copied back at subprogram termination.
5. Pass-by-Name: Pass-by-name is an inout-mode parameter transmission method. In it parameters are passed by name. Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter.
Program examples:
Call by value:
#include <iostream>
using namespace std;
void show(int x)
{
cout<<x<<endl;
}
int main()
{
int age = 20;
show(age);
show(10);
return 0;
}
Call by reference:
#include <iostream>
using namespace std;
void show(int *x)
{
cout<<*x<<endl;
}
int main()
{
int age = 20;
show(&age);
return 0;
}
References:
- Sebesta,”Concept of programming Language”, Pearson Edu
- Louden, “Programming Languages: Principles & Practices” , Cengage Learning
- Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill.
- E Horowitz, “Programming Languages”, 2nd Edition, Addison Wesley