Вы находитесь на странице: 1из 18

FUNCTION IN C

SUMITED BY : Arjuna maharana MCA RAAJDHANI ENGINEERING COLLEG

Contents
1. Introduction
Function

2. 3. 4. 5. 6. 7.

Types of function Elements of function Categories of function Scope of function Why we use Function Importance

Function
Definition:A function is a self contained block of statement which have a special task inside a program. Types of function:1. library Function 2. User define Function

Library Function:Library functions are those functions which are already define inside the compiler. For example- Printf(), Scanf(), getch() etc

Program:-

Void main() { Int i, j; Scanf(%d%d,&I, &j); Printf(%d%d,I,j); }

User define function:User define functions are those which are defined by the user.

Elements of User define function:The basic elements of user define functions are: Function definition Function declaration Function call

Function Definition:The definition defines the working of function and the type of value that should be return.
Elements of function definition:-

1.Function type:Function type specifies the type of value like float, integer, double. That means the function is expected to return; if the return type is not specify then it will assume that of integer. 2.List of parameter:The parameter lists declares the variables that will receives the data send by the main program. This parameter is known as formal parameter.

Function Name:The function name identifies the particular function that is used inside the program. Local variable:The local variable declaration specifies the variable needed by the function. Function statement:The function statement specifies the statement which is used inside the function for executing the in the program. Return statement:Return statement is statement that returns by function.

Function Declaration:A function declaration is also known as function prototype. It consists of 4 parts.
Function type Function name List of parameter Terminating semicolon

General form of function declarationFunction_type function_name (List of paramaters); Example-

int sum(int , int);

function call:A function can be called by using simply the function name follow by list of actual paramaters and semicolon.

Example:-

int sum(int, int); /prototype void main() { int a,b; scanf(%d%d,&a,&b); printf(%d,sum(a,b)); /calling } int sum(int e,int w) /definition { return(a+b); }

Category of functions:Depending on whether the arguments are present or not or a value is return or not. It is divided into 5 types.
Function with no argument & no return value. Function with argument but no return value. Function with argument & one return value. Function with no argument but return value. Function that return multiple value.

Function with no argument & no return value.


When a function has no argument and no return type, it does not receive any data from the calling function to called function also dont return a value to calling function.

For example:-

Sum(); Void main() { Sum(); } Sum() { Int a,b,c; Scanf(%d%d,&a,&b); C=a+b; Printf(%d,c); }

Function with argument but no return value:When a function has argument and no return type, then it receive any data from the calling function to called function but dont return a value to calling function. Example:Int Mul(int,int); Void main() { Int a,b; Scanf(%d%d,&a,&b); Mul(a,b); } Int Mul(int a, int b) { Printf(a+b=,a+b) ; }

Function with argument & one return value:When a function has argument and return type, then it receive any data from the calling function to called function also return a value to calling function. Example:Int Mul(int,int); Void main() { Int a,b; Scanf(%d%d,&a,&b); Printf(%d,Mul(a,b)); } Int Mul(int a, int b) { Return(a+b) ; }

Function with no argument but return value:when a function has no argument but return type, it does not receive any data from the calling function to called function but return a value to calling function. Example:Int sub(); Void main() { Int a,b; Scanf(%d%d, &a, &b); Printf(%d, sub()); } Int sub() { Int a,b; Return(a+b) ; }

Advantages of using functions:There are many advantages in using functions in a program they are: 1. The length of the source program can be reduced by using functions at appropriate places. 2. It becomes uncomplicated to locate and separate a function for further study. 3. A function may be used later by many other programs this means that a c programmer can use function written by others. 4. A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated.

Reference:Books: Let us c ANSIC


http/www.gnu.org http/www.balgurswmy.com

Thank you

Вам также может понравиться