9.Functions
9.Functions
1) Library functions
2) User defined functions
1) Library functions-> The library functions are pre-defined set of functions. Their task
is limited. A user can not understand the internal working of these functions. The user
can only use the functions but can’t change or modify them. For example, sqrt(), cos(),
tan().
2) User defined functions-> The functions defined by the user according to his/her
requirement are called as User-defined functions. The user can modify the function
according to the requirement. The user certainly understands the internal working of the
function. The user has full scope to implement the his/her own ideas in the function. Thus
the set of such user-defined functions can be useful to another programmer.
function_name(argument/parameter list)
argument declaration;
{
local variable declaration;
statement1;
statement2;
return(value);
}
void main()
{
……..
……..
abc(x,y,z); function call
actual argument
…………….
…………….
}
abc(l,k,j) function definition
formal argument
…………..
…………..
return(); return value
}
a) Actual Argument The arguments of calling functions are actual arguments. Variables
‘x’, ‘y’, and ‘z’ are actual arguments.
b) Formal Argument The arguments of called function are formal arguments. Variable
l,k and j are formal arguments.
Example
value(int k, int m)
{
int r,t;
}
2) Global variables-> Global variables are defined outside the main() function. Multiple
functions can use them.
int b=10,c=5;
void main()
{
clrscr();
printf(“\nin main() b=%d c=%d”,b,c);
fun();
b++;
c--;
printf(“\nAgain in main() B=%d C=%d”,b,c);
getch();
}
fun()
{
b++;
c--;
printf(“\nIn fun() B=%d C=%d”,b,c);
}
Here variables ‘b’ & ‘c’ are defined outside the main() hence they are global.
Return value-> It is the outcome of the function. The result obtained by the function is
sent back to the calling function through the return statement. The return statement
returns one value per call. The value returned is collected by variable of the calling
function.
Function Prototypes
The user-defined function uses return statement to return the value to the calling function.
Exit from the called function to the calling function is done by the use of return
statement. When return statement is executed it always returns 1.
a) return(expression)
example : return(a+b+c);
If such a statement is executed, the expression within the parenthesis is first solved and
the result obtained is returned.
b) A function may use one or more return statements. It is used when we want to return a
value depending upon certain conditions.
Types of functions
Depending upon the arguments present, return value sends the result back to the calling
function. Based on this, the functions are divided into four types.
1) Neither the data is passed through the calling function nor the data is sent back from
the called function.
2) There is no data transfer between calling and the called functions.
3) The function is only executed and nothing is obtained.
Ex-
#include<stdio.h>
#include<conio.h>
void add();
void main()
{
clrscr();
add();
getch();
}
void add()
{
int a,b,sum;
#include<stdio.h>
#include<conio.h>
void add(int a,int b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
add(a,b);
getch();
}
void add(int a,int b)
{
int sum;
sum=a+b;
printf(“\nsum=%d”,sum);
}
#include<stdio.h>
#include<conio.h>
int add(int a,int b);
void main()
{
int a,b,s;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
s=add(a,b);
printf(“\nsum=%d”,s);
getch();
}
int add(int a,int b)
{
int sum;
sum=a+b;
return(sum);
}
There are two ways in which we can pass arguments to the function.
1) Call by value
2) Call by reference
1) Call by value
In this type value of actual arguments are passed to the formal arguments and the
operation is done on the formal arguments. Any change made in the formal argument
does not effect the actual argument because formal arguments are photocopy of actual
arguments. Hence, when function is called by the call or by value method, it does not
affect the actual contents of the actual arguments. Changes made in the formal arguments
are local to the block of the called function. Once control returns back to the calling
function the changes made vanish.
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
swap(a,b);
printf(“\n After swapping :”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
void swap(int a,int b)
{
int c=a;
a=b;
b=c;
}
2) call by reference
In this type instead of passing values, addresses(reference) are passed. Function operates
on addresses rather than values. Here the formal arguments are pointers to the actual
arguments. In this type formal arguments point to the actual argument. Hence changes
made in the arguments are permanent.
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
printf(“\n After swapping :”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
void swap(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}
Recursion
#include<stdio.h>
#include<conio.h>
int fact(int x);
void main()
{
int x,f;
clrscr();
printf(“\n Enter a number :”);
scanf(“%d”,&x);
f=fact(x);
printf(“\n Factorial=%d”,f);
getch();
}
int fact(int m)
{
int f=1;
if(m==1)
return(1);
else
f=m*fact(m-1);
return(f);
}