function
DESCRIPTION
c-languageTRANSCRIPT
Function
A function is a set of statements, written in a particular language that performs a particular task ,when ever called.
Types of functions
1.Inbuilt or system defined or Library Functions
2.User defined Functions
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.
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.
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.
Defining a function
Syntax :<return type><name>(parameterlist)>
{Statement 1;Statement 2;------------------Statement n;Return (expression);}
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;
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);}
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.
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.
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()
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”);}
output
I m from mainI m from punjabI m back in mainI m from himchalI m from main again
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();
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{
Int a,b;Printf(“\n enter 2 numbers”);Scanf(“%d%d”,&a,&b);Return(a+b);}
Out putReady to calculate sum of two
numbers-press any keyEnter 2 number 1223The result after adding numbers is 35
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();
} //end of mainVoid sum(int f1,int f2){Int result; //variable in
functionResult=(f1+f2);Printf(“\n the result after addition is
%d”,result);}
Out put
Enter 2 numbers 10 10The result after addition is 20
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);
Getch();}Int sum(int f1,int f2);{Int result;Result=f1+f2;Return(result);}
output
Enter 2 numbers 12 12 The result after addition is 24