You are on page 1of 3

Functions Definition ----------A program implements a task, that is a collection of subtasks.

A function is a module that implements a subtask of the program. Design ------A function is made up of * signature * body A * * * functions signature is made up of return type function name (must be unique) parameters

A functions body contains the code (logic) that resolves the subtask. Invokation ----------Program execution starts with main() function. In main() we have code and calls to other user defined functions. The user defined functions may also call other functions. As a function is called program control transfers from the line of call to the definition of the function. Next the code of the function executes. On completion program control returns to the statement following the call to function. Parameters ----------Variables defined inside a function are local to the function. And can be used in the function only. Local variables of a function are by default not accessible in other functions of the prorgam. If variables of one function are to be used in another function then it can be implemented through "parameters". Parameters are values passed from one function to another along with the function call.

Parameters are categorized as * Actual Parameters * Formal Parameters Actual Parameters are the parameters that are passed along with the function call. Formal Parameters are the parameters that are received by the called function. Actual and formal parameters must match in * count * datatype * sequence Formal parameter variable is a copy of the actual parameter variable. Any changes made to the formal parameter by the called function, remain limited to the formal parameter only and never affect the actual parameter of the caller function. This concept is called as "PASS BY VALUE". return ------Known that on invokation the code of a function executes. The processing done by the function may result in some value (answer). Generally such values are stored in local variables of the function. Such local variables can be accessed in the function itself and not in other functions. If a value generated by a called function is to be given to the caller function then it can be done through "return" statement. A return statement * Terminates the current function. * Transfers the program control back to the caller function. * If a single value is associated with "return" statement then that value is also transferred from the current function to the called function. If a function associates a value with the return statement then the datatype of that value must be specified in the functions signature. If a function doesnt return any value then a special datatype called as "void" must be specified in the functions signature.

Note : It is optional for the caller function to receive the value returned by the called function. Note : In C "int" is the default return type of every function. Arrays as Parameters -------------------Arrays defined inside a function are local to the function. And can be used in the function only. Local arrays of a function are by default not accessible in other functions of the prorgam. If arrays of one function are to be used in another function then it can be implemented through "parameters". Parameters are values passed from one function to another along with the function call. Parameters are categorized as * Actual Parameters * Formal Parameters Actual Parameters are the parameters that are passed along with the function call. Formal Parameters are the parameters that are received by the called function. Actual and formal parameters must match in * count * datatype * sequence Formal parameter array refers to the same memory as of the actual parameter array (It is not a copy). Any changes made to the formal parameter by the called function, affect the actual parameter of the caller function. This concept is called as "PASS BY REFERENCE".

You might also like