1 / 23

CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

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.

Télécharger la présentation

CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Syllabus • Summary • Value Parameter • Reference Parameter • Value-Result Parameter • Name Parameter • In-Out Parameter • References

  3. Summary Parameter passing is programming language tool to bind information of caller to callée The parameter on the calling side is the actual parameter (AP); may be an expression or named object (variable) The parameter on the called side (callée) is the formal parameter (FP); it is always named The actual is bound to the formal at place of call Binding lasts for the duration of the call Association actual-to-formal is generally by position May also be by naming the formal at the place of call, e.g. in Ada. In that case the order may be arbitrarily intermixed at place of call

  4. Summary Types of actual and formal generally must be compatible, subject to the language’s type compatibility- or type conversion rules C/C++ allow a lesser number of actual parameters to be passed than formally declared Typical implementation of C/C++ therefore must pass actuals in reverse textual order Theoretically, APs are passed on the run-time stack Physically, good optimizers pass APs in registers Practically, compiler passes the first few parameters in registers and remaining ones on the stack FPs are generally located on one side off the stack marker, while locals are positioned on the opposite side of the stack marker, the one requiring negative, the other positive offsets from a base pointer register

  5. Summary Value Parameter (VP): Actual parameter (AP) provides initial value via a type-compatible expression. If modifications to bound formal parameter (FP) occur, they do not impact the AP Reference Parameter(RP): AP must be an addressable object. If modifications to bound FP occur, they do impact the AP. Implemented by passing the address of the AP which is a variable Value-Result Parameter (VRP): AP must be an addressable object. If modifications to bound FP occur, they do impact the AP, but such assignments to AP occur at the place of return. Implemented via copy-in copy-out More detail and other kinds of formal parameters below

  6. Value Parameter (VP) The actual parameter (AP) to be bound to a formal value parameter (VP) must provide an initial value to the formal parameter (FP) Initial value is the first value of the FP; that initial actual value may be an expression or object During the call, assignments to the formal VP are allowed; exception: in-parameters in Ada Assignments to the formal VP have no impact on AP during the call Assignments to the formal VP also have no impact on the actual after the call Implementation: must make a copy of the initial value of the AP and pass it in register or more generally on the stack At the return, discard the copy, which also voids al changes to the FP from the time during the call

  7. Value Parameter in C C requires parameter passing by value 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 course the object pointed to may very well change during call, but that still keeps the pointer intact C structs are passed by value, requiring a copy of the actual struct; may be a large amount of data space How can a programmer pass an array[] by value in C? Pack array[] as a field into a struct, consumes identical amount of space as the original array[]; will all be copied at place & moment of call

  8. Value Parameter in C++ C++ inherits C’s parameter passing methods Hence the default parameter passing method in C++ is also by value But C++ adds the explicit reference parameter, using the & operator; see later

  9. Value Parameter in C, Sample Set-Up #define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", msg, value );\ } //end if typedef struct { int field; } struct_tp; typedef int * int_ptr; int global1 = 109; int global2 = 2012;

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

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

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

  13. Reference Parameter (RP) The AP for RP passing must be a named variable The AP does not have to be initialized, in which case callée better abstain from referencing before assigning Goal is for the callée to compute and then return a new value in the AP Assignments to the formal reference parameter (RP) have an immediate impact on the AP, since FP and AP are aliased to one another in RP passing Note that aliasing may be a source of hard to track errors Implementations generally pass RP by passing the address of the AP; hence it is clear that the AP must be an addressable object

  14. Reference Parameter in C++, Set-Up #include <iostream> #define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", \ msg, ( value ) ); \ } #define MAX 10 typedef struct { int field; } struct_tp; typedef int arr_tp[ MAX ]; typedef int * int_ptr; // some globally visible variables int global1 = 109; int global2 = 2012;

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

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

  17. Reference pointer Parameter in C++ // assign to formal "ref ptr param" impacts actual void change_ptr_ref( int_ptr & value ) { // change_ptr_ref ASSERT( *value == 109, "pointer-to should be 109", *value ); value = & global2; // formal value changed ASSERT( *value == 2012, "pointed-to should be 2012", *value ); } //end change_ptr_ref void ptr_ref( void ) { // ptr_ref int_ptr local1 = & global1; ASSERT( *local1 == 109, "pointer-to should be 109 in ptr_ref", *local1 ); change_ptr_ref( local1 ); ASSERT( local1 == & global2, "Wrong pointer; points to:", *local1 ); ASSERT( *local1 == 2012, "pointed-to value should be 2012", *local1 ); } //end ptr_ref

  18. Reference array[] Parameter in C++ // assign to formal "array param" impacts actual void change_arr_ref( arr_tp value ) // reference default!! { // change_arr_ref ASSERT( value[ 5 ] == 109, "value[ 5 ] should be 109", value[ 5 ] ); value[ 5 ]++; // formal value changed ASSERT( value[ 5 ] == 110, "value[5] should be 110 in change()”, value[5] ); } //end change_arr_ref void arr_ref( void ) { // arr_ref arr_tp value; for ( int i=0; i < MAX; i++ ) { value[i] = 109; } ASSERT( value[ 5 ] == 109, "value[5] should be 109 in arr_ref", value[5] ); change_arr_ref( value ); ASSERT( ( value[ 5 ] == 110 ), "value[ 5 ] should be 110", value[ 5 ] ); } //end arr_ref

  19. Value-Result Parameter AP must be addressable object, to be bound to FP Assignments to FP are allowed in callée During the call, the AP is unchanged; note that this is quite different from RP passing! At moment of return –and there may be many places in the code– the last value assigned to the FP is copied back into the AP Not discussed further in CS 201 Should be covered in Fortran programming class, or compiler class

  20. Name Parameter Is the unusual, quirky, default parameter passing method in Algo60 The text of the AP substitutes the corresponding FP for any particular call Highly complicated to understand and implement, first done by Ingerman, using “thunks” Not covered in CS 201; belongs into Compiler class

  21. Name Parameter Comment: Algol60 sample; definition of confuse() procedure confuse( a, b ) integer a, b begin b := 4; a := 5; end; confuse( x[ i ], i ); comment: i = 33, x[33] = 10 Comment: as if confuse() had been coded like this: procedure confuse( x[i], i ) integer x[ i ], i begin i := 4; x[4] := 5; end; Comment: x[33] is unaffected, i = 4, and x[4] = 5

  22. In-Out Parameter Is one of the parameter passing methods used in Ada; not identical to VRP Is a combination of the in-parameter and out-parameter passing methods Similar to value-result parameter passing, but it is left unspecified at which moment the value of the FP is copied back into the AP Will not be discussed in CS 201

  23. References Revised Algol60 report: http://www.masswerk.at/algol60/report.htm ADA reference manual: http://www.adaic.org/resources/add_content/standards/05rm/html/RM-TTL.html Ada report: http://www.sigada.org/ada_95/what_is_ada.html Fortran standard: http://www.j3-fortran.org

More Related