DOC PREVIEW
UMBC CMSC 331 - Parameter Passing

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 CorrespondenceWhich actual parameters go to which formal parameters?Most common case: positional parametersCorrespondence determined by positionsnth formal parameter matched with nth actual01/13/19 6Keyword ParametersDetermined by matching parameter namesAda:DIVIDE( DIVIDEND => X, DIVISOR => Y); DIVIDE( DIVISOR => Y, DIVIDEND => X);Parameter order is irrelevant here01/13/19 7Mixed Keyword And PositionalMost languages that support keyword parameters allow both: Ada, Fortran, Dylan, PythonThe first parameters in a list can be positional, and the remainder can be keyword parameters01/13/19 8Optional ParametersDefault 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 ListsSome languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and PerlLibrary routines must be used to access the excess actual parametersA 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 ValueSimplest methodWidely usedThe 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 ResultAlso called copy-out Actual must have an lvalueIntroduced 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-ResultAlso called copy-in/copy-outActual 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: FortranMost efficient for large objectsStill frequently usedFor passing parameters by reference,


View Full Document

UMBC CMSC 331 - Parameter Passing

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Parameter Passing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Parameter Passing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Parameter Passing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?