Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Function

Definition
A function is a self contained program segment that
carries out some specific, well defined task. A function
takes some data from main () and returns a value.
There are two types of function. They are
 Library function: Library functions are the in-built function in C
programming system.
For example: printf()
 User defined function: C allows programmer to define their own
function according to their requirement. These types of
functions are known as user-defined functions.
For example: sum().
Elements of user defined function
In order to write an efficient user defined function, the programmer must be familiar the
following three elements:
Function Declaration: Like the normal variable the function must be declared before it
is defined and called or invoked.
Syntax :
return_type function_name(arg1, arg2, )
Example : int add(int x, int y); OR int add(int,int);
Function Definition: It is the process of specifying and establishing the user defined
function by specifying all its elements and characteristics.
Syntax:
return_type function_name(arg1,arg2. )
{
Local variable declaration
Body of the function
return(expr);
}
Example:
int add(int a,int b)
{
int c; c=a+b; return(c);
}
Function Call:
The function can be simply called by specifying the
name of the function, return value and parameters if
present.
Syntax:
function_name();
OR
function_name(arg1,arg2 )
OR
return_value=function_name(arg1,arg2 );
Example
add();
OR
add(x,y);
OR
z=add(x,y);
Example
#include<stdio.h> #include<conio.h> void main()
{
int c,a,b;
int add(int,int); clrscr();
printf("Enter the values of a and b to perform addition:"); scanf("%d
%d",&a,&b);

c=add(a,b);
printf("The sum is %d",c); getch();
}
int add(int a1, int b1)
{
return(a1+b1);
}
Call by value
If data is passed by value, the data is copied from the
variable used in for example main() to a variable used
by the function. So if the data passed (that is stored in
the function variable) is modified inside the function,
the value is only changed in the variable used inside
the function.
#include<stdio.h> void swap(int number1,int
#include<conio.h> number2)
void main()
{ {
int num1=50,num2=70; void int temp;
swap(int,int); clrscr(); temp = number1; number1 =
printf("Before Swapping\ number2; number2 =
n"); printf("\nNumber 1 : temp;
%d",num1); printf("\ printf("\nNumber 1 :
nNumber 2 : %d",num2); %d",number1); printf("\
printf("\nAfter Swapping\ nNumber 2 :
n"); swap(num1,num2); %d",number2);
getch(); }
}
Call by reference
If data is passed by reference, a pointer to the data is
copied instead of the actual variable as is done in a call
by value. Because a pointer is copied, if the value at
that pointers address is changed in the function, the
value is also changed in main().
#include<stdio.h> nNumber 2 : %d",num2);
#include<conio.h> void getch();
main() }
{ void swap(int *num1,int
int num1=50,num2=70; void *num2)
swap(int *,int *); clrscr(); {
printf("Before Swapping\n"); int temp;
printf("\nNumber 1 :
%d",num1); printf("\ temp = *num1;
nNumber 2 : %d",num2);
*num1 = *num2;
swap(&num1,&num2);
printf("\nAfter Swapping\ *num2 = temp;
n"); printf("\nNumber 1 : }
%d",num1); printf("\

You might also like