User
Defined Functions
A
function is a block of code designed to perform a particular task. User-defined
functions are functions which are defined by the user. Functions are made for
code reusability and for saving time and space.
Every user defined function
has three parts as:
1.
Prototype
or Declaration
2.
Calling
3.
Definition
1. Prototype or Declaration
Every
function in C programming should be declared before they are used.
This declaration
is called function prototype.
Function
prototype gives information to compiler about function name, total number of
arguments and type of arguments to be passed and return type.
Syntax:
return_data_type
function_name(data_type parameter1, data_type parameter2 ......);
In above
syntax "return_data_type" means return value of
function:
if
return type is void then function will return nothing if return type is
int,float char then it will return as type declared.
In above
syntax "function_name" means that the name of function which we want
to define.
Example:
void
display(int a,int b); //return nothing
int
sum(int a,int b); //return integer
float avg(float,
int); //return
float
2.Calling
The
calling part of function work as:
It is call the function.
It forward the control from main() to user
define function and return back to main again.
Syntax:
function_name(actual
argument/parameter);
Example:
display(a,b);
//calling void function return nothing
int s;
s=sum(a,
b); //calling integer function return integer
float a;
a=avg(f,
i); //calling
float function return float
3.Definition
The
"definition" part is the code of function.
What
will function do? It carry the code we want to write in function.
The
definition part must be outside of main() function as we can not define
function inside another function.
syntax:
//if
return type is void:
function_name(data_type
argument1, data_type argument2…/parameter)
{
statement 1;
statement 2;
.
statement n;
}
Example:
void sum(int
a, int b)
{
int s=a+b
printf(“sum of a+b=%d”,s);
}
//Function
with void type
#include<stdio.h>
#include<conio.h>
void
sum(int , int);
//Function
Prototype/Declaration
void
main()
{
int
x,y;
clrscr();
printf("Enter value of x and
y: ");
scanf("%d %d", &x,
&y);
sum(x, y);
//calling of function-sum
getch();
}
//Definition
of function sum
void sum(int
a, int b)
{
int
res;
res = a + b;
printf("Sum of %d + %d =
%d",a,b,result);
}
|
//Function
with int type
#include<stdio.h>
#include<conio.h>
int
sum(int , int);
//Function
Prototype/Declaration
void
main()
{
int
x,y,result;
clrscr();
printf("Enter value of x and
y: ");
scanf("%d %d", &x,
&y);
result=sum(x, y); //calling of function-sum
printf("Sum of %d + %d =
%d",x,y,result);
getch();
}
//Definition
of function sum
int sum(int
a, int b)
{
int
res;
res = a + b;
return res;
}
|
Category/Type
of Function
User-defined
functions can be categorized as:
1.
Function
with no arguments and no return value
2.
Function
with no arguments and return value
3.
Function
with arguments but no return value
4.
Function
with arguments and return value
1. Function
with no arguments and no return value
#include<stdio.h>
#include<conio.h>
void sum( );
//Function Prototype/Declaration with no
arguments and no return value
void main()
{
clrscr();
sum(x,
y);
//calling
of function-sum
getch();
}
//Definition of function sum
void sum( )
{
int x,y,res;
printf("Enter
value of x and y: ");
scanf("%d
%d", &x, &y);
res
= a + b;
printf("Sum
of %d + %d = %d",a,b,result);
}
|
2. Function
with no arguments and return value
#include<stdio.h>
#include<conio.h>
int sum( ); //Function Prototype/Declaration with no
arguments and return value
void main()
{
int
result;
clrscr();
result=sum(
);
//calling
of function-sum
printf("Sum
of x + y = %d", result);
getch();
}
//Definition of function sum
void sum( )
{
int x,y,res;
printf("Enter
value of x and y: ");
scanf("%d
%d", &x, &y);
res
= a + b;
return
res; //returning integer value
}
|
3. Function
with arguments and no return value
#include<stdio.h>
#include<conio.h>
void sum(int , int);
//Function Prototype/Declaration with
arguments and no return value
void main()
{
int x,y;
clrscr();
printf("Enter
value of x and y: ");
scanf("%d
%d", &x, &y);
sum(x,
y);
//calling
of function-sum
getch();
}
//Definition of function sum
void sum(int a, int b)
{
int res;
res
= a + b;
printf("Sum
of %d + %d = %d",a,b,result);
}
|
4. Function
with arguments and return value
#include<stdio.h>
#include<conio.h>
int sum(int , int); //Function Prototype/Declaration with
arguments and return value
void main()
{
int x,y,result;
clrscr();
printf("Enter
value of x and y: ");
scanf("%d
%d", &x, &y);
result=sum(x,
y); //calling of function-sum
printf("Sum
of %d + %d = %d",x,y,result);
getch();
}
//Definition of function sum
int sum(int a, int b)
{
int res;
res
= a + b;
return
res;
}
|
Recursion
Recursion is the process of repeating items in a
self-similar way. In programming languages, if a program allows you to call a
function inside the same function, then it is called a recursive call of the
function.
void
recursion()
{
recursion(); /* function calls itself */
}
void
main()
{
recursion();
}
|
The
C programming language supports recursion, i.e., a function to call itself. But
while using recursion, programmers need to be careful to define an exit
condition from the function,
Otherwise
it will go into an infinite loop
//Fibonacci
series using recursion function
#include<stdio.h>
#include<conio.h>
int
fibo(int n);
void
main()
{
int n, i;
printf("\n Enter how many no
of terms you want of Fibonacci series : ");
scanf("%d", &n);
printf("\nFibonacci Series of
%d terms is as follows: \n",n);
for(i=1;i<=n;i++)
{
printf("%5d",
fibo(i));
}
getch();
}
int
fibo(int n)
{
int r;
if(( n == 1) || ( n == 0))
return 0;
else if(n == 2)
return 1;
else
return (fibo(n-1) +
fibo(n-2));
}
|
//Write
a program to find out factorial of a number using recursion funciton
#include<stdio.h>
#include<conio.h>
int
fact(int n);
void
main()
{
int n,f1;
printf("\nENTER N:");
scanf("%d",&n);
f1 = fact(n);
printf("\nN = %d",n);
printf("\nFact =
%d",f1);
getch();
}
int
fact(int n)
{
int f;
if(n==1)
return 1;
else
f=n*fact(n-1);
return f;
}
|
Passing
array to Function
Like
simple variable it is possible to pass the array to the function. To pass array
to the function it requires the name of the array and size of the array like
display (ar,n); //Where ar
is the name of array and n is size
of an array
It
will pass the whole array ar to the
function display. The function definition will be like
void display(in ar[],int size) //Definition
The
function display is defined with two arguments, the name of array and the size
of the array. It is not necessary to specify the size of array here.
//Example
of passing array to function
#include<stdio.h>
#include<conio.h>
void
display(int ar[],int n); //function prototype
void
main()
{
int ar[5],i;
clrscr();
printf("Insert data");
for(i=0;i<5;i++)
{
scanf("%d",&ar[i]);
}
display(ar,5); //passing array in
function
getch();
}
void
display(int ar[],int size) //function definition
{
for(i=0;i<5;i++)
{
printf("\n
%d",ar[i]);
}
}
|
Same
way we can also pass the string to the function
//Example
of passing string to array
#include<stdio.h>
#include<conio.h>
void
display(char name[]);
void
main()
{
char name[20];
clrscr();
printf("Insert name");
scanf("%s",name);
display(name);
getch();
}
void
display(char name[])
{
int l;
l=strlen(name);
printf("\nstring:%s\nlength:%d",name,l);
}
|
0 comments:
Post a Comment