Você está na página 1de 6

Functions Function: Function is self contained block of statements that specifies one or more actions to be performed for large

program. Functions are sub program. The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use. Function are of two types 1.Library functions 2.Use defined functions Library function: The library function are predefined functions and supplied along with compiler and these can be used in c program.This function are not required to be written by us. Eg: printf scanf sqrt getch

User define function: This function as to be developed by the user at the time of writing a program.

Eg:main() main is specially used function in C. Ever program must have main function to indicate, where the program begins its execution. Main function invokes other function within it.exprintf,scanf,getch A function that invokes another function is known as calling function A function which is invoked is termed as called function

Advantages of functions: Main advantage is it allows us to nreak a large ,complex task into series of smaller ones Removes the need to duplicate the code A function may be used any number of times by any number of other programs It felicitate top down modular program. It is easy to locate and isolate and fault function easily A function can used by many other program, it means programmer built an what have already done, insert of starting over scratch. The inner details of functions are invisible A function can call itself or any other function

A function can be put at before or after calling function

Similarities between variable and function Both function and variable names are identifiers,They need to follow rules for identifier Like variables,function have types associated with them Like variables.function names must be declared and defined before they are used. The 3 components associated with functions are:

1.The function definition 2.Function call 3.The Calling Statement

Function definition is independent programmodule that is specially written to implement the requirements of function.in order to usethe function we need to call the function.Thisis known as function call The function should be declared like other variables before use.

Definition of functions Defintion of function defines the work or action taken within the function body Function name Function type List of parameters Local variable Function statements Return statements

This six elements are grouped into two parts. Function header(first three elements) Function body(next three elements)

Syntax Function_typefunction_name(parameter_list) { Local variable declaration; Statements 1; Statements 2; . .. Return statement; }

Function name Function name is any valid C identifier and therefore it must follow sames rules of formation as other variables names in C.The names shouldbe appropriate to task performed by function.

Function type Function type specifies type of value that a function is expected to return to calling function.Ifretrn type is not exlicityspecified,c will assume as integer type.If function is not returning anything,then we need to specify return type as VOID

Formal parameters list

The parameters list declares the variable that will receive data sent by calling program.They serve as input data to function to carry out specified task. Ex Intadd(inta,int b) { .} Float temperature(float cent) {.} Float mul(float x,float y) {..}

Illegal Intadd(inta,b) {..} A function may not recieves values from calling program.In such cases,function have no parameters.To indicate that it is empty,we use keyword void between

Ex

Void printline(void)

Function body Function body contains the declaration and statements necessary for performing the required task.The body is enclosed in bracescontains three parts Local declarations that specify variables needed by function Function statements that perform task of function A return statement that returns value evaluated by function

When we dont have to return any values then we dont specify the return statements. Return value and their types A function may or may not send back any value to calling function.If it need to return any value,it is done through returnstatement.It is possible to pass to called function any number of values,the called function can return only one value per call Syntax return; Or return(expression); Ex If(error) Return; intmul(intx,int y) { Int p; P=x*y; Return(p); } A function may have more than one return value if(x=0) return(0); else return(1); Function call A function call is specified by function name followed by parameters enclosed inparanthesis and terminated by semicolon.We dont mention return in function call.

When complier encounters the function call statement,it causes the control transferred to first statement in function body and after execution of function body the control is transferred to statement following to function call. Void main() {int y; Y=add(10,5); Printf(%d,y);} Function Declaration Like variables,all function must be declared ,before they are used.Such declaration is known as function declaration or function prototype..Functiondeclaration consist of four parts

Function type Function name Parameters list Terminating semicolon

Syntax return_typefunction_name(parameters-1,parameters-2,..,parameters-n); Function prototyping is one of key feature added to c function.When a function call is encountered,the compiler checks the function call with prototype so that correct parameters types are used. Return_type specifies the type of value returned at end of function.If there is no return value,a keyword Function type Function type specifies type of value that a function is expected to return to calling function.Ifretrn type is not exlicityspecified,c will assume as integer type.If function is not returning anything,then we need to specify return type as void before function name.

Ex Intadd(inta,int b); Float mul(float a,float b);

Você também pode gostar