Functions

Programming, for all ages and all languages.
Post Reply
beyondsociety

Functions

Post by beyondsociety »

A function:

Code: Select all

return-type function-name(declarations)
{
         declarations
         statements
         return expression;
}
1) Most of the time you assign the return-type to void when writing c functions. When would you use a different return-type like int, double, etc.?

Return expression

There need to be no expression after return; in that case, no value is returned to the caller. Control also returns to the caller with no value when execution "falls off the end" of the function by reaching the closing right brace. It is not illegal, but probably a sign of trouble, if a function returns a value from one place and no value from the other. In any case, if a function fails to return a value, its "value" is certain to be garbage.

2) What do they mean by this, and when should you use return and when should you not?

3) If the return-type is ommited, int is assumed like in:

Code: Select all

main()
{
}
What is assumed if you add a void to the beginning of main?
Tim

Re:Functions

Post by Tim »

beyondsociety wrote:1) Most of the time you assign the return-type to void when writing c functions. When would you use a different return-type like int, double, etc.?
When you want to return a value to the caller. [tt]void[/tt] doesn't let you return anything directly (although you can still pass values back to the caller via pointers passed as parameters).
2) What do they mean by this, and when should you use return and when should you not?
You must use [tt]return n;[/tt] at least once when your function is not void. Otherwise, use [tt]return[/tt] anywhere where you want to leave the function.
3) If the return-type is ommited, int is assumed like in:

Code: Select all

main()
{
}
Not according to any modern C standard. This behaviour is kept in just so that the old K&R samples will compile with a current compiler. Don't actually do this.
What is assumed if you add a void to the beginning of main?
It is assumed that the author of the program doesn't know C. [tt]main[/tt] returns [tt]int[/tt], always. However, if you don't [tt]return[/tt] anything from main, [tt]return 0;[/tt] is assumed. This is a special case for the [tt]main[/tt] function.
lacrymology

Re:Functions

Post by lacrymology »

beyondsociety wrote: There need to be no expression after return; in that case, no value is returned to the caller. Control also returns to the caller with no value when execution "falls off the end" of the function by reaching the closing right brace. It is not illegal, but probably a sign of trouble, if a function returns a value from one place and no value from the other. In any case, if a function fails to return a value, its "value" is certain to be garbage.

2) What do they mean by this
It means that when a function returns a value it is placed into a certain place for retrieval by the calling function. In the case of the x86 cpu, I believe that integer return valuea are placed in the register EAX (correct me if I'm wrong). If a function is expected to return a value, but does not, then the calling function will still look in EAX (or whichever location is used by the C implementation) for the return value. However, this value will be meaningless to the context involved (and therefore garbage).

-m
beyondsociety

Re:Functions

Post by beyondsociety »

Thanks for the help.
What is assumed if you add a void to the beginning of main? It is assumed that the author of the program doesn't know C. main returns int, always.
Thanks for knocking some sense into me, tim. ;D
Post Reply