Functions
Posted: Mon Aug 04, 2003 9:37 am
A function:
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:What is assumed if you add a void to the beginning of main?
Code: Select all
return-type function-name(declarations)
{
declarations
statements
return expression;
}
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()
{
}