Question on Wording

  • Thread starter Paulo Jorge de O. C. de Matos
  • Start date
P

Paulo Jorge de O. C. de Matos

Hello all,

I am trying to understand in detail the correct wording to use in C
for teaching purposes.
For the example:
int foo(int x, int y) {
int k;
...
return k;
}

you could call foo a function or a procedure. On this matter I guess
that the choice is function (K&R2 and C99 standard) given there's no
occurrences of the word procedure there as far as I can tell. Still,
it is unfortunate because you need to explain that these functions are
not in the mathematical sense and so on but it's not a huge issue.

On the other hand, what do you call x? Is it an argument or a parameter?
K&R2 seems to mention both words and I couldn't spot the difference. I
would then assume that "int x, int y" is the argument list/ parameter
list according to what x and y are. But which wording to use?

Moreover, I presented the function definition. The function
declaration would be:
int foo(int x, int y);

but, what if I present the function definition and want to refer only
to the part before the body of the function (compound statement as far
as C99 is concerned)? Shall I call it function declaration (seems
ambiguous since I already used function declaration above) or function
header (this one might create confusion with header files).

I would appreciate input on this.

Best Regards,
 
S

santosh

Paulo said:
Hello all,

I am trying to understand in detail the correct wording to use in C
for teaching purposes.
For the example:
int foo(int x, int y) {
int k;
...
return k;
}

you could call foo a function or a procedure. On this matter I guess
that the choice is function (K&R2 and C99 standard) given there's no
occurrences of the word procedure there as far as I can tell. Still,
it is unfortunate because you need to explain that these functions are
not in the mathematical sense and so on but it's not a huge issue.

On the other hand, what do you call x? Is it an argument or a
parameter? K&R2 seems to mention both words and I couldn't spot the
difference. I would then assume that "int x, int y" is the argument
list/ parameter
list according to what x and y are. But which wording to use?

I would use the word arguments for the actual expressions used within
the parenthesis in a call to a function and use the word parameter to
designate the same in a function declaration.

<http://en.wikipedia.org/wiki/Parameter_(computer_science)>

An alternative is use "formal parameter" and "actual parameter" but
that's cumbersome for quick communication.
Moreover, I presented the function definition. The function
declaration would be:
int foo(int x, int y);

but, what if I present the function definition and want to refer only
to the part before the body of the function (compound statement as far
as C99 is concerned)? Shall I call it function declaration (seems
ambiguous since I already used function declaration above) or function
header (this one might create confusion with header files).

It's a syntax error to present a function definition without it's
associated brace pairs or to present a function declaration without
it's terminating semicolon, there I suggest that you always use either:

T foo(T param) {}

or

T foo(T param);

when presenting examples. If you do want to use a name for it, then
function header seems the most suitable to me. Maybe someone else will
suggest something more correct or better.
 
M

muntyan

Paulo Jorge de O. C. de Matos wrote:







The "parameters" or "formal parameters" are `x' and `y'.
The "arguments" are the values provided by the caller.

... but not all speakers and writers are careful to make
the distinction, so the terms are often (mis)used as if they
meant the same thing. It's the same kind of loose language
as when we say "The argument to strlen() is a string" or
"is a pointer to a string;" it would be more precise to call
it "a pointer to the first char of a string," but it would
also be more tedious to maintain the exactness.


Or just `int foo(int, int);'. And in pursuit of precision
it might be well to note that the `;' is not part of the
declaration.

Yes it is.
For example, consider

int foo(int, int), bar(double), baz(int, const char*);

... a style I would not ordinarily use, but which is legal.

The whole thing is a declaration.
I don't know what advice to offer. The formal grammar of
the language does not define one symbol that encompasses all
and only that part of the definition, so the Standard is no
help in coming up with a suitable name. How pressing is your
need to refer to this part as a quasi-independent unit, rather
than as the introductory part of the function definition? I
guess "preamble" would be as good a word as any, if one must
be found.

I'd avoid "declaration" and "header" for the reasons you
mention, and also because in an old-style function definition
this part can *contain* declarations:

int foo(x, y)
int x; /* declaration */
int y; /* declaration */
{ ... }

int func(int a) - it contains a declaration too, of the
parameter a. It's not called "declaration" in the grammar
yet it declares the parameter (and you'd probably have
hard time pronouncing dash in the "parameter-declaration").
"Function declarator" is good and standard, except one
would need to be silent about old-style parameter declarations
so they don't spoil the picture :)
One needs "declarator" if he wants to explain array
and function pointer declarations anyway.

Yevgen
 
K

Keith Thompson

Eric Sosman said:
The "parameters" or "formal parameters" are `x' and `y'.
The "arguments" are the values provided by the caller.

... but not all speakers and writers are careful to make
the distinction, so the terms are often (mis)used as if they
meant the same thing. It's the same kind of loose language
as when we say "The argument to strlen() is a string" or
"is a pointer to a string;" it would be more precise to call
it "a pointer to the first char of a string," but it would
also be more tedious to maintain the exactness.
[...]

Except that the phrase "pointer to a string" is perfectly correct and
unambiguous. See C99 7.1.1p1:

A _string_ is a contiguous sequence of characters terminated by
and including the first null character. [...] A _pointer to a
string_ is a pointer to its initial (lowest addressed) character.

There's a related error of referring to a pointer to the first element
of an array as a pointer to the array; that's wrong, because a pointer
to an array is a distinct entity of a different type. But since a
string is a data format, not a data type, the standard was free to
provide a definition for the otherwise meaningles phrase "pointer to a
string".
 
S

santosh

Jack said:
No, this is not correct. All of these are function declarations:

1. int foo();

2. int foo(int, int);

3. int foo(x, y)
int x, y;
{ return x + y; }

4. int foo(int x, int y)
{ return x + y; }

It is impossible to define a function without also declaring it, to 3.
and 4. are function declarations as well as definitions. In addition
to being a definition, 3. is a declaration but not a prototype, 4. is
a all of a declaration, a prototype, and a definition.

1. and 2. are declarations that are not definitions, 2. is also a
prototype.

Thank you very much.
 
H

Hallvard B Furuseth

Paulo said:
On this matter I guess
that the choice is function (K&R2 and C99 standard) given there's no
occurrences of the word procedure there as far as I can tell. Still,
it is unfortunate because you need to explain that these functions are
not in the mathematical sense and so on but it's not a huge issue.

Turn it around, into a lecture about the importance of programmer
discipline, trying to be consistent about function parameters, etc.

"Code to execute in C is put in 'functions'. They don't resemble
mathematical functions all that much though. A function can modify what
its parameters point at or global variables, but whether it does so is
not expressed in the function declaration. It can have return type void
which means it doesn't return anything. Some other languages would call
that it a procedure, but as far as C is concerned it's a function. If a
function does return a value, the caller is free to ignore it - even
when doing so would be a bug in the program.

So it's the programmer's job to try to make it visible what is going on,
in particular which function call may modify what. One could e.g. try
to normally make modifiable parameters the first parameters. If a
function needs to return several values, don't just pick any one of them
as the "real" return value and pass the address of the other as a
parameter - it might be more readable to pass the address of both as
parameters, and return nothing or return an error indication. ...etc..."
 
H

Harald van Dijk

I recall once,
that upon my mentioning that function definitions are the only
declarations that are not semicolon terminated, that it was pointed out
to me that function definitions do not fit the standard's description of
what declarations are.

I believe that it is because function definitions are not semicolon
terminated.

It's because function definitions are not allowed inside of another
function. A translation unit is made up of declarations, mixed with
function definitions. Declarations may be omitted, function definitions
may be omitted, but at least one of either must be present. This could
have been specified as

translation-unit:
declaration
function-definition
translation-unit declaration
translation-unit function-definition

but it's just as easy to specify it as

translation-unit:
declaration-or-function-definition
translation-unit declaration-or-function-definition
 
H

Harald van Dijk

It's because function definitions are not allowed inside of another
function. A translation unit is made up of declarations, mixed with
function definitions. Declarations may be omitted, function definitions
may be omitted, but at least one of either must be present. This could
have been specified as

translation-unit:
declaration
function-definition
translation-unit declaration
translation-unit function-definition

but it's just as easy to specify it as

translation-unit:
declaration-or-function-definition
translation-unit declaration-or-function-definition

Apologies, I hit Send too soon.

declaration-or-function-definition was named external-declaration, which
just means that:

external-declaration:
declaration
function-definition

The alternative would have been to make function-definition a part of
declarations directly: it's now

declaration:
declaration-specifiers init-declarator-list/opt/ ;

and it would become

declaration:
declaration-specifiers init-declarator-list/opt/ ;
function-definition

However, doing this would syntactically allow function definitions
_anywhere_ declarations are allowed. In particular, it would allow
function definitions inside of other function definitions. While it could
then be explicitly prohibited, it's as easy to simply not allow them in
the first place.
 
D

David Thompson

Paulo Jorge de O. C. de Matos said:
I am trying to understand in detail the correct wording to use in C
for teaching purposes. <snip>
you could call foo a function or a procedure. On this matter I guess
that the choice is function (K&R2 and C99 standard)

Right. That isn't just K&R/C99 terminology - it's general C programmer
terminology too. <snip>
Concur.
On the other hand, what do you call x? Is it an argument or a parameter?

It's a parameter. ... Here's an argument: [in the call]

<Python> No it isn't. Yes it is. No it isn't, mere contradiction is
(It was already a compound-statement in C89.)
It's called a "function declarator".

Not quite. The function declarator is foo(int x,int y) or foo(x,y) and
does not include the (return) type-specifier(s) mandatory in C99 and
optional-recommended in C89, and the 'storage-class' (linkage) if any.
And for more complicated return types, it leaves out more; the
standardly convenient infamous example is signal().

I think the OP is correct there is no good common syntax term for just
the 'visible' (to file scope) part of the function definition. I would
use 'function header' in spite of the drawback he mentioned;
'preamble' just doesn't feel right to me. OTOH the _semantics_ of this
element, namely the (full) type declared for the function, is commonly
(though not standardly) called the signature, a term which has the
advantage of not being used for anything else in this area.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top