0

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

Deepaank
  • 1
  • 1
  • You should add more context. Unlike you, none of us has taken that class. To be honest, this seems to be plain wrong terminology that a lazy lecturer invented. Assuming this is about C, then there's really just 1 and 2 in the language, and 3 is not really a mechanism but a design pattern, 4 is not parameter passing, and 5 is something completely different. – Marcus Müller Jul 26 '15 at 16:41
  • I myself hasn't attended that class. I came through this link while searching for the answer to my question. Though the last two are not at all used in present implementation of parameter passing in any language, I just want to clarify the difference, theoretically, or by some pseudo-code-type example. – Deepaank Jul 26 '15 at 17:06

1 Answers1

1

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

Andrew Deniszczyc
  • 3,298
  • 3
  • 19
  • 16