Home : C Language : Functions
Functions are declared thus:
- type
function_name(typeparameter_name,typeparameter_name){- any local variable declarations
;- the code of the function
;- [
return[expression]];}
And are called from within an expression thus:
return_value
=function_name(parameter,parameter)
If the function returns no value, the type name void may be used to indicate this:
voidfunction_name(typeparameter_name,typeparameter_name)
Since an expression in C does not have to be assigned to anything, the following statement to call the function is still valid:
function_name
(parameter,parameter)
Similarly, if the function has no parameters, the type name void may be used to indicate this:
type
function_name(void)
A function with no parameters must be called using the form:
return_value
=function_name()
function_name, with no parentheses, returns the address of the entry point of the function.
By default, all parameters (except for arrays) are passed by value to a function. They appear as local variables, and if modified do not affect the variable which was passed.
To enable a variable to be modified within a function, its address must be passed instead:
return_value
=function_name(&variable)
and the function declared as taking a pointer parameter:
- type
function_name(type*parameter_name){*parameter_name=value to set variable to}
Note that unindexed arrays always have their address passed anyway, so
the & operator is optional.