CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ” Plus

30
1 CS 201 Computer Systems Programming Chapter 18 “Parameter Passing” Plus Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 7/8/2014 Status 7/8/2014 Corrections contributed by George Nicol Corrections contributed by George Nicol

description

Herbert G. Mayer, PSU CS Status 7/8/2014 Corrections contributed by George Nicol. CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ” Plus. Syllabus. Summary Aliasing Parameter Overview Value Parameter Reference Parameter Value-Result Parameter Name Parameter - PowerPoint PPT Presentation

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

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

1

CS 201Computer Systems Programming

Chapter 18“Parameter Passing” Plus

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/8/2014Status 7/8/2014

Corrections contributed by George Nicol Corrections contributed by George Nicol

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

2

Syllabus

SummarySummary

AliasingAliasing

Parameter OverviewParameter Overview

Value ParameterValue Parameter

Reference ParameterReference Parameter

Value-Result ParameterValue-Result Parameter

Name ParameterName Parameter

In-Out ParameterIn-Out Parameter

ReferencesReferences

The Plus: Operator PrecedencesThe Plus: Operator Precedences

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

3

Summary Passing parameters is Passing parameters is thethe key programming key programming

language tool for language tool for binding caller to calléebinding caller to callée

The parameter on the calling side is the actual The parameter on the calling side is the actual parameter (AP); the actual may be an expression or parameter (AP); the actual may be an expression or a named object (variable)a named 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 the place of callThe actual is bound to the formal at the place of call

BindingBinding lasts for the duration of the call lasts for the duration of the call

Association actual-to-formal is generally by Association actual-to-formal is generally by positionposition

Association may also be by Association may also be by namingnaming the formal at the the formal at the place of call, e.g. in Ada. In that case the order may place of call, e.g. in Ada. In that case the order may be arbitrarily permuted, adding great possibilities for be arbitrarily permuted, adding great possibilities for obfuscationobfuscation

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

4

Summary Types and numbers of actual and formal parameter (Types and numbers of actual and formal parameter (FPFP) )

generally must be compatible, subject to type generally must be compatible, subject to type compatibility- or type conversion rulescompatibility- or type conversion rules

C allows a lesser number of actual parameters (C allows a lesser number of actual parameters (APAP) to be ) to be passed than formally declaredpassed than formally declared

Implementations of C therefore pass actuals in reverse Implementations of C therefore pass actuals in reverse textual order; see detail in Compiler class CA 321/322 or textual order; see detail in Compiler class CA 321/322 or similarsimilar

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 in , compiler passes the first few parameters in

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

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

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

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. A modification to the bound formal expression. A modification to the bound formal parameter (FP) does not impact the APparameter (FP) does not impact the AP

Reference ParameterReference Parameter (RP): AP must be an (RP): AP must be an addressable object! A modification to the bound FP addressable object! A modification to the bound FP immediatelyimmediately impacts the AP. Implemented by impacts 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. A modification to the bound FP addressable object. A modification to the bound FP impacts the AP, but at the moment and impacts the AP, but at the moment and place of place of returnreturn. Implemented via copy-in copy-out!. Implemented via copy-in copy-out!

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

6

Aliasing Making one program object Making one program object o1o1 accessible via a accessible via a

second name second name o2o2 is known as is known as aliasingaliasing We say: object We say: object o2o2 is an alias of object is an alias of object o1o1, if both , if both

designate the same memory addressdesignate the same memory address The aliasing relation is reflexive: if The aliasing relation is reflexive: if o1o1 is and alias of is and alias of

o2o2, then , then o2o2 is also an alias of is also an alias of o1o1 When When o1o1 and and o2o2 are aliases, and are aliases, and o1o1 is changed, then is changed, then

magicallymagically o2o2 also changes. This magic can be a also changes. This magic can be a surprisesurprise

Surprises in SW are generally a sign of Surprises in SW are generally a sign of poor SWEpoor SWE Aliasing is Aliasing is not always not always a sign of poor programming!a sign of poor programming! Software using aliasing is generally harder to Software using aliasing is generally harder to

comprehend, thus tends to be more error pronecomprehend, thus tends to be more error prone

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

7

Aliasing Alias relations are typically established via reference Alias relations are typically established via reference

parametersparameters We won’t argue here whether pointers cause We won’t argue here whether pointers cause

aliasing as well; they surely allow programmers to aliasing as well; they surely allow programmers to access objects differently than via their nameaccess objects differently than via their name

Example of aliasing, assuming Example of aliasing, assuming ASSERT()ASSERT()::

int glob_i = 109; // use C++ as exception!int glob_i = 109; // use C++ as exception!. . .. . .void foo1( int void foo1( int && loc_i ) loc_i ){ // foo1{ // foo1 ASSERT( 109 == glob_i, "wrong value for glob_i" );ASSERT( 109 == glob_i, "wrong value for glob_i" ); ASSERT( 109 == loc_i, "wrong parameter passing" );ASSERT( 109 == loc_i, "wrong parameter passing" ); loc_i++;loc_i++; // what will be changed? // what will be changed? ASSERT( 110 == glob_i, "aliasing failed." );ASSERT( 110 == glob_i, "aliasing failed." );} //end foo1} //end foo1. . .. . .int main( void )int main( void ){ // main{ // main foo1( glob_i );foo1( glob_i ); // the only call to foo1() // the only call to foo1()} //end main} //end main

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

8

Aliasing Pointers add further aliasing methods, even without Pointers add further aliasing methods, even without

parameterizing!parameterizing! Yet each time we change an object pointed to, we Yet each time we change an object pointed to, we

know: know: this may change memory anywherethis may change memory anywhere Explanation see last slides! Thanks George!Explanation see last slides! Thanks George!

int glob_i = 110;int glob_i = 110;. . .. . .void foo2( void )void foo2( void ){ // foo2{ // foo2 int * p_i = & glob_i;int * p_i = & glob_i; // set local pointer to glob_i // set local pointer to glob_i

ASSERT( 110 == glob_i, "error, glob_i unexpected" );ASSERT( 110 == glob_i, "error, glob_i unexpected" ); ASSERT( 110 == *p_i, "error, * to wrong 1. object" );ASSERT( 110 == *p_i, "error, * to wrong 1. object" ); (*p_i)++; // *p_i is alias of glob_i(*p_i)++; // *p_i is alias of glob_i ASSERT( 111 == glob_i, "error in * assignment" );ASSERT( 111 == glob_i, "error in * assignment" ); ASSERT( 111 == *p_i, "error, * to wrong 2. object" );ASSERT( 111 == *p_i, "error, * to wrong 2. object" ); ++(*p_i); // another way to change++(*p_i); // another way to change ASSERT( 112 == glob_i, "error in pre-inc. via *" );ASSERT( 112 == glob_i, "error in pre-inc. via *" ); *p_i++; // *p_i++; // this fails the ASSERTthis fails the ASSERT ASSERT( 113 == glob_i, ”precedence + associativity!" );ASSERT( 113 == glob_i, ”precedence + associativity!" );} //end foo2} //end foo2

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

9

Parameter Overview

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

10

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

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

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

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 or after the callduring or after the call

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

At place of return, discard the copy, which At place of return, discard the copy, which immediately voids changes to the FP during the callimmediately voids changes to the FP during the call

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

11

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

Arrays in C are passed by reference; generally implemented by passing the address of the actual array; in C that is also the address of element [0]

Not to be confused with pointer types! More below! Pointer type parameters in C are also passed by value; Pointer type parameters in C are also passed by value;

of course the object pointed to may very well change of course the object pointed to may very well change during call, but that still keeps the pointer invariantduring call, but that still keeps the pointer invariant

C structs are passed by valueC structs are passed by value, requiring a copy of the , requiring a copy of the actual struct; that may be a large amount of data spaceactual struct; that 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[] modulo amount of space as the original array[] modulo padding; will all be copied at place and moment of callpadding; will all be copied at place and moment of call

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

12

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

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

type, using the type, using the && operator for formal reference operator for formal reference parametersparameters

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

13

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; } typedef struct { int field; } struct_tpstruct_tp;;typedef int * typedef int * int_ptr_tpint_ptr_tp;;

int global1 = 109;int global1 = 109;int global2 = 2013;int global2 = 2013;

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

14

Value int Parameter in C// assignment to formal VP has no impact on AP// assignment to formal VP has no impact on APvoid change_int_val( int value )void change_int_val( int value ){ // change_int_val{ // change_int_val ASSERT( 109 == value,ASSERT( 109 == value, "in change_int_val should be 109”, value );"in change_int_val should be 109”, value ); value++; // change formal parameter (FP)value++; // change formal parameter (FP) ASSERT( 110 == value,ASSERT( 110 == value, "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 ) // // <- start here!!<- start here!!{ // int_val{ // int_val int local1 = 109;int local1 = 109; change_int_val( local1 );change_int_val( local1 ); ASSERT( ( 109 == local1 ), “should be 109", local1 );ASSERT( ( 109 == local1 ), “should be 109", local1 );} //end int_val} //end int_val

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

15

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( void change_struct_val( struct_tpstruct_tp value ) value ){ // change_struct_val{ // change_struct_val ASSERT( 109 == value.field,ASSERT( 109 == value.field, "in change_struct_val, expect 109”, value.field );"in change_struct_val, expect 109”, value.field ); value.field++; // change formalvalue.field++; // change formal ASSERT( 110 == value.field,ASSERT( 110 == value.field, "in change_struct_val, expect 110”, value.field );"in change_struct_val, expect 110”, value.field );} //end change_struct_val} //end change_struct_val

void struct_val( void ) void struct_val( void ) // // <- start here!!<- start here!!{ // struct_val{ // struct_val struct_tpstruct_tp local1; local1; local1.field = 109;local1.field = 109; change_struct_val( local1 );change_struct_val( local1 ); ASSERT( ( 109 == local1.field ),ASSERT( ( 109 == local1.field ), "struct field should be 109", local1.field );"struct field should be 109", local1.field );} //end struct_val} //end struct_val

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

16

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( void change_ptr_val( int_ptr_tpint_ptr_tp value ) value ){ // change_ptr_val{ // change_ptr_val ASSERT( ASSERT( 109 == *value109 == *value,, "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 == 2013,ASSERT( *value == 2013, "pointed-to should be 2013", *value );"pointed-to should be 2013", *value );} //end change_ptr_val} //end change_ptr_val

void ptr_val( void ) void ptr_val( void ) // // <- start here!!<- start here!!{ // ptr_val{ // ptr_val int_ptr_tpint_ptr_tp local1 = & global1; // init 109; slide 13 local1 = & global1; // init 109; slide 13 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( 109 == *local1,ASSERT( 109 == *local1, "pointed-to value should be 109", *local1 );"pointed-to value should be 109", *local1 );} //end ptr_val} //end ptr_val

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

17

Reference Parameter (RP) The AP for RP passing must be a named object; The AP for RP passing must be a named object;

cannot be an expressioncannot be an expression

AP AP does not have to be does not have to be initialized (caution!), but in initialized (caution!), but in such cases the callée better abstain from referencing such cases the callée better abstain from referencing uninitialized objects before assigninguninitialized objects before assigning

Goal is for callée to eventually compute and then Goal is for callée to eventually compute and then return a new value in the AP; else RP use senselessreturn a new value in the AP; else RP use senseless

Assignments to the formal RP have an immediate Assignments to the formal RP have an immediate impact on the AP, since impact on the AP, since FP and AP are aliased FP and AP are aliased to one to one anotheranother

Aliasing can be the source of hard to track errors!Aliasing can be the source of hard to track errors!

Implement RP by passing the address of the AP; Implement RP by passing the address of the AP; hence it is clear that the AP must be an addressable hence it is clear that the AP must be an addressable object; other implementations possible, specifically: object; other implementations possible, specifically: passing address in registerpassing address in register

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

18

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

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

#define #define TRACK_NO_ERRORTRACK_NO_ERROR( msg ) \( msg ) \ if ( ! error_count ) { \if ( ! error_count ) { \ cout << ”OK in: " << msg << endl; \cout << ”OK in: " << msg << endl; \ }}#define MAX 10#define MAX 10 // used for some array type defs// used for some array type defs

typedef struct {typedef struct { // create struct with 1 int field// create struct with 1 int field int field;int field; // due to size = 4/8 -> no padding// due to size = 4/8 -> no padding} } struct_tpstruct_tp;;

typedef int typedef int arr_tparr_tp[ MAX ];[ MAX ];typedef int * typedef int * int_ptr_tpint_ptr_tp;;

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

19

Reference Parameter in C++, Set-Up

int error_count; // will be reset to 0 after each testint error_count; // will be reset to 0 after each testint global1; // will be set by init()int global1; // will be set by init()int global2; // also set by init()int global2; // also set by init()

struct_tp global_struct;struct_tp global_struct; // init globally interesting things to defined values// init globally interesting things to defined valuesvoid init()void init(){ // init{ // init global1 = 109;global1 = 109; error_count = 0;error_count = 0; global2 = 2013;global2 = 2013; global_struct.field = 109;global_struct.field = 109;} //end init} //end init

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

20

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

void int_ref( void )void int_ref( void ){ // int_ref{ // int_ref // // <- start here!!<- start here!! global1 = 109;global1 = 109; change_int_ref( global1 );change_int_ref( global1 ); ASSERT( 110 == global1, ”... NOT 110", global1 );ASSERT( 110 == global1, ”... NOT 110", global1 ); TRACK_NO_ERROR( "int_ref()" );TRACK_NO_ERROR( "int_ref()" );} //end int_ref} //end int_ref

... Means: there must be specific, meaningful message... Means: there must be specific, meaningful message

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

21

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

// assume global_struct.field initialized to 109// assume global_struct.field initialized to 109void struct_ref( void ) void struct_ref( void ) // // <- start here!!<- start here!!{ // struct_ref{ // struct_ref change_struct_ref( global_struct );change_struct_ref( global_struct ); ASSERT( ( global_struct.field == 110 ),ASSERT( ( global_struct.field == 110 ), "struct field should be 110","struct field should be 110", global_struct.field );global_struct.field );} //end struct_ref} //end struct_ref

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

22

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

void ptr_ref( void ) void ptr_ref( void ) // // <- start here<- start here{ // ptr_ref{ // ptr_ref int_ptr_tpint_ptr_tp local1 = & global1; local1 = & global1; ASSERT( 109 == *local1,ASSERT( 109 == *local1, "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( 2013 == *local1,ASSERT( 2013 == *local1, "pointed-to value should be 2013", *local1 );"pointed-to value should be 2013", *local1 );} //end ptr_ref} //end ptr_ref

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

23

No Ref array[] Parameters in C++// assign to formal "array param" impacts actual// assign to formal "array param" impacts actualvoid change_arr_ref( void change_arr_ref( arr_tparr_tp value ) // value ) // ref. is default!!ref. is 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 ) // // <- start here<- start here{ // 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( 109 == value[ 5 ],ASSERT( 109 == value[ 5 ], "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( 110 == value[ 5 ],ASSERT( 110 == value[ 5 ], "value[ 5 ] should be 110", value[ 5 ] );"value[ 5 ] should be 110", value[ 5 ] );} //end arr_ref} //end arr_ref

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

24

Value-Result Parameter in Fortran 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 is quite different from RP different from RP passing!passing!

At the moment of return –and there may be many At the moment of return –and there may be many places in the code– the last value assigned to the FP places in the code– the last value assigned to the FP is copied back into the APis copied 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 25: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ” Plus

25

Name Parameter in Algol60 Name parameter passing is the default, quirky Name parameter passing is the default, quirky

parameter passing method in Algol60parameter passing method in Algol60

Hard to implementHard to implement

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

Can have strange side-effectsCan have strange side-effects

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

Also not covered in CS 201; belongs into advanced Also not covered in CS 201; belongs into advanced Compiler classCompiler class

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

26

Name Parameter in Algol60Comment: 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 27: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ” Plus

27

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

Ada; close but Ada; close but not identical to not identical to value-result value-result parameter passingparameter passing

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 28: CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ” Plus

28

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

5. Fast Interprocedural Alias Analysis: Keith D. Cooper, Ken Kennedy, Dept. of CS, Rice University, NSF grant CCR 86-19893

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

29

Operator Precedenceint numint num = 1;= 1;int arr[]int arr[] = { 33, 55, 77 };= { 33, 55, 77 };int * piint * pi = arr; // note C quirk: ( arr )!= arr; // note C quirk: ( arr )!

#define TRACK( msg, val ) printf( "num %d '%s' %6d, pi= %d, \#define TRACK( msg, val ) printf( "num %d '%s' %6d, pi= %d, \arr[0] = %d, arr[1] = %d, arr[2] = %d\n", \arr[0] = %d, arr[1] = %d, arr[2] = %d\n", \num++, msg, val, pi, arr[0], arr[1], arr[2] )num++, msg, val, pi, arr[0], arr[1], arr[2] )

int main( void )int main( void ){ // main{ // main

TRACK( " *pi", *pi ); // deref ^ to arr[0]TRACK( " *pi", *pi ); // deref ^ to arr[0] TRACK( "(*pi)++", (*pi)++ ); // post-inc arr[0], NOT the ptr piTRACK( "(*pi)++", (*pi)++ ); // post-inc arr[0], NOT the ptr pi TRACK( "++(*pi)", ++(*pi) ); // pre-inc arr[0], NOT the ptr piTRACK( "++(*pi)", ++(*pi) ); // pre-inc arr[0], NOT the ptr pi TRACK( " *pi++", *pi++ ); // fetch a[0], post-inc pi to A(arr[1])TRACK( " *pi++", *pi++ ); // fetch a[0], post-inc pi to A(arr[1]) TRACK( "++*pi ", ++*pi ); // pre-inc a[1]TRACK( "++*pi ", ++*pi ); // pre-inc a[1] TRACK( "*++pi ", *++pi ); // pre-inc ptr to a[2], then fetch a[2]TRACK( "*++pi ", *++pi ); // pre-inc ptr to a[2], then fetch a[2] TRACK( " pi++", pi++ ); // print addr, then inc by 4TRACK( " pi++", pi++ ); // print addr, then inc by 4 TRACK( " ++pi", ++pi ); // inc addr to word after arr[2], print!TRACK( " ++pi", ++pi ); // inc addr to word after arr[2], print!

return 0;return 0;} //end main} //end main

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

30

Operator Precedencenum 1 ' *pi' 33, pi= 135336, arr[0] = 33, arr[1] = 55, arr[2] = 77num 1 ' *pi' 33, pi= 135336, arr[0] = 33, arr[1] = 55, arr[2] = 77num 2 '(*pi)++' 33, pi= 135336, arr[0] = 34, arr[1] = 55, arr[2] = 77num 2 '(*pi)++' 33, pi= 135336, arr[0] = 34, arr[1] = 55, arr[2] = 77num 3 '++(*pi)' 35, pi= 135336, arr[0] = 35, arr[1] = 55, arr[2] = 77num 3 '++(*pi)' 35, pi= 135336, arr[0] = 35, arr[1] = 55, arr[2] = 77num 4 ' *pi++' 35, pi= 135340, arr[0] = 35, arr[1] = 55, arr[2] = 77num 4 ' *pi++' 35, pi= 135340, arr[0] = 35, arr[1] = 55, arr[2] = 77num 5 '++*pi ' 56, pi= 135340, arr[0] = 35, arr[1] = 56, arr[2] = 77num 5 '++*pi ' 56, pi= 135340, arr[0] = 35, arr[1] = 56, arr[2] = 77num 6 '*++pi ' 77, pi= 135344, arr[0] = 35, arr[1] = 56, arr[2] = 77num 6 '*++pi ' 77, pi= 135344, arr[0] = 35, arr[1] = 56, arr[2] = 77num 7 ' pi++' 135344, pi= 135348, arr[0] = 35, arr[1] = 56, arr[2] = 77num 7 ' pi++' 135344, pi= 135348, arr[0] = 35, arr[1] = 56, arr[2] = 77num 8 ' ++pi' 135352, pi= 135352, arr[0] = 35, arr[1] = 56, arr[2] = 77num 8 ' ++pi' 135352, pi= 135352, arr[0] = 35, arr[1] = 56, arr[2] = 77