Vous êtes sur la page 1sur 3

FUNCTIONS / USER DEFINED FUNCTIONS Decomposing a problem into modules is known as modularization. Each module is known as a sub-program or function.

Definition : A function is self-contained program segment, that carries out some specific welldefined task. Functions are divided into two categories. 1) Library function 2) User defined functions. 1)Library functions: These are built in functions, included in the compiler. For example Sqrt(), abs(), strlen() etc., 2) User defined function: Apart from the library functions, user can define functions to perform a specific task. Such functions are called user-defined functions. Parts of a function / components of a function 1) definition of a function (function declarator) 2) function prototype 3) function call 4) actual and formal arguments 5) return statement Caller void main() { f = fact(n); } Callee int fact(int n) { }

Function definition / DECLARATION OF A FUNCTION The general form of a function definition is data_type function_name (data_type1 arg1, data_type2 arg2, ... ) { body of the function return(expression); } Here data type refers to the type of value returned by the function. ( to the calling portion ie., caller ). It may be simple data type such as int, long int, float, char or user defined data type such as structures. If a function returns no value, then it is declared as the type "void". void is a key word in C. Function name is any valid identifier. Arg1,arg2 ... are known as a formal arguments / parameters. Formal parameters are optional. Body of the function contains local variable declaration and action statements to solve the problem ( task ). They must be enclosed with in the braces. RETURN STATEMENT: Return statement causes an immediate exit from the function. Return statement is used to return a value to its caller. return statement returns only one value to the calling program (caller). Its general form is return(expression); (or) return;

Expression may be a variable, constant or a expression. If return statement is omitted the control is transferred back to the calling program with out carrying any value. Local variables: Variables declared in side of the function are known as local variables / internal variables. Local variables must be accessed only in the function where they are declared. Note: Variables declared in main() function are also called local variables. FUNCTION CALL / ACCESSING A FUNCTION: To execute the statements of a function, its name must be specified in the caller function (main () ). When the function call is executed, then control is transferred to the corresponding function, after executing the statements in that function control will be transferred back to the caller (reference point). The format of the function call is function name(); function name(actual arg1, arg2,.....arg-n); The first form can be used when there are no arguments. In the second form arguments are included, these are called as actual parameters / arguments. Actual and formal arguments Arguments included in the function call are called as actual arguments. When a function is called, actual parameters are assigned (copied) to their corresponding formal parameters. Actual parameters may be expressed as constants, variables, expressions. Function uses formal arguments for computing purpose (processing). Actual and formal parameters must be same in type, number and order. Names of actual and formal parameters need not to be same NOTE: A 2)cout statement function call may 3) arithmetic statement be used in 1) assignment statement

FUNCTION PROTOTYPE: Function prototype provides the following information to the complier. Name of the function, return type of the function and the number and type of arguments. It facilitates error checking between function call (caller) and function definition line (callee). The function prototype declaration statement is always terminated with semi-colon. The general form of the function prototype is return_type function_name(argument list); Eg: 1)int factorial(int n); 2) float average( int a, int b); In a function declaration, the names of the arguments are dummy variables, therefore names are optional. Eg: 1) int factorial(int); 2) float average(int,int); Note: 1) In c++ function prototype is compulsory, even though the function return integer value 2) If the function is defined before the main program then the prototype is optional. CATEGORIES OF A FUNCTION: 1)function with arguments and returns values 2)function with arguments and no return values

3)function without arguments and no return values ADVANTAGES OF A FUNCTION: 1) Support for modular programming. 2) Functions provide logical clarity. 3) Functions are easy to write, understand, modify and debug. 4) Reduction in program size, because of code reusability. 5) Duplication can be avoided. 6) A function may call another function or itself. RECURSION: The ability of calling the function itself repeatedly until a condition is satisfied is called 'recursion'. Two important conditions must be satisfied by any recursive functions. 1)There must be a terminating condition, to stop the process. 2) Each recursive call must be close to the terminating condition. When the recursive program is executed, the recursive function calls are not evaluated immediately. They are placed on a stack until the terminating condition is encountered. After satisfying the terminating condition the function calls are executed in reverse order, because the data structure stack is involved. Example : //recursive function to find factorial long int factorial(int n) { if(n==1) return(1); else return(n*factorial(n-1)); }

Vous aimerez peut-être aussi