CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

23
1 CS 201 Computer Systems Programming Chapter 18 “Parameter Passing” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 10/9/2012 Status 10/9/2012

description

Herbert G. Mayer, PSU CS Status 10/9/2012. CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”. Syllabus. Summary Value Parameter Reference Parameter Value-Result Parameter Name Parameter In-Out Parameter References. Summary. - PowerPoint PPT Presentation

Transcript of CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

Page 1: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

1

CS 201Computer Systems Programming

Chapter 18“Parameter Passing”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 10/9/2012Status 10/9/2012

Page 2: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

2

Syllabus SummarySummary

Value ParameterValue Parameter

Reference ParameterReference Parameter

Value-Result ParameterValue-Result Parameter

Name ParameterName Parameter

In-Out ParameterIn-Out Parameter

ReferencesReferences

Page 3: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

3

Summary Parameter passing is programming language tool to Parameter passing is programming language tool to

bind information of caller to calléebind information of caller to callée

The parameter on the calling side is the actual The parameter on the calling side is the actual parameter (AP); may be an expression or named parameter (AP); may be an expression or named object (variable)object (variable)

The parameter on the called side (callée) is the The parameter on the called side (callée) is the formal parameter (FP); it is always namedformal parameter (FP); it is always named

The actual is bound to the formal at place of callThe actual is bound to the formal at place of call

Binding lasts for the duration of the callBinding lasts for the duration of the call

Association actual-to-formal is generally by positionAssociation actual-to-formal is generally by position

May also be by naming the formal at the place of May also be by naming the formal at the place of call, e.g. in Ada. In that case the order may be call, e.g. in Ada. In that case the order may be arbitrarily intermixed at place of callarbitrarily intermixed at place of call

Page 4: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

4

Summary Types of actual and formal generally must be Types of actual and formal generally must be

compatible, subject to the language’s type compatible, subject to the language’s type compatibility- or type conversion rulescompatibility- or type conversion rules

C/C++ allow a lesser number of actual parameters to C/C++ allow a lesser number of actual parameters to be passed than formally declaredbe passed than formally declared

Typical implementation of C/C++ therefore must pass Typical implementation of C/C++ therefore must pass actuals in reverse textual orderactuals in reverse textual order

TheoreticallyTheoretically, APs are passed on the run-time stack, APs are passed on the run-time stack PhysicallyPhysically, good optimizers pass APs in registers, good optimizers pass APs in registers PracticallyPractically, compiler passes the first few parameters , compiler passes the first few parameters

in registers and remaining ones on the stackin registers and remaining ones on the stack FPs are generally located on one side off the stack FPs are generally located on one side off the stack

marker, while locals are positioned on the opposite marker, while locals are positioned on the opposite side of the stack marker, the one requiring negative, side of the stack marker, the one requiring negative, the other positive offsets from a base pointer register the other positive offsets from a base pointer register

Page 5: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

5

Summary Value Parameter Value Parameter (VP): Actual parameter (AP) (VP): Actual parameter (AP)

provides initial value via a type-compatible provides initial value via a type-compatible expression. If modifications to bound formal expression. If modifications to bound formal parameter (FP) occur, they do not impact the APparameter (FP) occur, they do not impact the AP

Reference ParameterReference Parameter (RP): AP must be an (RP): AP must be an addressable object. If modifications to bound FP addressable object. If modifications to bound FP occur, they do impact the AP. Implemented by occur, they do impact the AP. Implemented by passing the address of the AP which is a variable passing the address of the AP which is a variable

Value-Result Parameter Value-Result Parameter (VRP): AP must be an (VRP): AP must be an addressable object. If modifications to bound FP addressable object. If modifications to bound FP occur, they do impact the AP, but such assignments occur, they do impact the AP, but such assignments to AP occur at the place of return. Implemented via to AP occur at the place of return. Implemented via copy-in copy-outcopy-in copy-out

More detail and other kinds of formal parameters belowMore detail and other kinds of formal parameters below

Page 6: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

6

Value Parameter (VP) The actual parameter (AP) to be bound to a formal value The actual parameter (AP) to be bound to a formal value

parameter (VP) must provide an initial value to the parameter (VP) must provide an initial value to the formal parameter (FP)formal parameter (FP)

Initial value is the first value of the FP; that initial actual Initial value is the first value of the FP; that initial actual value may be an expression or objectvalue may be an expression or object

During the call, assignments to the formal VP are During the call, assignments to the formal VP are allowed; allowed; exception: in-parameters exception: in-parameters in Adain Ada

Assignments to the formal VP have no impact on AP Assignments to the formal VP have no impact on AP during the callduring the call

Assignments to the formal VP also have no impact on Assignments to the formal VP also have no impact on the actual after the callthe actual after the call

Implementation: must make a copy of the initial value of Implementation: must make a copy of the initial value of the AP and pass it in register or more generally on the the AP and pass it in register or more generally on the stackstack

At the return, discard the copy, which also voids al At the return, discard the copy, which also voids al changes to the FP from the time during the callchanges to the FP from the time during the call

Page 7: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

7

Value Parameter in C C requires parameter passing C requires parameter passing by valueby value Except for parameters of type array[]Except for parameters of type array[]

Arrays in C and C++ are passed by reference; note this logical description; physically this is implemented by passing the address of the first element of actual array; in C element [0]

Don’t confuse this with pointer types! See below! Pointer type parameters are also passed by value; of Pointer type parameters are also passed by value; of

course the object pointed to may very well change course the object pointed to may very well change during call, but that still keeps the pointer intactduring call, but that still keeps the pointer intact

C structs are passed by value, requiring a copy of the C structs are passed by value, requiring a copy of the actual struct; may be a large amount of data spaceactual struct; may be a large amount of data space

How can a programmer pass an How can a programmer pass an array[] by value in Carray[] by value in C?? Pack array[] as a field into a struct, consumes identical Pack array[] as a field into a struct, consumes identical

amount of space as the original array[]; will all be amount of space as the original array[]; will all be copied at place & moment of callcopied at place & moment of call

Page 8: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

8

Value Parameter in C++ C++ inherits C’s parameter passing methodsC++ inherits C’s parameter passing methods Hence the default parameter passing method in C++ Hence the default parameter passing method in C++

is also by valueis also by value But C++ adds the explicit reference parameter, using But C++ adds the explicit reference parameter, using

the & operator; see laterthe & operator; see later

Page 9: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

9

Value Parameter in C, Sample Set-Up

#define ASSERT( condition, msg, value ) \#define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", msg, value );\printf( "<> error: %s. Instead: %d\n", msg, value );\ } //end if} //end if

typedef struct { int field; } struct_tp;typedef struct { int field; } struct_tp;typedef int * int_ptr;typedef int * int_ptr;

int global1 = 109;int global1 = 109;int global2 = 2012;int global2 = 2012;

Page 10: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

10

Value int Parameter in C// assign to formal has no impact on actual// assign to formal has no impact on actualvoid change_int_val( int value )void change_int_val( int value ){ // change_int_val{ // change_int_val ASSERT( value == 109,ASSERT( value == 109, "in change_int_val should be 109”, value );"in change_int_val should be 109”, value ); value++;value++; // change formal// change formal ASSERT( value == 110,ASSERT( value == 110, "in change_int_val should be 110”, value );"in change_int_val should be 110”, value );} //end change_int_val} //end change_int_val

void int_val( void )void int_val( void ){ // int_val{ // int_val int local1 = 109;int local1 = 109; change_int_val( local1 );change_int_val( local1 ); ASSERT( ( local1 == 109 ),ASSERT( ( local1 == 109 ), "int should be 109", local1 );"int should be 109", local1 );} //end int_val} //end int_val

Page 11: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

11

Value struct Parameter in C// assign to formal value struct param no impact on actual// assign to formal value struct param no impact on actualvoid change_struct_val( struct_tp value )void change_struct_val( struct_tp value ){ // change_struct_val{ // change_struct_val ASSERT( value.field == 109,ASSERT( value.field == 109, "in change_struct_vale should be 109”,"in change_struct_vale should be 109”, value.field );value.field ); value.field++; // change formalvalue.field++; // change formal ASSERT( value.field == 110,ASSERT( value.field == 110, "in change)struct_val should be 110”,"in change)struct_val should be 110”, value.field );value.field );} //end change_struct_val} //end change_struct_val

void struct_val( void )void struct_val( void ){ // struct_val{ // struct_val struct_tp local1;struct_tp local1; local1.field = 109;local1.field = 109; change_struct_val( local1 );change_struct_val( local1 ); ASSERT( ( local1.field == 109 ),ASSERT( ( local1.field == 109 ), "struct field should be 109", local1.field );"struct field should be 109", local1.field );} //end struct_val} //end struct_val

Page 12: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

12

Value pointer Parameter in C// assign to formal ptr value param has no impact on actual// assign to formal ptr value param has no impact on actualvoid change_ptr_val( int_ptr value )void change_ptr_val( int_ptr value ){ // change_ptr_val{ // change_ptr_val ASSERT( *value == 109,ASSERT( *value == 109, "in change_ptr_val should be 109", *value );"in change_ptr_val should be 109", *value ); value = & global2; // formal parameter change!value = & global2; // formal parameter change! ASSERT( *value == 2012,ASSERT( *value == 2012, "pointed-to should be 2012", *value );"pointed-to should be 2012", *value );} //end change_ptr_val} //end change_ptr_val

void ptr_val( void )void ptr_val( void ){ // ptr_val{ // ptr_val int_ptr local1 = & global1;int_ptr local1 = & global1; ASSERT( local1 == & global1,ASSERT( local1 == & global1, "before: should be & global1", & global1 ); "before: should be & global1", & global1 ); change_ptr_val( local1 ); change_ptr_val( local1 ); ASSERT( local1 == & global1,ASSERT( local1 == & global1, "Wrong pointer; points to:", *local1 );"Wrong pointer; points to:", *local1 ); ASSERT( *local1 == 109,ASSERT( *local1 == 109, "pointed-to value should be 109", *local1 );"pointed-to value should be 109", *local1 );} //end ptr_val} //end ptr_val

Page 13: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

13

Reference Parameter (RP) The AP for RP passing must be a named variableThe AP for RP passing must be a named variable

The AP does not have to be initialized, in which case The AP does not have to be initialized, in which case callée better abstain from referencing before callée better abstain from referencing before assigningassigning

Goal is for the callée to compute and then return a Goal is for the callée to compute and then return a new value in the APnew value in the AP

Assignments to the formal reference parameter (RP) Assignments to the formal reference parameter (RP) have an immediate impact on the AP, since FP and have an immediate impact on the AP, since FP and AP are aliased to one another in RP passingAP are aliased to one another in RP passing

Note that aliasing may be a source of hard to track Note that aliasing may be a source of hard to track errorserrors

Implementations generally pass RP by passing the Implementations generally pass RP by passing the address of the AP; hence it is clear that the AP must address of the AP; hence it is clear that the AP must be an addressable objectbe an addressable object

Page 14: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

14

Reference Parameter in C++, Set-Up#include <iostream>#include <iostream>

#define ASSERT( condition, msg, value ) \#define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", \printf( "<> error: %s. Instead: %d\n", \ msg, ( value ) ); \msg, ( value ) ); \ } }

#define MAX 10#define MAX 10

typedef struct {typedef struct { int field;int field;} struct_tp;} struct_tp;

typedef int arr_tp[ MAX ];typedef int arr_tp[ MAX ];typedef int * int_ptr;typedef int * int_ptr;

// some globally visible variables// some globally visible variablesint global1 = 109;int global1 = 109;int global2 = 2012;int global2 = 2012;

Page 15: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

15

Reference int Parameter in C++// assignment to formal impacts actual// assignment to formal impacts actualvoid change_int_ref( int & value )void change_int_ref( int & value ){ // change_int_ref{ // change_int_ref ASSERT( value == 109,ASSERT( value == 109, "in change_int_ref should be 109”,"in change_int_ref should be 109”, value );value ); value++; // change formalvalue++; // change formal ASSERT( value == 110,ASSERT( value == 110, "in change_int_ref should be 110”,"in change_int_ref should be 110”, value );value );} //end change_int_ref} //end change_int_ref

void int_ref( void )void int_ref( void ){ // int_ref{ // int_ref int local1 = 109;int local1 = 109; change_int_ref( local1 );change_int_ref( local1 ); ASSERT( ( local1 == 110 ),ASSERT( ( local1 == 110 ), "int should be 110", local1 );"int should be 110", local1 );} //end int_ref} //end int_ref

Page 16: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

16

Reference struct Parameter in C++// assign to formal "ref struct param" impacts actual// assign to formal "ref struct param" impacts actualvoid change_struct_ref( struct_tp void change_struct_ref( struct_tp && value ) value ){ // change_struct_ref{ // change_struct_ref ASSERT( value.field == 109,ASSERT( value.field == 109, "in change_struct_ref should be 109”,"in change_struct_ref should be 109”, value.field );value.field ); value.field++; // change formalvalue.field++; // change formal ASSERT( value.field == 110,ASSERT( value.field == 110, "in change_struct_ref should be 110”,"in change_struct_ref should be 110”, value.field );value.field );} //end change_struct_ref} //end change_struct_ref

void struct_ref( void )void struct_ref( void ){ // struct_ref{ // struct_ref struct_tp local1;struct_tp local1; local1.field = 109;local1.field = 109; change_struct_ref( local1 );change_struct_ref( local1 ); ASSERT( ( local1.field == 110 ),ASSERT( ( local1.field == 110 ), "struct field should be 110", local1.field );"struct field should be 110", local1.field );} //end struct_ref} //end struct_ref} //end struct_ref} //end struct_ref

Page 17: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

17

Reference pointer Parameter in C++// assign to formal "ref ptr param" impacts actual// assign to formal "ref ptr param" impacts actualvoid change_ptr_ref( int_ptr void change_ptr_ref( int_ptr && value ) value ){ // change_ptr_ref{ // change_ptr_ref ASSERT( *value == 109,ASSERT( *value == 109, "pointer-to should be 109", *value );"pointer-to should be 109", *value ); value = & global2; // formal value changedvalue = & global2; // formal value changed ASSERT( *value == 2012,ASSERT( *value == 2012, "pointed-to should be 2012", *value );"pointed-to should be 2012", *value );} //end change_ptr_ref} //end change_ptr_ref

void ptr_ref( void )void ptr_ref( void ){ // ptr_ref{ // ptr_ref int_ptr local1 = & global1;int_ptr local1 = & global1; ASSERT( *local1 == 109,ASSERT( *local1 == 109, "pointer-to should be 109 in ptr_ref", *local1 );"pointer-to should be 109 in ptr_ref", *local1 ); change_ptr_ref( local1 );change_ptr_ref( local1 ); ASSERT( local1 == & global2,ASSERT( local1 == & global2, "Wrong pointer; points to:", *local1 );"Wrong pointer; points to:", *local1 ); ASSERT( *local1 == 2012,ASSERT( *local1 == 2012, "pointed-to value should be 2012", *local1 );"pointed-to value should be 2012", *local1 );} //end ptr_ref} //end ptr_ref

Page 18: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

18

Reference array[] Parameter in C++// assign to formal "array param" impacts actual// assign to formal "array param" impacts actualvoid change_arr_ref( arr_tp value ) // void change_arr_ref( arr_tp value ) // reference default!!reference default!!{ // change_arr_ref{ // change_arr_ref ASSERT( value[ 5 ] == 109,ASSERT( value[ 5 ] == 109, "value[ 5 ] should be 109", value[ 5 ] );"value[ 5 ] should be 109", value[ 5 ] ); value[ 5 ]++; // formal value changedvalue[ 5 ]++; // formal value changed ASSERT( value[ 5 ] == 110,ASSERT( value[ 5 ] == 110, "value[5] should be 110 in change()”, value[5] );"value[5] should be 110 in change()”, value[5] );} //end change_arr_ref} //end change_arr_ref

void arr_ref( void )void arr_ref( void ){ // arr_ref{ // arr_ref arr_tp value;arr_tp value; for ( int i=0; i < MAX; i++ ) { value[i] = 109; }for ( int i=0; i < MAX; i++ ) { value[i] = 109; } ASSERT( value[ 5 ] == 109,ASSERT( value[ 5 ] == 109, "value[5] should be 109 in arr_ref", value[5] );"value[5] should be 109 in arr_ref", value[5] ); change_arr_ref( value );change_arr_ref( value ); ASSERT( ( value[ 5 ] == 110 ),ASSERT( ( value[ 5 ] == 110 ), "value[ 5 ] should be 110", value[ 5 ] );"value[ 5 ] should be 110", value[ 5 ] );} //end arr_ref} //end arr_ref

Page 19: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

19

Value-Result Parameter AP must be addressable object, to be bound to FPAP must be addressable object, to be bound to FP

Assignments to FP are allowed in calléeAssignments to FP are allowed in callée

During the call, the AP is unchanged; note that this During the call, the AP is unchanged; note that this is quite different from RP passing!is quite different from RP passing!

At moment of return –and there may be many places At moment of return –and there may be many places in the code– the last value assigned to the FP is in the code– the last value assigned to the FP is copied back into the APcopied back into the AP

Not discussed further in CS 201Not discussed further in CS 201

Should be covered in Fortran programming class, or Should be covered in Fortran programming class, or compiler classcompiler class

Page 20: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

20

Name Parameter Is the unusual, quirky, default parameter passing Is the unusual, quirky, default parameter passing

method in Algo60method in Algo60

The text of the AP substitutes the corresponding FP The text of the AP substitutes the corresponding FP for any particular callfor any particular call

Highly complicated to understand and implement, Highly complicated to understand and implement, first done by Ingerman, using “thunks”first done by Ingerman, using “thunks”

Not covered in CS 201; belongs into Compiler classNot covered in CS 201; belongs into Compiler class

Page 21: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

21

Name ParameterComment: Algol60 sample; definition of confuse()Comment: Algol60 sample; definition of confuse()procedure confuse( a, b )procedure confuse( a, b ) integer a, binteger a, b beginbegin b := 4;b := 4; a := 5;a := 5; end;end;

confuse( x[ i ], i ); comment: i = 33, x[33] = 10confuse( x[ i ], i ); comment: i = 33, x[33] = 10

Comment: as if confuse() had been coded like this:Comment: as if confuse() had been coded like this:procedure confuse( x[i], i )procedure confuse( x[i], i ) integer x[ i ], iinteger x[ i ], i beginbegin i := 4;i := 4; x[4] := 5;x[4] := 5; end;end;

Comment: x[33] is unaffected, i = 4, and x[4] = 5Comment: x[33] is unaffected, i = 4, and x[4] = 5

Page 22: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

22

In-Out Parameter Is one of the parameter passing methods used in Is one of the parameter passing methods used in

Ada; not identical to VRPAda; not identical to VRP

Is a combination of the in-parameter and out-Is a combination of the in-parameter and out-parameter passing methodsparameter passing methods

Similar to value-result parameter passing, but it is Similar to value-result parameter passing, but it is left unspecified at which moment the value of the FP left unspecified at which moment the value of the FP is copied back into the APis copied back into the AP

Will not be discussed in CS 201 Will not be discussed in CS 201

Page 23: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

23

References

1. Revised Algol60 report: http://www.masswerk.at/algol60/report.htm

2. ADA reference manual: http://www.adaic.org/resources/add_content/standards/05rm/html/RM-TTL.html

3. Ada report: http://www.sigada.org/ada_95/what_is_ada.html

4. Fortran standard: http://www.j3-fortran.org