Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: MSC-all C's Message-ID: <6676@brl-smoke.ARPA> Date: Sat, 14-Nov-87 15:59:35 EST Article-I.D.: brl-smok.6676 Posted: Sat Nov 14 15:59:35 1987 Date-Received: Sun, 15-Nov-87 19:07:54 EST References: <3094@rosevax.Rosemount.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 33 In article <3094@rosevax.Rosemount.COM> richw@rosevax.Rosemount.COM (Rich Wagenknecht) writes: > 1) What happens in C when a function returns a value which the > calling line does not assign to a variable? Where does this > value end up? (example: strcpy(str1,str2); ) Could someone > explain the process that is gone through when a function is > called? Just as with the value of "x = 1", the value of the expression is used only if the programmer chooses to use it. It is misleading to ask where the value ends up; it is up to the compiler to do the right thing (which amounts to not having the value "end up" anywhere that it was not explicitly placed by the programmer). The process that occurs when a function-call expression is evaluated at run time is: 1. The arguments are evaluated (in unspecified order). 2. The formal parameter variables in the function definition receive the corresponding values of the arguments. 3. The body of the function definition is evaluated until an explicit "return" expression is evaluated or the end of the body is reached (which constitutes an implicit "return;"). 4. If the function returns a value and its evaluation ended with a "return" that had a value as its operand, that value replaces the function invocation in the calling context, and that higher-level evaluation process resumes using that value. (If the value is not explicitly used by the programmer, then evaluation simply proceeds to the next expression.) This is really no different from most programming languages that support functions, although C is unusual in that all arguments are passed by value (array names are apparent exceptions; by the funny C rules for array names in various contexts, they are converted to pointers to the first members of the arrays when passed as arguments, which is similar to "call by reference" found in some languages).