function

23
Function A function is a set of statements, written in a particular language that performs a particular task ,when ever called.

Upload: jasscheema

Post on 20-Jan-2015

231 views

Category:

Education


2 download

DESCRIPTION

c-language

TRANSCRIPT

Page 1: Function

Function

A function is a set of statements, written in a particular language that performs a particular task ,when ever called.

Page 2: Function

Types of functions

1.Inbuilt or system defined or Library Functions

2.User defined Functions

Page 3: Function

Inbuilt functions

These are the functions whose defintions is already defined and stored in a respective header files of c library .

Example of this type of functions are gets(),puts(),clrscr(),getche(),printf(),scanf(). Etc.

Page 4: Function

User defined functions

The functions which are created by the user to, perform the desired task,are called user defined functions.

User defined functions provide the following

Benefits:-1.By using user defined functions,we

can divide a large problem into small independence modules,each having a defined purpose.the process is called modulation.

Page 5: Function

benefits

2.Smaller modules are easy to design and debug.

3.We can create own user defined functions,in the way,a function can be created once and used for many times.i.e sharing of the code.

4Modulation provide greater clarity to the programs.

Page 6: Function

Defining a function

Syntax :<return type><name>(parameterlist)>

{Statement 1;Statement 2;------------------Statement n;Return (expression);}

Page 7: Function

Elements of functions

1. Return type :- is datatype of the value ,returned by the function .void is used when the function does not return any value,int and float are used when functions returns integer or floating point values.

2.Name:-is the name of the function3.Parameter list:-contain variables carrying

information from the main function.4 Function call is represented by writing the

name of the function followed by braces and semicolon;

Page 8: Function

Example describes a function to calc sum of

two no. Int sum(int a,int b){ // function bodyPrintf(“the sum of no is being calculated”);Return(a+b);}Void main(){Int j=0;Int k=10;Int m=12;J=sum(k,m); //call to sum functionPrintf(“the sum is %d”,j);}

Page 9: Function

Return type of the function is “int”.the name of the function is “sum” .the parameter are” a” and “b” of integer type.

C allows us to define user defined function in various ways.we can define the functions in following ways:

1. Functions with no argument and no return type.

2. Function with argument and no return type.

3. Functions with argument and with return type like int ,float.

Page 10: Function

Functions with no argument and no return type. These are the simple function

which do not take any data from calling functions, in the form of parameters and do not return anything to calling functions, after execution.

Page 11: Function

The example describes the use of func. with no parameter and no return typeSimple function.#include<stdio.h>#include<conio.h>Void main(){void punjab(); //function prototypingVoid delhi(); //function prototypingPrintf(“\n I m from main”);Punjab(); // call to

punjab()

Page 12: Function

Printf(“\n\t I m back in main”);Himachal();Print f(“\n\t \t I m from main again”);}Void punjab(){Printf(“\n I m from punjab”);}Void himachal(){Print(“\n I m from himachal”);}

Page 13: Function

output

I m from mainI m from punjabI m back in mainI m from himchalI m from main again

Page 14: Function

The example describes the use of func. with no parameter and return typeExample to calculate the sum of two

numbers using#include<stdio.h>#include<conio.h>Void main(){Int sum();Int result();

Page 15: Function

Clrscr();Printf(“\calculate sum of two numbers

“);Getch();Result=sum(); call to function

sum //result contains the

value enteredPrintf(“\n the result after adding is

number %d”,result);Getch();}Int sum() body of the

function{

Page 16: Function

Int a,b;Printf(“\n enter 2 numbers”);Scanf(“%d%d”,&a,&b);Return(a+b);}

Page 17: Function

Out putReady to calculate sum of two

numbers-press any keyEnter 2 number 1223The result after adding numbers is 35

Page 18: Function

Functions with argu and no return type To demonstrate the functions with parameter

and no return#include<stdio.h>#include<conio.h>Void main(){Void sum(int,int);Int a,b;Printf(“enter 2 numbers”);Scanf(“%d%”,&a,&b);Sum(a,b);Getch();

Page 19: Function

} //end of mainVoid sum(int f1,int f2){Int result; //variable in

functionResult=(f1+f2);Printf(“\n the result after addition is

%d”,result);}

Page 20: Function

Out put

Enter 2 numbers 10 10The result after addition is 20

Page 21: Function

Func with argu and return type

#include<stdio.h>#include<conio.h>Void main(){Int sum(int,int);Int a,b,ans;Printf(“enter 2 numbers”);Sanf(“%d%d”,&a,&b);Ans=sum(a,b);Printf(“\n the result after addition is

%d”,ans);

Page 22: Function

Getch();}Int sum(int f1,int f2);{Int result;Result=f1+f2;Return(result);}

Page 23: Function

output

Enter 2 numbers 12 12 The result after addition is 24