Subprograms Programming Language PragmaticsFundamentals of SubprogramsTerminologiesSubprogram Syntax in Different LanguagesDefault Values of ParametersVariable Numbers of ParametersParametersParameter Passing MethodsParameter passingPass-by-valueParameter passing (cont.)Pass-by-resultPass-by-value-resultSlide 14Pass-by-referenceSlide 16More About Parameter CollisionParameter CollisionParameter Passing in Major LanguagesExampleType Checking ParametersDesign ConsiderationsParameters that are Subprogram NamesIn Different LanguagesImplementing subprogramsRun-time stackRun-time stack (cont.)Slide 28In-class exerciseOptimizing scopingOverloaded SubprogramsOverloaded Subprogram ExampleGeneric SubprogramsTermsExamples of Parametric Polymorphism: C++CoroutinesSlide 37Coroutines with LoopsDesign Issues for FunctionsConditionals & loopsBranchingBranching (cont.)Procedural controlParameters in JavaPolymorphismReferences/ResourcesSubprogramsSubprogramsProgramming Language PragmaticsProgramming Language PragmaticsCMSC 331 1Fundamentals of SubprogramsFundamentals of SubprogramsEach subprogram has a single entry pointThe calling program is suspended during execution of the called subprogramControl always returns to the caller when the called subprogram’s execution terminates2Func_A{ … call B(); … call B(); …}Func_C{ … call B(); … call B(); …}Func_B{ … …}TerminologiesTerminologiesint Adder(int a, int b);…int Adder(int a, int b){ return a+b; }…void main(){ int x; x = Adder(30, 20);}3real parametersformal parameterswith return values: functionswithout return values: procedurescaller calleeSubprogram Syntax in Different LanguagesSubprogram Syntax in Different LanguagesDeclaration of procedureAda: procedure Adder(…)C/C++: void Adder(…)Binding actual/formal parametersMost languages use positional parametersSome support keyword parameterssum(length=> my_length, list=> my_list);Advantage: specify the parameters in any order4Default Values of ParametersDefault Values of ParametersSupport absent actual parameters in Adafunction compute_pay(income: float; exemptions: integer :=1; tax_rate: float) return float;answer:= compute_pay( 2000.0, tax_rate=> 0.15);Default parameters must appear last in C++ float compute_pay(income: float, tax_rate: float, exemptions: integer :=1);answer:= compute_pay( 2000.0, 0.15);5Variable Numbers of ParametersVariable Numbers of ParametersIn C:printf(“x=%d %d\n”, a, b);Define a function with variable number of parameters“…” indicates anonymous parameters#include <varargs.h>void myTrace(const char *fmt, ......) { va_list ap; // declare variable list va_start(ap, fmt); // init it to fmtint sum = 0;for( int i = 0 ; i < n100; i++ ) { // get an arg. Whose type is known, here we’ll assume int int arg = va_arg( listPointer, int ); printf( " The %dth arg is %d\n", i, arg ); if (arg == 0) return(); sum += arg; }}67ParametersParametersin most languages, parameters are positionalAda also provides keyword parameters: AddEntry(dbase -> cds, new_entry -> mine);advantage: don’t have to remember parameter orderdisadvantage: do have to remember parameter namesAda and C/C++ allow for default values for parametersC/C++ & Java allow for optional parameters (specify with …)public static double average(double... values) { double sum = 0; for (double v : values) { sum += v; } return sum / values.length; } System.out.println( average(3.2, 3.6) );System.out.println( average(1, 2, 4, 5, 8) );if multiple parameters, optional parameter must be rightmost WHY?Parameter Passing MethodsParameter Passing MethodsHow to pass dataInto the function for computationOut from the function after the computationMany modelsPass-by-valuePass-by-resultPass-by-value-resultPass-by-referencePass-by-Name89Parameter passingParameter passingcan be characterized by the direction of information flowin mode: pass by-valueout mode: pass by-resultinout mode: pass by-value-result, by-reference, by-nameby-value (in mode)parameter is treated as local variable, initialized to argument valueadvantage: safe (function manipulates a copy of the argument)disadvantage: time & space required for copyingused in ALGOL 60, ALGOL 68default method in C++, Pascal, Modula-2only method in C (and, technically, in Java)Pass-by-valuePass-by-valueThe value of the actual parameter is used to initialize the formal parameter, which then acts as a local variableActual/formal parameters are stored in different placesExtra storage, expensive for large objects int x = 7;int y = 10;void func1(int •a, int •b){ a = 15; b = a+b; }void main(){ func1 (x, y); printf(“ x= %d y= %d \n”, x, y);}10a = 7b = 10x = 7 y = 10a = 15b = 25x = 7 y = 10Before: After:globaldatamainfunc111Parameter passing (cont.)Parameter passing (cont.)by-result (out mode)parameter is treated as local variable, no initializationwhen function terminates, value of parameter is passed back to argumentpotential problems: ReadValues(x, x);Update(list[GLOBAL]);by-value-result (inout mode)combination of by-value and by-result methodstreated as local variable, initialized to argument, passed back when donesame potential problems as by-resultused in ALGOL-W, later versions of FORTRANPass-by-resultPass-by-resultNo initialization, at the end of the function call, pass the value of formal parameter to its corresponding actual parameterActual/formal parameters are stored in different placesExtra storage, expensive for large objects int x = 7;int y = 10;void func1(int •a, int •b){ a = 15; b = 18; }void main(){ func1 (x, y); printf(“ x= %d y= %d \n”, x, y);}12abx = 7 y = 10a = 15b = 18x = 15 y = 18Before: After:globaldatamainfunc1Pass-by-value-resultPass-by-value-resultCombine pass-by-value and pass-by-resultint x = 7;int y = 10;void func1(int •a, int •b){ a = 15; b = a+b; }void main(){ func1 (x, y); printf(“ x= %d y= %d \n”, x, y);}13a = 7b = 10x = 7 y = 10a = 15b = 25x = 15 y = 25Before: After:globaldatamainfunc114Parameter passing (cont.)Parameter passing (cont.)by-reference (inout mode)instead of passing a value, pass an access path (i.e., reference to argument)advantage: time and space efficientdisadvantage: slower access to values (must dereference), alias confusionvoid IncrementBoth(int & x, int & y) int
View Full Document