Trying to clearly state the semantics of a function call.
In calling a function, are the arguments passed to the function the ones the calling code initially gives or the ones the function receives?
With code like below, the calling code in bar()
calls foo()
twice. This first time with int 2
, then ...
1) Function foo()
is called with a double 3.1
, x
has the converted value of int 3
,
or
2) The value double 3.1
is converted to int 3
and function foo()
is called,
or
3) Something else.
IOWs, is the conversion of double 3.1
to int 3
part of the call of a function (the conversion would not happen without the function call) or is it a preceding activity (consider to be part of the calling code) before the function call?
void foo(int x);
void bar() {
foo(2);
foo(3.1);
}
This query is primarily C
, yet language agnostic thoughts appreciated. A quick answer is not needed.
[Edit]
Note: This is not an question of how platforms create the binary/executable to implement the program code - just about the code/language.
[Edit 2] @ Erik Eidt useful comment provided better words to use (at least for C) for this question.
Perhaps a more succinct question would be, from the language perspective: "Are functions called with "actual arguments" or "formal parameters"?
C11 3.3 actual arguments and 3.16 1 formal parameter.
foo
andbar
mixed up a few times in your explanation so I'm not quite sure what you're asking. That said, I suspect the answer is "the compiler can put the conversion wherever it wants" because it would never have a noticeable effect on your program's behavior either way. In fact, for your exact sample I'd expect a compiler to drop the runtime conversion entirely and just output the code for afoo(3)
call.foo/bar
correction. Yes a compiler can optimize tofoo(3)
. This example is a simplification as the code could bedouble x = fred(); foo(x)
. I am more questioning the language concepts and not so much what binary a compiler might render.actual arguments
,actual parameters
, oractuals
, where as the ones the function receives are calledformal arguments
,formal parameters
, orformals
.