Docsity

Parameter Passing in Programming Languages: Modes and Examples, Summaries of Software Engineering

Toggle sidebar
bg1
Cairo University
Faculty of Computers and Artificial Intelligence
Computer Science Department
Concepts of Programming
Languages Subprograms Dr. Amin Allam
[For more details, refer to ā€œConcepts of Programming Languagesā€ by Robert Sebesta]
1 Subprogram definition
Asubprogram definition consists of:
•Subprogram header: specifies the subprogram kind,name, and protocol. The subprogram kind is
either function or procedure. A procedure is a function without return type. The protocol consists
of parameter profile and return type if it is a function in a typed language. The parameter profile
specifies the number, order, and types (for typed languages) of its formal parameters.C++ and
Java use the reserved word void to indicate no return type.
•Subprogram body: specifies a sequence of statement which are executed in order when another
subprogram calls it. A subprogram call is the explicit request to execute its body. The calling
subprogram provides the called subprogram with actual parameters to be bound to its formal pa-
rameters. Sometimes, the term parameters is used for formal parameters while the term arguments
is used for actual parameters. The calling subprogram is suspended during the execution of the
called subprogram.Control is passed to the entry point (usually the first statement) of the called
subprogram.Control returns to the caller when the called subprogram execution terminates.
The correspondence between actual and formal parameters is usually done by position. That is,
the ith actual parameter is bound to the ith formal parameter. That method is called positional
parameters. Another method provided by some languages is called keyword parameters which
specifies the name of the formal parameter to be bound with the actual parameter. The advantage
of this method is that parameters can appear in any order so the programmer does not need to
remember the order of formal parameters, such as the following Python subprogram call:
Fun(length=my_length, list=my_array, sum=my_sum)
In several languages, default values can be associated to formal parameters which are used when-
ever the subprogram call does not specify the corresponding actual parameters.
C# allows a method to accept a variable number of parameters of the same type with the params
modifier, where the caller sends either array or list of expressions:
public void DisplayList(params int[] list)
{foreach (int val in list) Console.WriteLn("Val={0}", val);}
The above function can be called by passing a list or a variable number of parameters such as:
obj.DisplayList(2, 4, 5, x-1, 17);
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Parameter Passing in Programming Languages: Modes and Examples and more Summaries Software Engineering in PDF only on Docsity!

Cairo University Faculty of Computers and Artificial Intelligence Computer Science Department

Concepts of Programming

Languages Subprograms^ Dr. Amin Allam

[For more details, refer to ā€œConcepts of Programming Languagesā€ by Robert Sebesta]

1 Subprogram definition

A subprogram definition consists of:

  • Subprogram header: specifies the subprogram kind, name, and protocol. The subprogram kind is either function or procedure. A procedure is a function without return type. The protocol consists of parameter profile and return type if it is a function in a typed language. The parameter profile specifies the number, order, and types (for typed languages) of its formal parameters. C++ and Java use the reserved word void to indicate no return type.
  • Subprogram body: specifies a sequence of statement which are executed in order when another subprogram calls it. A subprogram call is the explicit request to execute its body. The calling subprogram provides the called subprogram with actual parameters to be bound to its formal pa- rameters. Sometimes, the term parameters is used for formal parameters while the term arguments is used for actual parameters. The calling subprogram is suspended during the execution of the called subprogram. Control is passed to the entry point (usually the first statement) of the called subprogram. Control returns to the caller when the called subprogram execution terminates.

The correspondence between actual and formal parameters is usually done by position. That is, the ith^ actual parameter is bound to the ith^ formal parameter. That method is called positional parameters. Another method provided by some languages is called keyword parameters which specifies the name of the formal parameter to be bound with the actual parameter. The advantage of this method is that parameters can appear in any order so the programmer does not need to remember the order of formal parameters, such as the following Python subprogram call:

Fun(length=my_length, list=my_array, sum=my_sum)

In several languages, default values can be associated to formal parameters which are used when- ever the subprogram call does not specify the corresponding actual parameters.

C# allows a method to accept a variable number of parameters of the same type with the params modifier, where the caller sends either array or list of expressions:

public void DisplayList(params int[] list) {foreach (int val in list) Console.WriteLn("Val={0}", val);}

The above function can be called by passing a list or a variable number of parameters such as:

obj.DisplayList(2, 4, 5, x-1, 17);

2 Subprogram declaration

A subprogram declaration provides the subprogram header but does not include its body. It is required in languages that do not allow forward references to subprograms. C and C++ require all subprograms to be either defined or declared before they are called and inside the same translation unit where they are called.

double Fun(int, double); // Declaration (called prototype in C++) int main() { int a=5; double b=9.4; Fun(5, b); return 0; } double Fun(int x, double y) {return x+y;} // Definition

A project with multiple source (cpp) files in C++ is compiled by the following steps:

  • For each source file, independently of other source files, the preprocessor processes the source file by expanding all macros (instructions starting with #), usually by simple text substitution, to produce a translation unit. For example, consider the following file:

#include "mylib.h" #define Max(A, B) ((A)>(B)?(A):(B)) double F(int y, int z) {return G()+Max(2*y, z/5);}

Assuming that the file mylib.h consists of the following:

double G();

The cpp file will be expanded by the preprocessor to the following translation unit:

double G(); double F(int y, int z) {return G()+((2y)>(z/5)?(2y):(z/5));}

  • For each translation unit, independently of other translation units, the compiler compiles the translation unit to produce an object file. The compiler requires that all functions are either defined or declared before its first call in the same translation unit, in order to perform static type checking to validate type compatibility between actual and formal parameters.
  • The linker combines all object files into one executable image (also called load module), which is a machine-readable executable file or library. The addresses of called functions (such as G()) are not necessarily known during compilation because only their declarations may be available. Therefore, the linker is responsible for resolving all function calls by calculating the correct ad- dresses and placing them into the corresponding call statements inside the executable. To be able to do so, the definition of each function must exist exactly in one translation unit.

The above compilation and linking mechanisms reduce compilation time by avoiding recompila- tion of source files which are not changed, including source files for built-in C++ libraries. Only the changed source files of a project need to be recompiled.

The following example illustrates the parameter passing modes:

void Fun(in int a, out int b, in-out int c) { // Initially : a=7, b=has undefined value , c=

a=1; b=2; c=3; // Now: x=7 (no change), y=8 (no change) // z=9 (no change) if c is passed by valueāˆ’ result // z=3 (changed) if c is passed by reference

a=a; b=b; c=c; // Do something

} // Immediately before function returns : // x=7 (no change), y=2 (changed), z=

void main() { int x=7, y=8, z=9; Fun(x, y, z); // The above comments trace this call }

4 Implementing subprogram calls

We examine the implementation of subprogram calls focusing on the call and return procedures. Initially, we assume that the called subprograms do not contain any inner blocks. Subprograms with inner blocks are considered later on.

Each subprogram has the following simplified typical activation record:

Return variable Local variables Parameter variables Return address

Consider the following C++ function. The numbers shown on the left are the addresses of each instruction. Note that program instructions are loaded into memory and obtain memory addresses before they are executed. The activation record of this function is shown on the right:

int factorial(int n) { // Position 1 1004 if(n<=1) return 1; 1008 int f=factorial(n-1); 1012 int r=n*f; 1016 return r; } // Position 2

Return variable int Local variable int f Local variable int r Parameter variable int n Return address

Subprogram calls are implemented in the same way for recursive and non-recursive subprograms, but the main reason for such implementation is to support recursive subprograms. Consider the following C++ program which calls the above function:

int main() { 2004 int v=factorial(3); 2008 return 0; }

Each call to factorial() starts by pushing to the run-time stack an activation record instance of the activation record of the factorial() function. The run-time stack of a specific program is part of the main memory assigned by the operating system to this program and can be used to allocate its stack-dynamic variables. An activation record instance (ARI) is a specific instance of the activation record with specific allocated variables.

The call to factorial(3) in instruction 2004 starts by pushing to the run-time stack the fol- lowing activation record instance. So, when execution reaches Position 1 for the first time, the run-time stack contains:

Return variable int Local variable int f Local variable int r Parameter variable int n 3 Return address 2008

The return address is the address of the instruction that follows the function call instruction. This address will be used by the compiler to know where is should continue execution (pass control) after the function call terminates.

Since the size of an activation record instance for a specific function is known before the function call (actually it is known at compile time), only one memory allocation is needed to allocate the whole activation record instance which is efficient.

Then, control reaches instruction 1008 then calls factorial(2) which starts by pushing an- other activation record instance to the run-time stack. So, when execution reaches Position 1 for the second time, the run-time stack contains:

Return variable int Local variable int f Local variable int r Parameter variable int n 2 Return address 1012 Return variable int Local variable int f Local variable int r Parameter variable int n 3 Return address 2008

Then, control reaches instruction 1016 and the run-time stack contains:

Return variable int 2 Local variable int f 1 Local variable int r 2 Parameter variable int n 2 Return address 1012 Return variable int Local variable int f Local variable int r Parameter variable int n 3 Return address 2008

Then, control reaches Position 2 for the second time, which terminates the call of factorial(2). The compiler saves the return value and return address in registers, then pops the activation record instance of factorial(2) from the top of the stack. The saved return value is assigned to the variable f of the activation record instance of factorial(3). Then, control resumes from in- struction at the saved return address 1012. Now, the activation record instance on the top of the stack represents the set of variables associated with the factorial(3) call, and the run-time stack contains:

Return variable int Local variable int f 2 Local variable int r Parameter variable int n 3 Return address 2008

Then, control reaches instruction 1016 and the run-time stack contains:

Return variable int 6 Local variable int f 2 Local variable int r 6 Parameter variable int n 3 Return address 2008

Then, control reaches Position 2 for the third time, which terminates the call of factorial(3). The compiler saves the return value and return address in registers, then pops the activation record instance of factorial(3) from the top of the stack. The saved return value is assigned to the variable v of the activation record instance of main() (we did not show it in the previous figures). Then, control resumes from instruction at the saved return address 2008.

If the subprogram contains inner blocks, the compiler chooses one of the following two ways to implement its calls:

  • Each inner block is treated as a call to a subprogram with no parameters. In this case, the activation record of the subprogram does not contain any variable local to an inner block. Each inner block has its own activation record.
  • The activation record of the subprograms contains local variables which are not local to inner blocks, and also it contains space sufficient to hold the maximum amount of storage for inner block variables at any time during the subprogram execution, as shown in the following example:

void F(int n) { int x, y, z; while(...) { int a, b, c; while(...) { int d, e; } } while(...) { int f, g; } }

Block variable int e Block variable int d Block variable int c Block variable int b and g Block variable int a and f Local variable int z Local variable int y Local variable int x Parameter variable int n Return address

5 Simulating recursion

Consider the following recursive C++ implementation of MergeSort():

template void Merge(T* a, int istart, int imid, int iend) { T* t=new T[iend-istart]; int i=istart, j=imid, k=0; while(i<imid || j<iend) { if(i<imid && (j==iend || a[i]<a[j])) t[k++]=a[i++]; else t[k++]=a[j++]; } for(k=0;k<iend-istart;k++) a[k+istart]=t[k]; delete[] t; }

template // istart=start index, iend=last index+ void MergeSort(T* a, int istart, int iend) { if(iend-istart<=1) return; int imid=(istart+iend)/2; MergeSort(a, istart, imid); MergeSort(a, imid, iend); Merge(a, istart, imid, iend); }

#include using namespace std;

template void MergeSortIterative(T* a, int n) { MergeSortCall stcall; stcall.type=MERGE_SORT; stcall.istart=0; stcall.iend=n;

stack s; s.push(stcall);

while(!s.empty()) { MergeSortCall call=s.top(); s.pop();

if(call.type==MERGE) { Merge(a, call.istart, call.imid, call.iend); continue; }

// Now, we know that call is for MergeSort() if(call.iend-call.istart<=1) continue; // Base case int imid=(call.istart+call.iend)/2;

// MergeSort(a, istart, imid) ; MergeSortCall c1; c1.type=MERGE_SORT; c1.istart=call.istart; c1.iend=imid;

// MergeSort(a, imid, iend); MergeSortCall c2; c2.type=MERGE_SORT; c2.istart=imid; c2.iend=call.iend;

// Merge(a, istart, imid, iend) ; MergeSortCall c3; c3.type=MERGE; c3.istart=call.istart; c3.imid=imid; c3.iend=call.iend;

s.push(c3); s.push(c2); s.push(c1); } }