calling functions in C

Y

Yuan Zhong

Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.

Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

thanks.
 
E

Emmanuel Delahaye

Yuan Zhong wrote on 26/07/04 :
Can someone please explain me what's going on during a call to a
function.

The execution is detoured to a subroutine and resumed once the end of
the function or 'return' is reached. The values of the parameters are
redable from the fuction. Typed pointer parameters can be dereferenced.
A value can be returned.
Specifically, I wanted to know what's going on in Stacks.

It depends on the implementation. Some have no stack.
Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

It's an undefined behaviour. The result can be anything at all.
 
D

Darrell Grainger

Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.

There is no one answer to this for C language. There is flexibility in how
each person creates their C compiler. You could ask how is it implemented
on your compiler. For that question you'll need to find a newsgroup that
deals with your specific compiler. For example, there are newsgroups for
the gcc compiler where you could ask this.
Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

No. Any good compiler will not compile the code if you do this. The only
exception is when the function prototype has ... in it. For example,
printf prototype is:

int printf(const char *, ...);

This means it can take one or more input parameters. How many input
parameters depends on the format of the first parameter. For example, if
the first parameter is "%d %d %d" then it will need three more inputs (one
for each %d).
 
T

Thomas Matthews

Yuan said:
Hi, Can someone please explain me what's going on during a call to a
function. Specifically, I wanted to know what's going on in Stacks.
In some compilers, the parameters are "pushed" onto a stack. Other
implementations pass parameters via registers.

Upon a return, some implementations place the return value on the
stack. The popping of arguments off of the stack may be the
responsibility of the calling protocol or the function's protocol.

Some implementations can pass return values via registers.

Why is it ok to pass only 2 parameters or 5 parameters when the function
prototype requires 3.

thanks.
Show the code.

In C++, parameters can be assigned default values. So that if the
parameter is not supplied by the caller, the function receives
the default value. But, that is an issue for another newsgroup.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
Y

Yuan Zhong

: Yuan Zhong wrote:
:> Hi, Can someone please explain me what's going on during a call to a
:> function. Specifically, I wanted to know what's going on in Stacks.
:>
: In some compilers, the parameters are "pushed" onto a stack. Other
: implementations pass parameters via registers.

: Upon a return, some implementations place the return value on the
: stack. The popping of arguments off of the stack may be the
: responsibility of the calling protocol or the function's protocol.

: Some implementations can pass return values via registers.


:> Why is it ok to pass only 2 parameters or 5 parameters when the function
:> prototype requires 3.
:>
:> thanks.
:>
: Show the code.

This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}


int main(void)
{


char *a="hello", *b="hello, world\n";
char *(*f)();


f=&cmp_string;

printf("the longer one is %s", f(a,b,"hello","blah blah"));
printf("the longer one is %s", f(a));

return 0;

}

: In C++, parameters can be assigned default values. So that if the
: parameter is not supplied by the caller, the function receives
: the default value. But, that is an issue for another newsgroup.


: --
: Thomas Matthews

: C++ newsgroup welcome message:
: http://www.slack.net/~shiva/welcome.txt
: C++ Faq: http://www.parashift.com/c++-faq-lite
: C Faq: http://www.eskimo.com/~scs/c-faq/top.html
: alt.comp.lang.learn.c-c++ faq:
: http://www.raos.demon.uk/acllc-c++/faq.html
: Other sites:
: http://www.josuttis.com -- C++ STL Library book
 
E

Emmanuel Delahaye

Yuan Zhong wrote on 26/07/04 :
This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}

int main(void)
{

char *a="hello", *b="hello, world\n";
char *(*f)();

This is a dangerous game (a sin actually). It's a function declaration
(unspecified number and types of parameters). Should be a prototype:

char *(*f)(char *a, char *b);

Now, the compiler will whine.
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

: Yuan Zhong wrote:
:> Hi, Can someone please explain me what's going on during a call to a
:> function. Specifically, I wanted to know what's going on in Stacks.
:>
: In some compilers, the parameters are "pushed" onto a stack. Other
: implementations pass parameters via registers.

: Upon a return, some implementations place the return value on the
: stack. The popping of arguments off of the stack may be the
: responsibility of the calling protocol or the function's protocol.

: Some implementations can pass return values via registers.


:> Why is it ok to pass only 2 parameters or 5 parameters when the function
:> prototype requires 3.
:>
:> thanks.
:>
: Show the code.

This is code I tested under gcc3.3.2
It compiles successfully whether I pass one parameter or more than two.
Any explainations? Thanks.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char* cmp_string(char *a, char *b)
{
return (strlen(a)>strlen(b)?a:b);
}


int main(void)
{


char *a="hello", *b="hello, world\n";
char *(*f)();
a function with the prototype char *func();
is a function that takes _any_ number of parameters, and is
very diffrent from char *func(void);
 
A

Arthur J. O'Dwyer

Use <> angle brackets around the standard headers.

For future reference: '(const char *a, const char *b)' would have
been more appropriate, since this function doesn't need to modify
the things to which its parameters point.
a function with the prototype char *func();
is a function that takes _any_ number of parameters, and is
very diffrent from char *func(void);

A function with the declaration 'char *func();' has /no/ prototype,
and is a function that takes a currently /unspecified/ number of
parameters. It is compatible with, but not precisely equivalent to,
'char *func(void);'.

In other words,

char *func();
func(a);
func(a,b);

is guaranteed to be invalid C code, but the compiler is not actually
required to diagnose the error. Many compilers /do/ diagnose such
errors, if at all possible. In any event, my example above produces
undefined behavior (in one of the two function calls above, depending
on the real, actual type of 'func').

For the OP's benefit: Declare every variable with the most specific
type you can, and you'll never have to worry about these nitpicky
areas of the language.

HTH,
-Arthur
 
A

Alex Fraser

Arthur J. O'Dwyer said:
A function with the declaration 'char *func();' has /no/ prototype,
and is a function that takes a currently /unspecified/ number of
parameters. It is compatible with, but not precisely equivalent to,
'char *func(void);'.

In other words,

char *func();
func(a);
func(a,b);

is guaranteed to be invalid C code, but the compiler is not actually
required to diagnose the error. Many compilers /do/ diagnose such
errors, if at all possible. In any event, my example above produces
undefined behavior (in one of the two function calls above, depending
on the real, actual type of 'func').

ITYM in _at least_ one of the two function calls above.

Alex
 
G

Guillaume

In other words,
Not necessarily.

Function "func()" could actually be implemented in such a way that
it will be perfectly ok to call it with a variable number of parameters.

Since parameters are pushed onto the stack by the caller in C, much
can be done. ;-)

Of course, this is not good practice.
If you intend to write a function with variable parameters, use the
proper prototype using "..." where it is needed.

But still, your claim that the above code was *guaranteed* to be invalid
was a bit exxagerated.
 
E

Eric Sosman

Guillaume said:
>
Since parameters are pushed onto the stack by the caller in C, much
can be done. ;-)

No; the argument values are transmitted from the call
to the called function by a flock of carrier pigeons, as
described in RFC 2549.

(In other words, the C Standard requires that the
function's formal parameters somehow acquire the values
of the argument expressions, but says nothing at all
about how this is accomplished. Some machines use a
stack, some use registers, some use a mixture of both,
and some use Black Magic.)
 
C

Christopher Benson-Manica

Arthur J. O'Dwyer said:
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

Just curious, why the sudden switch to MIME-encoded posts?
Unintentional, perhaps?
 
A

Arthur J. O'Dwyer

Just curious, why the sudden switch to MIME-encoded posts?
Unintentional, perhaps?

Apparently so. :( I checked some other recent posts of mine, and
didn't find the "multipart/mixed" type. But CMU Computing Services
was updating the system recently, so maybe it was related. Seems
like it's fixed now.

Thanks for the heads-up, though; I wouldn't have noticed otherwise.

-Arthur
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top