Deepaank
Deepaank

Reputation: 1

Difference between call by name and call by text parameter passing mechanism with examples

Out of 5 types of parameter passing mechanism: 1.pass-by-value 2.pass-by-reference 3.pass-by-value-result 4.pass-by-text (macros in C) 5.pass-by-name (something like continuations)

I just want the difference between the last two.Please help !!

Reference: http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Outlines/outline.36.html

Upvotes: 0

Views: 2538

Answers (1)

Andrew Deniszczyc
Andrew Deniszczyc

Reputation: 3418

Call-by-text is where the function arguments are not evaluated before they are passed and are then substituted for the instances of the parameters. The arguments are passed "as text" and can hence cause problems if the local bound of the function use the same variable names outside the scope.

int i = 0;

void f(int j) {
    print(j);   // is replaced with print(i + 5) and prints 5
    int i = 20;
    print(j);   // is replaced with print(i + 5) and prints 25
}

f(i + 5);        // passes the unevaluated expression i + 5

Call-by-name is similar in that the function arguments are not evaluated before they are passed and are then substituted for the instances of the parameters. However, the parameters are bound to thunks, which act as a closure for variables within the scope of the calling function.

void f(int j) {
    print(j);   // prints 5
    print(j);   // prints 10
}

int i = 0;
f(i + 5);        // passes the unevaluated expression i + 5

More information can be found here: http://www.cs.sjsu.edu/~pearce/modules/projects/Jedi/params/index.htm

Upvotes: 1

Related Questions