Function Call
- A Function can be accessed i.e. called by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas.
- If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function.
- The arguments within the function call are called actual parameters or actual arguments.
- The actual arguments may be constants, variables or more complex expressions.
- Each actual argument must be of the same type as its corresponding formal arguments.
- Remember that it is the value of each actual arguments that is transferred into the function and assigned to the corresponding formal arguments.
- The general form of function call is given below:
fun (a,b);
- Where fun -> is the name of the function.
- a, b -> are the actual arguments within the parentheses.
- There will be no return value in this function call and it ends with;
- If the function returns a value, then the function call is often written as an assignment statement e.g.
a = area (rad);
or
s = sum (n1, n2);
- These functions will return a value and assigned to a and s respectively.
- On the other hand if the function does not return anything, the function call will be e.g.
message ();
Return Statement
- The general form of return statement is given below:
return (exp);
which can be a constant, variable or an expression, Parentheses around exp are optional.
C Language Notes
- The return statement is used to serve two purposes:
- return statement immediately transfers the control back to the calling program.
- It returns the value after return to the calling program.
- The return statement need not always be at the end of the called function.
- Also there is no restriction on the number of return statements that are present in the function.
- But there is one limitations of return i.e. it can return only one value.
Example:
big (int a, int b)
{
if (a > b)
return(a);
else
return(b);
}
- In the function, if the value of a is greater than b then a will be returned otherwise b will be returned to calling function.