ch9 parameter passing l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Ch9: Parameter Passing PowerPoint Presentation
Download Presentation
Ch9: Parameter Passing

Loading in 2 Seconds...

play fullscreen
1 / 44

Ch9: Parameter Passing - PowerPoint PPT Presentation


  • 181 Views
  • Uploaded on

Ch9: Parameter Passing. Outline. Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques. Characteristics of Subprogram. Each program has a single entry point

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Ch9: Parameter Passing' - abby


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
outline
Outline
  • Subprograms
  • Parameter Passing
    • Parameter correspondence
    • Main Issues when designing subroutine in programming languages
    • Parameter passing techniques
characteristics of subprogram
Characteristics of Subprogram
  • Each program has a single entry point
  • The calling program unit is suspended during execution of the called subprogram, which implies that there is only on subprogram in execution
  • Control always return to the caller when subprogram execution terminates
  • The two fundamental types of subprograms are:
    • procedure
    • functions
basic definitions
Basic Definitions
  • Structure or definition of a subprogram construct is:

special-word name (list of parameter)

Fortran: subroutine Addr (parameters)

Ada: procedure Addr (parameters)

C: void Addr (parameters)

parameters
Parameters
  • Formal parameters -> parts of the specification
  • Actual parameters

ex) formal parameter

void swap (first, second) {

int temp;

temp = first;

first = second;

second = temp;

} end swap

----------

---------- actual parameter

swap (x, y);

parameters6
Parameters
  • The parameters in the subprogram header are called formal parameters.
  • Parameters include as arguments when the subroutine is called are named actual parameters

note: a subroutine that return a value is called a function

  • A subroutine that does not return a value is usually called a procedure or method
  • Most programming languages require subroutine to be declared before they are used (forward)
parameter correspondence
Parameter Correspondence

Establishing the correspondence between actual

parameters and formal parameters

1. Positional Correspondence

The correspondence is established by pairing actual and formal parameters based on the positions of the actual and formal parameter list.

parameter correspondence8
Parameter Correspondence

2. Correspondence by explicit name

In Ada the formal parameter is paired with each actual parameter they may be named explicitly in the calling statement.

example:

sub (y => B, x=> 32);

formalactual

parameter correspondence9
Parameter Correspondence

3. Mixed keyword (Explicit name) and positional

  • Most languages that support keyword parameters allow both: Ada, Fortran, Dylan, Python
  • The first parameter in a list can be positional, and the remainder can be keyword parameters.

ex)

my_sub (Length, sub=>B, List =>my_array);

parameter correspondence10
Parameter Correspondence

4. Optimal parameters

  • Formal parameter list includes default values to be used if the corresponding actual parameter is missing.
  • This gives a very short way of writing certain kinds of overloaded function definitions
example in c
Example in C++

int f(int a =1, int b=2, int c=3) {body}

int f() { f(1, 2, 3); }

int f(int a) { f(a, 2, 3); }

int f(int a, int b) { f(a, b, 3); }

int f(int a, int b, int c) { f(a, b, c); }

unlimited parameter lists
Unlimited Parameter Lists
  • Some languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl
  • Library routines must be used to access the excess actual parameters
  • A hole in static type systems, since the types of the excess parameters cannot be checked at compile time

int printf (char *format, .....) { body }

example in ada
Example in Ada

function Compute_Pay (Income : Float;

Exemptions : Integer := 1;

Tax_Rate : Float) return Float;

Pay := Compute_Pay (2000.0, Tax_Rate => 0.15);

main issues when designing subroutine in programming languages
Main Issues when designing subroutine in programming languages
  • What parameter passing methods are used?
  • Are the types of the actual parameters checked against the types of the formal parameters?
  • Check for error condition
  • Are local variables statically or dynamically allocated?
  • Can subroutines definitions appear on other subroutines definitions?
main issues when designing subroutine in programming languages cont
Main Issues when designing subroutine in programming languages (cont)
  • Can subroutines be passed as parameters?
  • Can subroutines be nested?
  • If passed as parameters and/or nested, what is the referencing environment of a passed parameter?
  • Can a subroutine be generic?
parameter passing technique 1
Parameter Passing Technique 1

Pass-by-Value

  • The value of the actual parameter is associated with the formal parameter
  • It is initialized using the value of the corresponding actual parameter, before the called method begins executing
  • Simplest method
  • Widely used
  • The only method in real Java
  • Changing to a formal parameter do not affect the actual parameter
changes visible to the caller
Changes Visible to the Caller
  • When parameters are passed by value, changes to a formal do not affect the actual
  • But it is still possible for the called method to make changes that are visible to the caller
  • The value of the paramter could be a pointer( in Java, a reference)
  • Then the actual cannot be changed, but the object referred to by the actual can be
example for pass by value 1
int plus(int a, int b){

a += b;

return a;

}

void f(){

int x = 3;

int y = 4;

int z = plus(x, y);

}

Example for Pass by Value(1)

When plus

Is starting

example for pass by value 2
Example for Pass by Value(2)

name formal parameter type result

function square(x : integer) : integer

begin

square := x * x

end

square(2) => 4

example for pass by value 3
Example for Pass by Value(3)

procedure swap(a, b);

integer a, b, temp;

begin temp

temp := a;

a := b;

b := temp;

end;

y

swap(x, y); x effect

parameter passing technique 2
Parameter Passing Technique 2

Pass-by-Result

  • The formal parameter is just like a local variable in the activation record of the called procedure( it is uninitialized)
  • After the called method finishes execution, the final value of the formal parameter is assigned to the corresponding actual parameter
  • It is also called copy-out
  • It was introduced in ALGOL 68
  • Sometimes used in Ada
example for pass by result
Example for Pass by Result

void plus (int a, int b, by-result int c){

c = a + b;

}

void f(){

int x = 3;

int y = 4;

int z;

plus(x, y, z);

}

1

When plus

is starting

example cont
Example cont.

When plus

has returned

when plus is

ready to return

3

2

parameter passing technique 3
Parameter Passing Technique 3

Pass-by-Value Result

  • The formal parameter is just like a local variable in the activation record of the called procedure
  • After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter
  • It is also called copy-in/copy-out
  • It is initialized using the value of the corresponding actual parameter, before the called method begins execution
example for pass by value result
Example for Pass by Value Result

void plus(int a, by-value-result int b){

b += a;

}

void f(){

int x = 3;

plus(4, x);

}

When plus

is starting

1

example cont26
Example cont.

When plus is

ready to return

When plus

has returned

2

3

parameter passing technique 4
Parameter Passing Technique 4

Pass-by-Reference

  • The address of the actual parameter is passed to the called method. This address is used as the formal parameter.
  • One of the earliest methods: Fortran
  • Most efficient for large object
  • Still frequently used
picture
Picture

reference

value

copy-out

copy-in

reference

value

reference

value

reference

example for pass by reference 1
Example for Pass by Reference (1)

void plus (int a, by-reference int b) {

b += a;

}

void f() {

int x = 3;

plus(4, x);

}

When plus

Is starting

1

1

When plus is

Ready to return

2

implementing reference
Implementing Reference

void plus( int a, by-reference int b) {

b += a;

} previous example

void f() {

int x = 3;

plus(4, x);

}

void plus (int a, int *b) {

*b += a;

}

void f() { C implementation

int x = 3;

plus(4, &x); By-reference = address by value

}

aliasing
Aliasing
  • When two expressions have the same lvalue, they are aliases of each other
  • There are obvious cases:

ConsCell x = new ConsCell (0, null);

ConsCell y = x;

A[i] = A[j] + A[k];

  • Passing by reference leads to less obvious cases...
example for pass by reference 2
Example for Pass by Reference (2)

void sigsum( by-reference int n, by-reference int ans) {

ans = 0;

int I = I;

while ( i <= n) ans +=i ++;

}

Int f() { int g() {

int x, y; int x;

x = 10; x = 10;

sigsum (x, y); sigsum (x, x);

return y; return x;

} }

example cont33
Example cont

void sigsum(by-reference int n, by-reference int ans) {

ans = 0;

int i = 1;

while (i <= n) ans += i++;

}

int g() {

int x;

x = 10;

sigsum(x, x);

return x;

}

When sigsum

Is starting

CAR

X : 10

n :

RA

ans :

PAR

i : ?

Result : ?

RA

PAR

parameter passing technique 5
Parameter Passing Technique 5

Pass-by-Name

  • Each actual parameter is evaluated in the caller’s context, on every use of the corresponding formal parameter
  • Introduce in ALGOL 60
  • Now unpopular
  • The actual parameters is treated like a “Little anonymous function” (Thunk)
  • Whenever the called method needs the value of the formal, it calls the Thunk to get it.
example for pass by name 1
Example for Pass by Name(1)

procedure swap(a, b);

integer a, b, temp;

begin

temp := a;

a := b;

b := temp;

end

If actual is a scalar variable, it is passed by reference.

If actual is a constant expression, it is passed by value.

0 1 2 3 4

. . . .

A

A[3] = 10

I = 2

example 1 cont
Example 1 cont.

swap(I, A[3]){

temp := 2

I := A[3] = 10

A[10] := temp

}

swap(I, A[I]){

temp := 2

I := A[2] = 10

A[10] := temp

}

example for pass by name 2
Example for Pass by Name(2)

voidf( by-name int a, by-name int b){

b = 5;

b = a;

}

int g(){

int i = 3;

f(i + 1, i );

return;

}

Thunk

Thunk

i + 1

i

when

f() is starting

When f executes b = 5, the thunk returns i address and the value 5 is stored

In variable “ i ”. Then, when f executes b = a the thunk evaluates “5 + 1” and

stores the new value in variable “ i ”. The final result i = 6.

jensen s device
Jensen’s Device

Real procedure sum(j, lo, hi, Exp)

Value lo, hi;

Real Exp;

Integers j, lo, hi;

Begin

Real RTN;

RTN := 0;

FOR I = lo step 1 UNTIL hi do

RTN := RTN + Exp;

SUM : = RTM

End

x[ i ] * i

jensen s device example
Jensen’s Device Example

10

2

y =

3x - 5x + 2

i = 1

y = sum(x, I, 10, 3 * x * x - 5 * x + 2)

Each time through the loop, evaluation of the expression

is actually the evaluation of

2

3x - 5x + 2

Procedure swap(a, b);

Integer a, b, temp;

Begin

temp := a;

a := b;

b := temp;

End

jensen s device example cont
Jensen’s Device Example cont.

i = 2

x[i] = 5

Effect of calling swap(i, x[i])

Before the call: i = 2, x[2] = 5

What do we expect: i = 5, x[2] = 2

After the call: i = 5, x[2] = 5, x[5] = 2

side effect

temp 2

i

A[i] = 2

A[i] = A[5] = 2

thunks
Thunks

Function XjThunk() : Real address

VAR Expi : Real

Begin

Expi := x[i] * i

XjThunk := address(Expi)

End

specification issues
Specification Issues
  • Are these just implementation techniques, or part of the language specification?
  • Depends on the language:
    • Without side-effects, parameter-passing technique may be undetectable by the programmer
    • Even with side effects, some languages specify the parameter passing technique only partially
without side effects
Without Side Effects
  • Big question are parameters always evaluated, or only if they are really needed?
  • Cost model may also be used by the programmer
    • Is re-evaluation of a formal expensive?
    • Does parameter-passing take time proportional to the size of the object?
with side effects
With Side Effects
  • A program can detect which parameter-passing technique is being used by the language system
  • But it may be an implementation detail that programs are not supposed to depend on: it may not be part of the specification of the language
  • Case in point: Ada