Velocity Reviews > Strange function use

# Strange function use

Christian Christmann
Guest
Posts: n/a

 08-03-2006
Hi,

in a benchmark I've found an uncommon use of a function.
This is the simplified form:

1 int foo( int f )
2 {
3 return f;
4 }
5
6 int main( void )
7 {
8 int a, b, foo();
9 a = 10;
10 b = foo( a );
11
12 return 0;
13 }

I don't understand the use of function "foo" in line 8.
What's it's purpose? It's called without assigning it's
return value to any variable. Furthermore, I wonder
why this function use is allowed at all. According to
the function prototype "foo" expects an integer argument
that is in line 8 not given. Compiling the code with
"gcc -Wall -ansi" does not issue any warning/errors.

Could you shed some light on that.

Regards,
Chris

pemo
Guest
Posts: n/a

 08-03-2006
Christian Christmann wrote:
> Hi,
>
> in a benchmark I've found an uncommon use of a function.
> This is the simplified form:
>
> 1 int foo( int f )
> 2 {
> 3 return f;
> 4 }
> 5
> 6 int main( void )
> 7 {
> 8 int a, b, foo();
> 9 a = 10;
> 10 b = foo( a );
> 11
> 12 return 0;
> 13 }
>
> I don't understand the use of function "foo" in line 8.
> What's it's purpose? It's called without assigning it's
> return value to any variable. Furthermore, I wonder
> why this function use is allowed at all. According to
> the function prototype "foo" expects an integer argument
> that is in line 8 not given. Compiling the code with
> "gcc -Wall -ansi" does not issue any warning/errors.
>
> Could you shed some light on that.

It's not a /call/ but a declaration - it's say that foo is a function
returning an int. I believe it's also /saying/ that foo takes an unknown
number/type of args - however, as foo's definition is in scope, the compiler
already knows about it ... thus the declaration in line 8 isn't necessary.

--
==============
Not a pedant
==============

lovecreatesbeauty
Guest
Posts: n/a

 08-03-2006

Christian Christmann wrote:
> 1 int foo( int f )
> 2 {
> 3 return f;
> 4 }
> 5
> 6 int main( void )
> 7 {
> 8 int a, b, foo();

int a, b, foo( int );

> 9 a = 10;
> 10 b = foo( a );
> 11
> 12 return 0;
> 13 }
>
> I don't understand the use of function "foo" in line 8.
> What's it's purpose? It's called without assigning it's
> return value to any variable. Furthermore, I wonder
> return value to any variable. Furthermore, I wonder
> why this function use is allowed at all. According to
> the function prototype "foo" expects an integer argument
> that is in line 8 not given. Compiling the code with
> "gcc -Wall -ansi" does not issue any warning/errors.

int foo( ); /*int foo( int );*/

It's not a function use/call, foo() occurs inside a declaration. It's a
function prototype declaration.

lovecreatesbeauty
Guest
Posts: n/a

 08-03-2006
Christian Christmann wrote:
> 1 int foo( int f )
> 2 {
> 3 return f;
> 4 }
> 5
> 6 int main( void )
> 7 {
> 8 int a, b, foo();

int a, b, foo(); /*A*/
int a, b = foo(); /*B*/

> 9 a = 10;
> 10 b = foo( a );
> 11
> 12 return 0;
> 13 }

Differentiate line A and B. A function call occurs at line B.

Christian Christmann
Guest
Posts: n/a

 08-03-2006
On Thu, 03 Aug 2006 02:51:39 -0700, lovecreatesbeauty wrote:

>
> Christian Christmann wrote:
>> 1 int foo( int f )
>> 2 {
>> 3 return f;
>> 4 }
>> 5
>> 6 int main( void )
>> 7 {
>> 8 int a, b, foo();

>
> int a, b, foo( int );
>
>> 9 a = 10;
>> 10 b = foo( a );
>> 11
>> 12 return 0;
>> 13 }
>>
>> I don't understand the use of function "foo" in line 8.
>> What's it's purpose? It's called without assigning it's
>> return value to any variable. Furthermore, I wonder
>> return value to any variable. Furthermore, I wonder
>> why this function use is allowed at all. According to
>> the function prototype "foo" expects an integer argument
>> that is in line 8 not given. Compiling the code with
>> "gcc -Wall -ansi" does not issue any warning/errors.

>
> int foo( ); /*int foo( int );*/
>
> It's not a function use/call, foo() occurs inside a declaration. It's a
> function prototype declaration.

But it's a declaration that deviates from the declaration
given in line 1. There's no function "foo" defined without
any parameters, so why is the declaration from line 8
accepted by a compiler?

pemo
Guest
Posts: n/a

 08-03-2006
Christian Christmann wrote:
> On Thu, 03 Aug 2006 02:51:39 -0700, lovecreatesbeauty wrote:
>
>>
>> Christian Christmann wrote:
>>> 1 int foo( int f )
>>> 2 {
>>> 3 return f;
>>> 4 }
>>> 5
>>> 6 int main( void )
>>> 7 {
>>> 8 int a, b, foo();

>>
>> int a, b, foo( int );
>>
>>> 9 a = 10;
>>> 10 b = foo( a );
>>> 11
>>> 12 return 0;
>>> 13 }
>>>
>>> I don't understand the use of function "foo" in line 8.
>>> What's it's purpose? It's called without assigning it's
>>> return value to any variable. Furthermore, I wonder
>>> return value to any variable. Furthermore, I wonder
>>> why this function use is allowed at all. According to
>>> the function prototype "foo" expects an integer argument
>>> that is in line 8 not given. Compiling the code with
>>> "gcc -Wall -ansi" does not issue any warning/errors.

>>
>> int foo( ); /*int foo( int );*/
>>
>> It's not a function use/call, foo() occurs inside a declaration.
>> It's a function prototype declaration.

>
> But it's a declaration that deviates from the declaration
> given in line 1. There's no function "foo" defined without
> any parameters, so why is the declaration from line 8
> accepted by a compiler?

I think it's because you're saying that it can take any number/type of
args - and that doesn't conflict with the definition - which shows it takes
one arg of type int.

Change line 8 to, say >>> 8 int a, b, foo(float);

and then there'll be a conflict.

--
==============
Not a pedant
==============

spibou@gmail.com
Guest
Posts: n/a

 08-03-2006
pemo wrote:

> Christian Christmann wrote:
> > Hi,
> >
> > in a benchmark I've found an uncommon use of a function.
> > This is the simplified form:
> >
> > 1 int foo( int f )
> > 2 {
> > 3 return f;
> > 4 }
> > 5
> > 6 int main( void )
> > 7 {
> > 8 int a, b, foo();
> > 9 a = 10;
> > 10 b = foo( a );
> > 11
> > 12 return 0;
> > 13 }
> >
> > I don't understand the use of function "foo" in line 8.
> > What's it's purpose? It's called without assigning it's
> > return value to any variable. Furthermore, I wonder
> > why this function use is allowed at all. According to
> > the function prototype "foo" expects an integer argument
> > that is in line 8 not given. Compiling the code with
> > "gcc -Wall -ansi" does not issue any warning/errors.
> >
> > Could you shed some light on that.

>
> It's not a /call/ but a declaration - it's say that foo is a function
> returning an int. I believe it's also /saying/ that foo takes an unknown
> number/type of args - however, as foo's definition is in scope, the compiler
> already knows about it ... thus the declaration in line 8 isn't necessary.

What are the semantics of declaring a function inside
another function ?

pemo
Guest
Posts: n/a

 08-03-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> pemo wrote:
>
>> Christian Christmann wrote:
>>> Hi,
>>>
>>> in a benchmark I've found an uncommon use of a function.
>>> This is the simplified form:
>>>
>>> 1 int foo( int f )
>>> 2 {
>>> 3 return f;
>>> 4 }
>>> 5
>>> 6 int main( void )
>>> 7 {
>>> 8 int a, b, foo();
>>> 9 a = 10;
>>> 10 b = foo( a );
>>> 11
>>> 12 return 0;
>>> 13 }
>>>
>>> I don't understand the use of function "foo" in line 8.
>>> What's it's purpose? It's called without assigning it's
>>> return value to any variable. Furthermore, I wonder
>>> why this function use is allowed at all. According to
>>> the function prototype "foo" expects an integer argument
>>> that is in line 8 not given. Compiling the code with
>>> "gcc -Wall -ansi" does not issue any warning/errors.
>>>
>>> Could you shed some light on that.

>>
>> It's not a /call/ but a declaration - it's say that foo is a function
>> returning an int. I believe it's also /saying/ that foo takes an
>> unknown number/type of args - however, as foo's definition is in
>> scope, the compiler already knows about it ... thus the declaration
>> in line 8 isn't necessary.

>
> What are the semantics of declaring a function inside
> another function ?

A declaration of a function is just a /prototype/ - ok?

--
==============
Not a pedant
==============

spibou@gmail.com
Guest
Posts: n/a

 08-03-2006
pemo wrote:

> (E-Mail Removed) wrote:

> > What are the semantics of declaring a function inside
> > another function ?

>
> A declaration of a function is just a /prototype/ - ok?

Let me rephrase. If foo2 is declared inside foo1 are you
allowed to call foo2 only inside foo1 ? If this is so then
since you cannot define foo2 inside foo1 then you cannot
call it at all (you can call some function with the same name
but not the one you declared) which basically means that
the declaration was a waste of time. Have I got it right ?

> ==============
> Not a pedant
> ==============

Hmmmmmm.

Spiros Bousbouras

Eric Sosman
Guest
Posts: n/a

 08-03-2006
pemo wrote:

>>>Christian Christmann wrote:
>>> [...]
>>>>8 int a, b, foo();
>>> [...]

>
> A declaration of a function is just a /prototype/ - ok?

No function declaration "is" a prototype, and this
declaration "has" no prototype.

The "prototype" is the description of the function's
argument list, either in a declaration or a definition (a
definition also declares). The prototype part is

int bar(double trouble);
^^^^^^^^^^^^^^

void baz(int x)
^^^^^
{
printf ("%d\n", x);
}

void xyzzy(void);
^^^^

void plugh(const char*, ...);
^^^^^^^^^^^^^^^^

An "old-style" or "K&R" declaration like `int foo();' has
no prototype. Nor does an old-style definition:

double avg(x, y)
double x, y;
{
return (x + y) * 0.5;
}

(Why is it important to keep the nomenclature straight?
Because C's declarations are a source of confusion already,
and we should not add to it by misusing the terminology. If
I feel like calling `extern int x;' a "prototype" for the
variable x, no one will stop me -- but no one will appreciate
my muddying of the waters, either. That's why I never call
`extern int x;' a "prototype," but use the term "beta."

--
Eric Sosman
(E-Mail Removed)lid