Discussion:
Function calls
(too old to reply)
Stefan Ram
2023-10-04 15:59:54 UTC
Permalink
A recent draft of the C specification says about "return":

|A return statement terminates execution of the current
|function and returns control to its caller.

. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
Stefan Ram
2023-10-04 17:52:11 UTC
Permalink
Post by Stefan Ram
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
Now, some might say that C references "ISO/IEC 2382:2015,
Information technology — Vocabulary" which defines "call".
But this (at least in the second edition) defines a "call"
to be an /instruction/, not a runtime event.

The C specification uses the wording "before the actual call",
which wording requires a call to be a runtime event.

So, although "ISO/IEC 2382" then (in its second edition)
explains that a call is an instruction to "transfer control
from one module to another" it does not blend in well -
and it still does not tell us from which "module" to which
"module" control is transfered in the case of a call in C.
Kaz Kylheku
2023-10-04 18:41:45 UTC
Permalink
Post by Stefan Ram
Post by Stefan Ram
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
Now, some might say that C references "ISO/IEC 2382:2015,
Information technology — Vocabulary" which defines "call".
But this (at least in the second edition) defines a "call"
to be an /instruction/, not a runtime event.
In my opinion, ISO 2382 is bunk. Its definitions are comically
primitive; it's like some sixth grader's computer blog.

ISO C should beef up its definitions section and stop referring to that
useless, embarassing document.

Sample of the amateuris drivel:

block diagram

A diagram of a system in which the principal
parts or functions are represented by blocks
connected by lines that show the relationships
of the blocks.

Wow, really.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.
Kaz Kylheku
2023-10-04 18:38:36 UTC
Permalink
Post by Stefan Ram
|A return statement terminates execution of the current
|function and returns control to its caller.
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
6.2.4 Storage duration of objects, paragraph 6, has:

(Entering an enclosed block or calling a function suspends, but does not
end, execution of the current block.)

The parentheses likely acknowledge that the requirement doesn't
belong in a section about storage duration.

It doesn't seem to be repeated anywhere else. It is not defined what
"entering" means.

ISO 2382-1 itself uses "enter" in multiple places without defining it.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.
Tim Rentsch
2023-10-05 02:21:27 UTC
Permalink
Post by Stefan Ram
|A return statement terminates execution of the current
|function and returns control to its caller.
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
AFAICT the C standard does not say explicitly that a function
call gives or transfers control to the function being called.
It has been pointed out that the standard does say that calling
a function suspends execution of the current block.

Considering those facts, do you think there is a problem with
the current wording used in the standard? If you do, what
would you say the problem is, and why is it a problem?
James Kuyper
2023-10-06 03:50:04 UTC
Permalink
Post by Tim Rentsch
Post by Stefan Ram
|A return statement terminates execution of the current
|function and returns control to its caller.
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
AFAICT the C standard does not say explicitly that a function
call gives or transfers control to the function being called.
Section 6.9.1 is about function definitions,. while 6.5.2.2 is about
function calls, but 6.9.1 has several paragraphs of specification about
what happens when the function is called, and the relevant one is:

"After all parameters have been assigned, the compound statement
that constitutes the body of the function definition is executed." (6.9.1p11).
Tim Rentsch
2023-10-06 12:38:02 UTC
Permalink
Post by James Kuyper
Post by Tim Rentsch
Post by Stefan Ram
|A return statement terminates execution of the current
|function and returns control to its caller.
. There's also a section "Function calls" in a recent draft.
I expect that this section says something similar, to the effect that
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
AFAICT the C standard does not say explicitly that a function
call gives or transfers control to the function being called.
Section 6.9.1 is about function definitions,. while 6.5.2.2 is
about function calls, but 6.9.1 has several paragraphs of
specification about what happens when the function is called, and
"After all parameters have been assigned, the compound statement
that constitutes the body of the function definition is executed." (6.9.1p11).
That is one relevant paragraph, but not the only relevant paragraph.
Paragraph 10 of that section says

On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding
parameter as if by assignment. (Array expressions and function
designators as arguments were converted to pointers before the
call.)

What we are looking for is something that says the function is
"entered", that is, that control is transferred. It appears the
C standard doesn't actually say that. In section 6.5.2.2, which
describes function call expressions, the standard doesn't even
explicitly say that a function call expression will call the
function. The word "call" is used often in 6.5.2.2, but always
as a noun, never a verb. So 6.5.2.2 says a lot about function
calls, but never explicitly says that one takes place.

I expect no one is confused or unsure about what semantics are
intended for a function call expression. The question being
asked is about whether the C standard does or should say that a
transfer of control takes place (and if so, how explicitly).
AFAICT the C standard makes no explicit statement that a function
call expression causes a transfer of control, i.e., that at
runtime the called function is "entered". What text in the
standard says that a function call expression causes a transfer
of control to take place? Or isn't there one?
James Kuyper
2023-10-07 01:04:16 UTC
Permalink
...
Post by Tim Rentsch
Post by James Kuyper
Post by Tim Rentsch
AFAICT the C standard does not say explicitly that a function
call gives or transfers control to the function being called.
Section 6.9.1 is about function definitions,. while 6.5.2.2 is
about function calls, but 6.9.1 has several paragraphs of
specification about what happens when the function is called, and
"After all parameters have been assigned, the compound statement
that constitutes the body of the function definition is executed." (6.9.1p11).
That is one relevant paragraph, but not the only relevant paragraph.
Paragraph 10 of that section says
On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding
parameter as if by assignment. (Array expressions and function
designators as arguments were converted to pointers before the
call.)
Yes, I mentioned that - "several paragraphs of specification about
what happens when the function is called".
Post by Tim Rentsch
What we are looking for is something that says the function is
"entered", that is, that control is transferred.
I find it confusing that you think something more needs to be
specified. The standard specifies that the compound statement
that constitutes the body of the function gets executed. Could you
explain how that differs from having control transferred to the
function? Please give an example of what it would mean to have
the compound statement executed without transferring control. To
me, execution of the compound statement is what I want to have
happen. It may be a failure of my imagination, but I can't imagine
what it would mean for that to happen without transferring control,
but if it could, I also can't imagine caring.
Tim Rentsch
2023-10-08 22:30:47 UTC
Permalink
Post by James Kuyper
...
Post by Tim Rentsch
Post by James Kuyper
Post by Tim Rentsch
AFAICT the C standard does not say explicitly that a function
call gives or transfers control to the function being called.
Section 6.9.1 is about function definitions,. while 6.5.2.2 is
about function calls, but 6.9.1 has several paragraphs of
specification about what happens when the function is called, and
"After all parameters have been assigned, the compound statement
that constitutes the body of the function definition is executed." (6.9.1p11).
That is one relevant paragraph, but not the only relevant paragraph.
Paragraph 10 of that section says
On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding
parameter as if by assignment. (Array expressions and function
designators as arguments were converted to pointers before the
call.)
Yes, I mentioned that - "several paragraphs of specification about
what happens when the function is called".
Specificaly, paragraphs 10, 11, and 12 give the semantics for when
a function definition is elaborated. Note that the Standard does
not say that these paragraphs are about what happens when the
function is called, but what happens when the function is entered.
It may be that the intention is to say what happens when the
function is called, but the Standard doesn't say that. Paragraph 10
starts "On entry ..."; it does not say "On being called ...".
Post by James Kuyper
Post by Tim Rentsch
What we are looking for is something that says the function is
"entered", that is, that control is transferred.
I find it confusing that you think something more needs to be
specified.
I never said I think that. My guess is you didn't give full
attention to the second paragraph of my first posting (and which
you left out of your reply), which makes clear that I am asking
questions of the OP, and not stating a position of my own. I
don't have a dog in this fight.
Post by James Kuyper
The standard specifies that the compound statement that
constitutes the body of the function gets executed.
No, it doesn't. What the Standard does say is that the function
body (i.e., the compound statement that constitutes the body
thereof) gets executed /if and when the function is entered/.
No entry, no execution. The issue is Does the Standard say the
function is entered (or called)? Apparently the Standard does
not explicitly say that.

Note that every other state-changing construct (or at least those I
could think of: ++, --, assignment, if(), switch(), for(), do,
while(), goto, break, continue, and most notably return) describe
their semantics using a transitive verb (sometimes using the passive
voice, but a transitive verb nonetheless). The semantic description
of function call expressions does not use any such constructions for
the action of calling the function (although there are several
transitive verbs for actions that take place /before/ calling the
function). Because the description of return says "A return
statement terminates execution of the current function and returns
control to its caller", it isn't surprising that some people expect
to find some sort of similar wording about transferring control in
the description of semantics for function call expressions (and in
particular the OP did have such an expectation). But there isn't
any.
Post by James Kuyper
Could you explain how that differs from having control transferred
to the function? Please give an example of what it would mean to
have the compound statement executed without transferring control.
This question is moot because it starts from a false assumption.
Post by James Kuyper
To me, execution of the compound statement is what I want to have
happen.
The question under discussion is about what the C standard says,
not about what anyone wants or expects. Everyone, or at least
every C programmer, expects and wants a function body to be
executed when a function call expression is evaluated - there is
no disagreement about that.
Post by James Kuyper
It may be a failure of my imagination, but I can't imagine what it
would mean for that to happen without transferring control, but if
it could, I also can't imagine caring.
If you get to a point where a conversation doesn't make sense, you
might want to go back and take another look at earlier comments,
to see if there is a point where assumptions on the two sides
diverged, either because someone didn't understand what the other
party was saying or because what someone was trying to say wasn't
said very well and may have been misinterpreted. If that has
happened it's unlikely any progress will be made until the
divergence has been identified and somehow reconciled.

Stefan Ram
2023-10-06 08:54:37 UTC
Permalink
Post by Stefan Ram
during the evaluation of a function call, control is transferred to
the called function, but I was not able to find such wording!
Here is an example from a Java specification
(Java SE 18 Edition):

|15.12.4 Run-Time Evaluation of Method Invocation
...
|and control is transferred to the method code.

.
Loading...