By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

what is call by name & how it is done

P: n/a
plz reply,
thanks in advance.
bye
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <11**************************@posting.google.com >
Sweety <sw************@yahoo.co.in> writes:
plz reply,
("Plz"? I suppose this means "please go back and look at the subject,
even if whatever system you use to read news has now hidden it from
you." Luckily mine does not, so let me repeat it...)
what is call by name & how it is done?


In computer science / informatics, "call by name" usually refers
to a parameter-passing mechanism that does not exist in C (but did
in Algol). The mechanism simulates replacing all accesses to the
formal parameter in the callee with accesses to the actual parameter
in the caller, each time. A common example, not actual C code but
a sort of transliteration of an Algol call-by-name, might read:

static int i;

void f(int *p) {
*p = 3;
i += 1;
*p = 4;
}

void g(void) {
int a[2];

i = 0;
f(&a[i]);
/* after f() returns, we want a[0]==3 and a[1]==4 */
}

The reason this works in that other language (but not in C) is that
the call to f() says "operate on a[i]". In f(), the first operation
on "a[i]" (now named *p) is "set to 3", but then the second operation
is "increment i by 1". The third and last operation on "a[i]" is
"set to 4". Since i has changed from 0 to 1, this is intended to
set a[1] to 4, rather than writing on a[0] again.

C programmers can simulate call-by-name fairly easily, by writing
macros instead of functions. Macros actually do textual substitution,
so we might replace the above (which does not work) with:
#define F(var) do { \
var = 3; \
i += 1; \
var = 4; \
} while (0) /* see the FAQ for the reason for the do-while loop */

void g(void) {
int a[2];
int i;

i = 0;
F(a[i]);
/* the expansion of F() sets both a[0] and a[1] as desired */
}

Note that it was possible here to move the variable "i" into g(),
because F() is no longer an actual function.

In general "call-by-name"-like-mechanisms -- including the macro
expansion technique above -- lead to maintenance problems. This
is most likely why few languages have direct support for it.

You can simulate true call-by-name, rather than faking it with
macros, by writing C code that implements "thunks" (procedures that
return references to the variables in question -- in C, functions
that return pointers to the desired variables). This is generally
quite clumsy, and rarely useful. I have written articles in the
past showing how to write thunks in C; they are probably archived
in google.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2

P: n/a
Sweety wrote:

plz reply,


Oh very well.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.