Subprogram • procedure (command) Subprograms • function (expression) • Subprogram (subroutine) – statement abstraction, control abstraction • Subprogram design – if a subprogram does not fit on the screen, it is too long • divide the solution of the problem into smaller parts – if an action is needed twice (or more times), implement the action as a subprogram • when are two seemingly different things the same? • when are seemingly similar things different? TUT Pervasive Computing Principles of programming languages 1 Maarit Harsu / Matti Rintala / Henri Hansen
Subprogram terminology subprogram formal result name parameter type header function Square ( x: Integer ): Integer; begin body Square := x * x end ; Pascal-code TUT Pervasive Computing Principles of programming languages 2 Maarit Harsu / Matti Rintala / Henri Hansen
• header • name More terminology • formal parameters • name and type of subprograms • result type (of functions) • body • declarations and statements • (invariants) • Subprogram definition • (exceptions) – header (interface) and body (functionality) • Subprogram declaration (prototype) • Subprogram call (activation) procedure P ( parameters ) – active subprogram void P ( parameters ) • Parameters – formal and actual parameters • Parameter profile – number, order, and types of the formal parameters TUT Pervasive Computing Principles of programming languages 3 Maarit Harsu / Matti Rintala / Henri Hansen
Parameters • Parameter compatibility – binding of actual parameters to formal ones non-ANSI-C: – arity check void fun ( i, j ) – type checks int i, j; • C-language { ... } ... – K&R-C (non-ANSI-C) does not check fun ( 1, 2, 3 ); parameter numbers nor types – ANSI-C may omit checkings – allows variant number of parameters • e.g. printf int printf ( char *format, ... ) TUT Pervasive Computing Principles of programming languages 4 Maarit Harsu / Matti Rintala / Henri Hansen
Parameter binding • Positional parameters – binding according to the order • Keyword parameters – the name of the formal parameter is given in the call Ada-code: procedure F ( i, j: in Integer; a, b: out Float ); ... Imaginary F ( a => my_a, b => my_b, i => 0, j => 2 ); code: format_page ( colums => 2, window_height => 400, window_width => 200, header_font => Helvetica, body_font => Times, title_font => Times_Bold, header_point_size => 10, body_point_size => 11, title_point_size => 13, justification => true, hyphenation => false, page_num => 3, paragraph_indent => 18, background_color => white ); 5 TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Default value parameters Ada: procedure Increment ( i: in out Integer; by: in Integer := 1 ); ... Increment ( x ); • Problem: – Which formal parameter the actual parameter 3 is bound to? Imaginary code: procedure P ( x: Integer := 1; y: Integer := 2 ); ... P ( 3 ); • Programming languages have different rules to solve the problem TUT Pervasive Computing Principles of programming languages 6 Maarit Harsu / Matti Rintala / Henri Hansen
Rules for the default value parameters Ada: function ComputePay ( • Ada: Income: Float; • Python: Exemptions: Integer := 1; TaxRate: Float ) return Float; ... Pay := ComputePay ( 2000.0, TaxRate => 0.15 ); – parameters that follow absent default-value parameters must be keyword parameters • C++: – default-value parameters must be the last ones in the list TUT Pervasive Computing Principles of programming languages 7 Maarit Harsu / Matti Rintala / Henri Hansen
Parameter passing methods Imaginary language following Pascal syntax: • Pass-by-value program P; var I: Integer; • Pass-by-reference A: array [ 1..3 ] of Integer; procedure Test ( F, G: Integer ); • Pass-by-result begin F := F + 1; • Pass-by-value-result G := 5 * I; end ; • Pass-by-name begin for I := 1 to 3 do A [ I ] := I; I := 2; Test ( I, A [ I ] ); Write ( I, A [ 1 ], A [ 2 ], A [ 3 ] ); end . TUT Pervasive Computing Principles of programming languages 8 Maarit Harsu / Matti Rintala / Henri Hansen
Parameters passed by value • Passing information from the caller to the subprogram – formal parameter is like a local variable – initialized with the value of the actual parameter act. var F: Integer := I; = 2 G: Integer := A [ I ]; = A [ 2 ] = 2 ? begin F := F + 1; = 3 Output: G := 5 * I; = 10 2 1 2 3 end ; TUT Pervasive Computing Principles of programming languages 9 Maarit Harsu / Matti Rintala / Henri Hansen
Parameters passed by reference • Passing information in both directions – formal parameter refers to the variable (store, memory location) given in the call – variable (store) is defined before the call and remains the same throughout the execution – requires an actual parameter to be a variable (lvalue) begin act. I := I + 1; = 3 Output: A [ 2 ] := 5 * I; = 15 3 1 15 3 end ; TUT Pervasive Computing Principles of programming languages 10 Maarit Harsu / Matti Rintala / Henri Hansen
Constant reference parameters (pass-by-constant) • Passing information in one direction – formal parameter is a reference to the variable in the actual parameter – variable is defined before the call and remains the same throughout the subprogram execution – formal parameter cannot be changed – (changes in actual parameter are visible in the subprogram) – good if copying values is costly (memory/time) TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Parameters passed by value-result • Passing information in both directions – formal parameter is like a local variable – its initial value is the value of the actual parameter – final value is copied to the actual parameter var F: Integer := I; = 2 G: Integer := A [ 2 ]; = 2 act. begin F := F + 1; = 3 G := 5 * I; = 10 Output: --- I := F; = 3 3 1 10 3 A [ 2 ] := G; = 10 TUT Pervasive Computing Principles of programming languages 12 end ; Maarit Harsu / Matti Rintala / Henri Hansen
Parameters passed by result • Passing information from the subprogram to the caller – formal parameter is like a local variable that has not yet initialized – final value is copied to the actual parameter var F, G: Integer; act. begin Execution leads F := F + 1; to an error ? G := 5 * I; (in this example) --- I := F; ”Multiple A [ 2 ] := G; return values” end ; TUT Pervasive Computing Principles of programming languages 13 Maarit Harsu / Matti Rintala / Henri Hansen
In no major Parameters passed by name language any more (Algol60) • Textual copy • Actual parameter is evaluated each time it is referenced in the subprogram • Variable names in the parameter expression refer to the scope of the caller • C.f. lazy evaluation, lambdas, #define in C/C++ begin I := I + 1; = 3 Output: A [ I ] := 5 * I; i.e. A [ 3 ] = 15 3 1 2 15 end ; TUT Pervasive Computing Principles of programming languages 14 Maarit Harsu / Matti Rintala / Henri Hansen
Pass-by-name example Imaginary function Sum ( i: name Integer; p, n: Integer; language E: name Real ): Real; following var S: Real := 0.0; Pascal’s syntax: begin for i := p to n do S := S + E; n ∑ E ( i ) Sum := S; end ; i=p var x: Integer; r: Real; ... r := Sum ( x, 1, 100, 2 * Sin ( 3.14 * x ) – 3 * x ); TUT Pervasive Computing Principles of programming languages 15 Maarit Harsu / Matti Rintala / Henri Hansen
Parameter passing in programming languages • Java & Python – pass-by-value (for primitive types) – pass-by-reference (for classes) • C – pass-by-value (but arrays are passed by reference) • C++ – pass-by-value and pass-by-reference • Ada – pass-by-value ( in ) [default passing method] – pass-by-result ( out ) – pass-by-value-result ( in out ) • Pascal – pass-by-value ( val ) [default passing method] pass-by-name – pass-by-reference ( var ) in Algol60 TUT Pervasive Computing Principles of programming languages 16 Maarit Harsu / Matti Rintala / Henri Hansen
Parameter passing in Haskell • Called pass-by-assignment – actual parameter value is assigned to the formal parameter – lazyness: values are evaluated only when needed • In effect: pass-by-reference – every variable is a reference to an object – therefore actual parameters are references • However: in most cases pass-by-value – many objects are immutable – changing the formal parameter has no effect on the caller • E.g. passing a reference to an array – no effect on the caller when the formal parameter is assigned a new array object – however, assigning a value to an element of the array, the actual parameter is affected list [ 3 ] = 47 TUT Pervasive Computing Principles of programming languages 17 Maarit Harsu / Matti Rintala / Henri Hansen
Recommend
More recommend