






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Cairo University Faculty of Computers and Artificial Intelligence Computer Science Department
Concepts of Programming
[For more details, refer to āConcepts of Programming Languagesā by Robert Sebesta]
A subprogram definition consists of:
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);
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:
#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));}
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 }
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:
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
Consider the following recursive C++ implementation of MergeSort():
template
template
#include
template
stack
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
// MergeSort
// Merge
s.push(c3); s.push(c2); s.push(c1); } }