I know that C uses pass-by-value and we can emulate pass-by-reference with the help of pointers. But, for example, in order to calculate a simple mathematical expression, how do I implement pass-by-name (which is kind of lazy evaluation but not exactly) in C?
Asked
Active
Viewed 486 times
0
-
1Please explain what do you mean by pass by name, and how is that different from pass by value, and pass by reference – Rishikesh Raje May 04 '16 at 11:14
-
@RishikeshRaje Well, rather not. search here. – Sourav Ghosh May 04 '16 at 11:15
-
Never heard of it myself, but LMGTFY gave me: http://stackoverflow.com/questions/838079/what-is-pass-by-name-and-how-does-it-work-exactly – Lundin May 04 '16 at 11:15
-
@pheonix Please let us knopw why pass-by-value thr' a pointer does no suffice your needs. :) – Sourav Ghosh May 04 '16 at 11:15
-
Also, in your case, is _implement_ == _emulate_? – Sourav Ghosh May 04 '16 at 11:17
-
@SouravGhosh Yes, exactly emulating, sorry. – phoeNix May 04 '16 at 11:18
-
You use the right tool for the job. Which is, if you need pass by name, something other than C. – n. m. could be an AI May 04 '16 at 11:19
-
C does not support pass by reference. Your prerequisite is wrong. – too honest for this site May 04 '16 at 11:19
-
C is turing complete, so "how to implement it?" - By writing code. – too honest for this site May 04 '16 at 11:20
-
1@Olaf , what I mean is emulating. – phoeNix May 04 '16 at 11:21
-
Emulating writing code? Anyway, don't get too fancy. If you want such features, use a language which is suited for this, e.g. Python. – too honest for this site May 04 '16 at 11:22
-
@Olaf Of course I can do this with Python, but I want to learn to do this in C. I am studying computer science, I do not expect you to do my assignments, but help me learning the process. – phoeNix May 04 '16 at 11:26
-
The standard term is "call by name". It is best known from Algol 60, and classically implemented using objects known as "thunks". Basically, arguments are evaluated when the called function accesses them, as many times as they are accessed, in the context of the caller. Probably the closest you can get in C would be to pass functions which are called by the function each time the argument is needed. – Tom Karzes May 04 '16 at 11:31
-
1It might be possible to implement this using the gcc extension which supports nested functions, which could then be passed as arguments. The caller would then call the functions when it needs the arguments. But this is not portable and is not true C, so it would only work with gcc with the extension enabled. I suppose you could instead use structures that contain both data pointers and function pointers. It's ugly no matter how you do it. – Tom Karzes May 04 '16 at 11:48
-
Call by name example: You call a function `foo(i, a[i])`. Function `foo` is declared `void foo(int x, int y)`. With call by name, if `foo` assigns 5 to `x`, then accesses `y`, it will end up getting `a[5]`, since `i` is now 5 in the caller. – Tom Karzes May 04 '16 at 11:53
-
@TomKarzes I've got the point, thank you very much. – phoeNix May 04 '16 at 12:02
-
I believe that the C preprocessor uses something pretty close to pass by name. For example, if we say `#define foo(x, y) (x=5, printf("%d\n", y))`, then invoke `foo(i, a[i])`, it'll do just what @TomKarzes described. – Steve Summit May 04 '16 at 13:04
-
See http://c-faq.com/ptrs/passbyref.html . – Steve Summit May 04 '16 at 13:07
2 Answers
3
C is only pass-by-value. You can't pass by reference or name. With the pre-processor you can do various hacks but not in the C language.
Sometimes, people call passing a pointer "pass-by-reference" but this is not the case. The pointer is passed by value like anything else. C++ is a different story but you asked about C.
You might also be interested in this article discussion this at length

Johannes Weiss
- 52,533
- 16
- 102
- 136
-
_You can't implement pass-by-reference_, sorry, but isn't it too negative? – Sourav Ghosh May 04 '16 at 11:16
-
I am pretty sure that you can emulate pass-by-reference by using pointers. – phoeNix May 04 '16 at 11:18
-
2#offtopic: `C is only pass-by-value`..I was unaware... I thought C is a language.. :P – Sourav Ghosh May 04 '16 at 11:18
-
thanks both, no idea why I wrote "you can't implement". The implement is not necessary and might even be misleading. – Johannes Weiss May 04 '16 at 11:18
-
1Of course you can implement pass-by reference. The question is what it applies to. Given `void func (int* x)`, the pointer address is passed by value, but the pointed-at data is passed by reference. Otherwise if you want to be picky, computers can only pass addresses to functions by putting values on the stack or in index registers. No pass by reference exists anywhere inside a computer. – Lundin May 04 '16 at 11:19
-
1Note OP changed the question, which renders part of your answer useless. – too honest for this site May 04 '16 at 11:21
-
I have corrected the misleading part of the question. What I need is an example of pass-by-name emulation using C (I think macros may be useful, but I have no idea..) – phoeNix May 04 '16 at 11:23
-
@phoeNix: *Tom Karzes* explained the concept here: http://stackoverflow.com/questions/37026187/pass-by-name-implementation-in-c#comment61603043_37026187. Now it's up to *you* to implement it. If running into *specific* issues while doing so, feel free come back here asking another question on it. – alk May 04 '16 at 11:37
0
The parameter substitution used by function-like preprocessor macros is sometimes described as being "pass by name".

Steve Summit
- 45,437
- 7
- 70
- 103
-
Do you have any example demonstrating that? For example, subtracting two variables? I have tried thunking but I could not work it out.. – phoeNix May 05 '16 at 19:24