3rd Unit
3rd Unit
int main()
{
printf("%d", g());
printf("\n");
return 0;
}
Output :
10
Dynamic Scoping: With dynamic scope, a global identifier refers to the identifier associated with the most
recent environment, and is uncommon in modern languages. In technical terms, this means that each
identifier has a global stack of bindings and the occurrence of a identifier is searched in the most recent
binding.
In simpler terms, in dynamic scoping the compiler first searches the current block and then successively all the
calling functions.
// Since dynamic scoping is very uncommon in
// the familiar languages, we consider the
// following pseudo code as our example. It
// prints 20 in a language that uses dynamic
// scoping.
int x = 10;
// Called by g()
int f()
{
return x;
}
// g() has its own variable
// named as x and calls f()
int g()
{
int x = 20;
return f();
}
main()
{
printf(g());
}
Output
20
ii.Pass by Result
With pass-by-result, no value is transmitted to the subprogram. Instead, the formal parameter acts like a local
variable, and before control is transferred back to the caller the variables value is transmitted back to the
actual parameter, because no data is transferred to the subprogram, but it transmits data back to the actual
parameter it is an out-mode semantic. Most typically pass-by-result uses a copy conceptual model.
iii.Pass-by-value-result
This passing method is actually a combination of pass-by-value and pass-by-result. The value of the actual
parameter is used to initialize the corresponding formal parameter, which then acts as a local variable. The
formal parameters must have local storage associated with the called subprogram. At termination, the
subprogram transmits the value of the formal parameter back to the actual parameter. As such, it uses in out-
mode semantics and copy passing conceptual model. Also, pass-by-value-result has the same advantages and
disadvantages as pass-by-value and pass-by-result with some more advantages. The largest extra advantage of
pass-by-value-result is that it solves pass-by- efe e e s aliasi g p o le s.
iv.Pass-by-reference
With pass-by-reference an address (reference to the memory location of the actual parameter) is passed to
the subprogram. It is another example of an inout-mode semantic.
Advantage- It is efficient in both time and space. This is no duplicate space required or copying.
Disadvantage – It increase the time to access formal parameters because of the additional level of indirect
addressing.
// Illustration of pass by reference
#include <iostream.h>
void square (int *x)
{
*x = (*x) * (*x);
}
int main ( )
{
int num = 10;
square(&num);
cout<<" Value of num is "<<num; // Value of num is 100
return 0;
}
As you can see the result will be that the value of a is 100. The idea is simple: the argument passed is the
add ess of the a ia le u . The pa a ete of s ua e fu tio is a poi te poi ti g to t pe i tege . The
add ess of u is assig ed to this poi te . You a a al ze it as follo s: & u is passed to i t * , the efo e it
is the same as:
int *x = #
This ea s that is a poi te to an integer and has the address of the variable num.
Within the function we have: *x = (*x) * (*x);
* when used before a pointer will give the value stored at that particular address. Hence we find the product
of u a d sto e it i u itself. i.e. the alue of is sto ed i the add ess of u i stead of hi h
was originally present there. The diagram below illustrates the difference between pass by value and pass by
efe e e. No he e de efe e e e a e a tuall a ipulati g the alue sto ed i u .
2. Type-Checking Parameters
It is now widely accepted that software reliability demands that the types of actual parameters be checked for
consistency with the types of the corresponding formal parameters.
Example
Result = sub1 (1)
The actual parameter is an integer constant. If the formal parameter of sub1 is a floating-point type, no
error will be detected without parameter type checking.
Early languages, such as Fortran 77 and the original version of C, did not require parameter type checking.
Pascal, FORTRAN 90, Java, and ADA: it is always required
Perl, PHP, and JavaScript do not have type checking.
The question is what referencing environment for executing the past subprogram should be used.
subprogram names that are passed as parameters.
5. Overloaded Subprograms
An overloaded operator is one that has multiple meanings. The types of its operands determine the
For example, if the * operator has two floating-point operands in a Java program, it specifies floating-
meaning of a particular instance of an overloaded operator.
But if the same operator has two integer operands, it specifies integer multiplication.
point multiplication.
An overloaded subprogram is a subprogram that has the same name as another subprogram in the
Every version of an overloaded subprogram must have a unique protocol; that is, it must be different
same referencing environment.
The meaning of a call to an overloaded subprogram is determined by the actual parameter list.
from the others in the number, order, or types of its parameters, or in its return if it is a function.
Users are also allowed to write multiple versions of subprograms with the same name in ADA, Java,
Overloaded subprograms that have default parameters can lead to ambiguous subprogram calls.
C++, and C#.
Function Overloading
If any class has multiple functions with same names but different parameters then they are said to be
overloaded. Function overloading allows you to use the same name for different functions, to perform, either
same or different functions in the same class.
Function overloading is usually used to enhance the readability of the program. If you have to perform one
single operation but with different number or types of arguments, then you can simply overload the function.
Example - int sum (int x, int y)
{
cout<<x+y;
}
FORTRAN 77, Pascal and Modula-2 functions allow only unstructured types to be returned.
restrict the type that can be returned by their functions.
C allows any type to be returned by its functions except arrays and functions.
C++ also allows any type to be returned by functions including class.
Co-routines
A co-routine is a subprogram that has multiple entries and controls them itself
Also called symmetric control: caller and called co-routines are on a more equal basis
A co-routine call is named a resume
The first resume of a co-routine is to its beginning, but subsequent calls enter at the point just after the
Co-routines provide quasi-concurrent execution of program units (the co-routines); their execution is
interleaved, but not overlapped
A B
Resume
Resume B Resume A
From master
Resume B Resume A
Resume B Resume A
From master
First resume
Resume B
Resume A
subsquent
resume