A
function is a sub program which performs a specific task.
Or
A
block of code is written outside the main.
Advantages of using functions:
Size
of the program reduces
It
occupies less memory
Repetition
of the data will not be there because once a function is defined it can be
called any number of times.
Separation
of a code will be there
Easy
to understand
Easy
to fine errors
Execution
of the program the program will be fast.
Types of functions:
C
is supported to following types of Functions
Predefined
Functions
User
defined functions
Predefined
Functions:
The
functions which are already defined c language.
Ex:
printf(),scanf(),sqrt()…..etc.
User defined
functions:
The
functions which are defined by the user are called as user defined functions.
The
contents of this function can be change when ever the user wants.
Syntax:
Returntype functionname (argument_list)
{
…
…
}
Return type:
It represents the type of the value function
is going to return.
If return type is int function will return
integer value.
If return type is float function will return
float value.
If return type is void it represents that
function will not return any value.
Return statement is used for returning a
value to the caller.
Syntax:
Return value;
Note: at a time a function can return only
one value.
Function Name:
It can be any valid name.
Argument list:
It represents the type of the arguments and
the no. of arguments the function takes.
Body of the Function:
The content of the function is called as body
of the function.
Types of functions based on arguments list
and return type.
C- Supports 4 types functions based on argument list and return
type.
- Function with no arguments and no return value.
- Function with arguments and no return value.
- Function with arguments and with return value.
- Function with no arguments and with return value.
Function
with no arguments and no return value
|
Function
with arguments and no return value
|
Function
with arguments and with return value
|
Function
with no arguments and with return value
|
Syntax:
void fn ( )
{
…
…
}
|
Syntax:
void fn (al)
{
…
…
}
|
Syntax:
rt fn (al)
{
…
…
}
|
Syntax:
rt fn ( )
{
…
…
}
|
rt: return type , fn
:function name , al: arguments list
|
|||
//
write a program to find sum of two numbers
|
|||
#include<stdio.h>
#include<conio.h>
void
sum ();
void
main ();
{
clrscr();
sum();
getch();
}
void sum()
{
int
a,b,c;
printf(“\nEnter
the values of a,b:”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“\nsum=%d”,c);
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<stdio.h>
#include<conio.h>
void
sum (int a, int b);
void
main ();
{
int
x,y;
clrscr();
printf(“\nEnter
the values of x,y:”);
scanf(“%d%d”,&x,&y);
sum(x,y);
getch();
}
void sum(int a, int b)
{
int
c;
c=a+b;
printf(“\nsum=%d”,c);
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<stdio.h>
#include<conio.h>
int
sum (int a, int b);
void
main ();
{
int
x,y;
clrscr();
printf(“\nEnter
the values of x,y:”);
scanf(“%d%d”,&x,&y);
rv=sum(x,y);
printf(“\nsum=%d”,rv);
getch();
}
int sum(int a, int b)
{
int
c;
c=a+b;
return c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<stdio.h>
#include<conio.h>
int
sum ();
void
main ();
{
int
rv;
clrscr();
rv=sum();
printf(“\nsum=%d”,rv);
getch();
}
int sum()
{
int
a,b,c;
printf(“\nEnter
the values of a,b:”);
scanf(“%d%d”,&a,&b);
c=a+b;
return
c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
Note:
After
the execution of function completes control returns back to the caller.
|
Note:
//x,
y are actual parameters.
//a, b are formal parameters.
Actual
Parameters:
The
parameters passed by the user by calling the function are called as actual
parameters.
Formal
Parameters:
The
parameters used in the function definition are called formal parameters
Note:
While
calling the function we have to pass the exact no of arguments the function
takes.
If
we pass less no of arguments then the function takes, it generates an error
message two few parameters to call the function.
If
we pass more no of arguments then the function takes, it generates an error
message extra parameters in calling the function.
|
Note:
Return
statement is used for returning a value.
Syntax:
return
value;
After
execution of this statement control returns back to the caller.
At
a time a function can return only one value.
|
Note: the variables declared in one function
are not visible in other functions so we must declare the variables in which
they are used.
//write a program to find maximum of
two numbers
|
|||
#include<stdio.h>
#include<conio.h>
void
max ();
void
main ();
{
clrscr();
max();
getch();
}
void max()
{
int
a,b;
printf(“\nEnter
the values of a,b:”);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“\nmax=%d”,a);
}
else
{
printf(“\nmax=%d”,b);
}
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<stdio.h>
#include<conio.h>
void
max (int a, int b);
void
main ();
{
int
x,y;
clrscr();
printf(“\nEnter
the values of x,y:”);
scanf(“%d%d”,&x,&y);
max(x,y);
getch();
}
void max(int a, int b)
{
if(a>b)
{
printf(“\nmax=%d”,a);
}
else
{
printf(“\nmax=%d”,b);
}
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<stdio.h>
#include<conio.h>
int
max (int a, int b);
void
main ();
{
int
x,y;
clrscr();
printf(“\nEnter
the values of x,y:”);
scanf(“%d%d”,&x,&y);
rv=max(x,y);
printf(“\nmax=%d”,rv);
getch();
}
int max(int a, int b)
{
if(a>b)
return
a;
else
return
b;
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<stdio.h>
#include<conio.h>
int
max ();
void
main ();
{
int
rv;
clrscr();
rv=max();
printf(“\nmax=%d”,rv);
getch();
}
int max()
{
int
a,b,c;
printf(“\nEnter
the values of a,b:”);
if(a>b)
return
a;
else
return
b;
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
Recursive function:
The function called it self is known as
Recursive function.
// program
|
#include<stdio.h>
#include<conio.h>
long int fact (long int n);
void
main ();
{
int
n;
clrscr();
printf(“\nEnter
the value of n:”);
scanf(“%ld”,&n);
rv=fact(n);
printf(“\nfact
of %ld=%ld”,n,rv);
getch();
}
long int fact(long int n)
{
if(n==0)
return
1;
else
return
n*fact(n-1);
}
Result:
Input:
Enter
the value of n: 5
Output:
Fact
of 5=120
|
No comments:
Post a Comment