Parameter PassingParameter Passing7 methods for passing parametersWhat’s wrong?Parameter CorrespondenceKeyword ParametersMixed Keyword And PositionalOptional ParametersUnlimited Parameter ListsCall By ValueSlide 11By ResultSlide 13Slide 14Slide 15By Value-ResultSlide 17Slide 18Slide 19Why call by Value-Result?By Reference (address)Slide 22Slide 23Slide 24Slide 25TrickyAliasingAliasing in call by referenceTrouble caused by AliasBy Macro ExpansionMacro Expansions In CPreprocessingA little troubleRepeated EvaluationCapture, used to be a big problem in logicCapture in actuals (Bad form)Solution to bad capture: ConversionCapture in PLCapture ExampleBy Name (a way to avoid previous bad capture)Implementing By-NameCall by name ExampleSlide 43ComparisonBy NeedSlide 46Slide 47Laziness: compute only when necessaryParameter PassingCMSC 331Extracted with only very minor changes fromhttp://www.itk.ilstu.edu/faculty/chungli/ITK327/Parameters.ppt01/13/19Parameter Passing How are parameters passed?Looks simple enough…We will see seven techniquesint plus (int a, int b){return a+b;}int x = plus(1,2);formal parametersfunctionbodyfunction callactual parameters(arguments)01/13/19 37 methods for passing parametersBy valueBy resultBy value-resultBy referenceBy macro expansionBy nameBy need01/13/19 4What’s wrong?int * sum (int & a, int & b){int sum;sum = a + b;return & sum;}int * sum (int & a, int & b){a += b;return & a;}int * sum (int & a, int & b){a += b;;return a;}int a, b, *c;a=1;b=2;c = sum(a,b);01/13/19 5Parameter CorrespondenceWhich actual parameters go to which formal parameters?Most common case: positional parametersCorrespondence determined by positionsnth formal parameter matched with nth actual01/13/19 6Keyword ParametersDetermined by matching parameter namesAda:DIVIDE( DIVIDEND => X, DIVISOR => Y); DIVIDE( DIVISOR => Y, DIVIDEND => X);Parameter order is irrelevant here01/13/19 7Mixed Keyword And PositionalMost languages that support keyword parameters allow both: Ada, Fortran, Dylan, PythonThe first parameters in a list can be positional, and the remainder can be keyword parameters01/13/19 8Optional ParametersDefault values: formal parameter list includes default values to be used if the corresponding actual is missingint f(int a=1, int b=2, int c=3) { body }int f() {f(1,2,3);}int f(int a) {f(a,2,3);}int f(int a, int b) {f(a,b,3);}int f(int a, int b, int c) { body }Same as Overloading:int x=f(), y=f(0), z=f(0,0), h=f(0,0,0);01/13/19 9Unlimited Parameter ListsSome languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and PerlLibrary routines must be used to access the excess actual parametersA hole in static type systems, since the types of the excess parameters cannot be checked at compile timeint printf(char *format, ...) { body }01/13/19 10Call By ValueSimplest methodWidely usedThe only method in Java!!!!For by-value parameter passing, the formal parameter is just like a local variable in the activation record of the called method, with one important difference: it is initialized using the value of the corresponding actual parameter, before the called method begins executing.Or not!Java is Pass-By-Value, Dammit!01/13/19 11previous activation record return address a: 3 result: ? current activation record previous activation record return address x: 3 b: 4 y: 4 z: ? int plus(int a, int b) { a += b; return a; }void f() { int x = 3; int y = 4; int z = plus(x, y);}When plus is startingf()plus(3,4)01/13/19 12By ResultAlso called copy-out Actual must have an lvalueIntroduced in Algol 68; sometimes used for AdaFor by-result parameter passing, the formal parameter is just like a local variable in the activation record of the called method—it is uninitialized. After the called method finished executing, the final value of the formal parameter is assigned to the corresponding actual parameter.01/13/19 13void plus(int a, int b, by-result int c) { c = a+b;} void f() { int x = 3; int y = 4; int z; plus(x, y, z); }previous activation record return address a: 3 current activation record previous activation record return address x: 3 b: 4 y: 4 z: ? c: ? When plus is startingf()plus(3,4,z)01/13/19 14void plus(int a, int b, by-result int c) { c = a+b;} void f() { int x = 3; int y = 4; int z; plus(x, y, z); }When plus is ready to returnprevious activation record return address a: 3 current activation record previous activation record return address x: 3 b: 4 y: 4 z: ? c: 7 f()plus(3,4,z)01/13/19 15void plus(int a, int b, by-result int c) { c = a+b;} void f() { int x = 3; int y = 4; int z; plus(x, y, z); }When plus has returnedprevious activation record return address a: 3 current activation record previous activation record return address x: 3 b: 4 y: 4 z: 7 c: 7 f()01/13/19 16By Value-ResultAlso called copy-in/copy-outActual must have an lvalueFor passing parameters by value-result, the formal parameter is just like a local variable in the activation record of the called method. It is initialized using the value of the corresponding actual parameter, before the called method begins executing. Then, after the called method finishes executing, the final value of the formal parameter is assigned to the actual parameter.01/13/19 17void plus(int a, by-value-result int b) { b += a;}void f() { int x = 3; plus(4, x); }When plus is startingprevious activation record return address a: 4 current activation record previous activation record return address x: 3 b: 3 f()plus(4,x)01/13/19 18void plus(int a, by-value-result int b) { b += a;}void f() { int x = 3; plus(4, x); }When plus is ready to returnprevious activation record return address a: 4 current activation record previous activation record return address x: 3 b: 7 f()plus(4,x)01/13/19 19void plus(int a, by-value-result int b) { b += a;}void f() { int x = 3; plus(4, x); }When plus has returnedprevious activation record return address a: 4 current activation record previous activation record return address x: 7 b: 7 f()01/13/19 20Why call by Value-Result?f(): int x = 3; fork: { check_d(4, x);check_d(5, x); }f():x: 3check_d1():a: 4b: 3check_d1():a: 5b: 38 7check_d(int a, int value-result b) b = ....;}01/13/19 21By Reference (address)One of the earliest methods: FortranMost efficient for large objectsStill frequently usedFor passing parameters by reference,
View Full Document