0% found this document useful (0 votes)
38 views6 pages

Prog in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Prog in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

User Defined Functions :

Introduction :

Functions in C are classified into two categories :-


1. Library Function.
2. User Defined Function.

Library Function : These functions are defined in C compiler i.e. programs for library functions are written in C compiler
and we do not need to write any program for these functions. If we use/call these functions in our program, we have to
include appropriate header file into our program. E.g. pow(), sqrt(), scanf(), printf(), getch() etc.

User Defined Function :- These functions are developed by the user at the time of writing a program. In C main() is an
example of user defined function. We are able to code any problem using only main() function. For a complex problem
the main() function becomes too large and also complicated. As a result the task of debugging, testing and maintaining
of the program will be difficult.
Using user defined functions we can develop structured program. In a structured program, a program is written in
separate modules, where each module performs a separate task. Each of these modules is called subprogram or
function.

Advantage :
1. More readable i.e. can be understood easily.
2. Appropriate use of function may reduce the size of program.
3. Fault isolation or error detection is easier.

Format of a program using user defined function :

Format of a program that uses user defined function is as follows :-

#include<stdio.h>
…………
Function_type function_name1(type of parameters separated by comma); /*Function declaration or prototype*/
Function_type function_name2(type of parameters separated by comma); /*Function declaration or prototype*/
…………
void main()
{
-----------
-----------
function_name1(list of parameters separated by comma); /*Function call*/
-----------
-----------
function_name2(list of parameters separated by comma);/*Function call*/
-----------
-----------
}
Function_type function_name1(list of parameters separated by comma)/*Function header*/
{
---------- Function
---------- Function Body Definition
----------
}
Function_type function_name2(list of parameters separated by comma)/*Function header*/
{
---------- Function
---------- Function Body Definition
----------
}
In the given example when we call a function as shown, control will move to the definition section of the corresponding function
and body of that function will be executed. After execution of the function, control will return to the statement just after the
statement from where the function is called.

Elements of User Defined Function :

There are mainly 3 elements of a function –


1. Function declaration
2. Function definition
3. Function call
Function Declaration:
It is also known as function prototype. Syntax or grammar of function prototype is as follows :-
Function_type Function_name(type of parameters separated by comma);
Example: int xyz(int, float);
Function type int means the function returns a value of type integer. xyz is the function name. The function contains
two parameters of type int and float.

Function Definition :
In function definition section user have to write code for the function. General format of function definition section is
as follows :

Function_type Function_name(List of parameters separated by comma) /*Function Header*/


{
Local declarations of variables or other function;
Statement1;
----------- Function body
-----------
return statement;
}

Example :-
Formal parameter
int fact(int n)
{
int i,f;
f=1;
for(i=1;i<=n;i++)
f=f*I;
return f;
}
**Point to be noted that parameters used in function header are called formal/dummy parameters. In the above
example n is formal parameter.

Function Call :
A function can be called by simply using the function name followed by appropriate parameters which are enclosed in
parenthesis. It can be called from main() function or from any other function depending on the type of the function.
Example :
Void main()
{
Int m,p;
------
------ Actual Parameter
p=fact(m);/*Function call*/
------
------
}
** Point to be noted that parameters used in function call are called actual parameters or arguments. In the above
example m is actual parameter or argument.
Category of function:

Different categories of function are described here with example.


1. No arguments and no return values :

#include<stdio.h>
#include<conio.h>
void fact(void);
void main()
{
fact();
getch();
}
void fact(void)
{
int i,n,f;
printf(“\nEnter a number:”);
scanf(“%d”,&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf(“\nFactorial=%d”,f);
}

In the above program when we call the function fact() at main, control will skip to the function definition section of the
called function fact(). The body of the function will be executed. In this example factorial of n will be calculated and it
will be displayed on screen. After that control will return to the statement just after the statement from where it is
called i.e. getch() in this example. In this case no value passes from main() function to subprogram fact() and the
subprogram returns no value.

2. Passing constant and returning no value:

#include<stdio.h>
#include<conio.h>
void fact(int);
void main()
{
fact(5);
getch();
}
void fact(int n)
{
int i,f;
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf(“\nFactorial=%d”,f);
}

In the above example when we call the function at main() by the statement fact(5), control will move to the subprogram
for fact() and the value 5 will be copied to n (formal or dummy parameter). Now factorial of n i.e. factorial of 5 will be
evaluated in the function and it will be displayed on screen. After that control will return to the next statement in the
calling function i.e. at the statement getch() in this example.
3. Passing variable and returning no value:

#include<stdio.h>
#include<conio.h>
void fact(int);
void main()
{
int m;
printf(“\nEnter a number:”);
scanf(“%d”,&m);
fact(m);
getch();
}
void fact(int n)
{
int i,f;
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf(“\nFactorial=%d”,f);
}

In the above example when we call the function, control will move to the subprogram for fact() and the value of m
(actual parameter) will be copied to n (formal or dummy parameter). Now factorial of n i.e. factorial of m will be
evaluated in the function and it will be displayed on screen. After that control will return to the next statement in the
calling function i.e. at the statement getch() in this example. In this way we can pass values of parameter from calling
function to the called function. We are also able to pass more than one value from calling function to the called function.
Passing values of parameters from calling function to called function is known as parameter passing.

4. Passing value and returning value :

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,r,a,b,c,res;
printf(“\nEnter values of n and r:”);
scanf(“%d%d”,&n,&r);
a=fact(n);
b=fact(r);
c=fact(n-r);
res=a/(b*c);
printf(“\nResult=%d”,res);
getch();
}
void fact(int m)
{
int i,f;
f=1;
for(i=1;i<=m;i++)
f=f*i;
return f; /*return statement to return value*/
}

In the above example the function fact() returns the value of the variable f, which is of type integer. Hence function
type is int. In this example for the statement a=fact(n), the value of n will be copied to m and the function will calculate
factorial of m i.e. factorial of n, which is stored in variable f. For the return statement the value of f will be returned at
the position from where the function is called. As a result the value will be stored in variable a. Similarly the values of
factorial of r and factorial of (n-r) will be stored in variables b and c respectively. The program calculates
n
Cr=n!/(r!*(n-r)!) .

Parameter passing :
Passing values of parameters from calling function to called function is known as parameter passing. Parameter passing is
classified into two categories:
1. Call-by-value parameter passing
2. Call-by-reference parameter passing.
Call-by-value parameter passing:
In this case value of actual parameter will be copied to corresponding formal or dummy parameter. Any change in formal
parameter does not affect/change the value of actual parameter.
Call-by-reference parameter passing:
In this case, actual and corresponding formal parameters use the same memory location. As a result any change in formal
parameter changes the value of corresponding actual parameter.

**Using call-by-value parameter passing we can return only one value from function. If we want to return more than one
value from function, we have to use call-by-reference parameter passing.

Example:
Program to calculate roots of a quadratic equation:-

#include<stdio.h>
#include<conio.h>
#include<math.h>
void quad(int,int,int,float&,float&,float&,float&);
void main()
{
int p,q,r;
float root1,root2,rp,ip;
printf(“\nEnter coefficients:”);
scanf(“%d%d%d”,&p,&q,&r);
ip=0.0;
quad(p,q,r,root1,root2,rp,ip);
if(ip!=0.0)
{
printf(“\nRoots are imaginary”);
printf(“\nReal Part=%f\tImaginary Part=%f”,rp,ip);
}
else
{
printf(“\nRoots are real”);
printf(“\nRoot1=%f\tRoot2=%f”,root1,root2);
}
getch();
}
Void quad(int a,int b,int c,float& r1,float& r2,float& rp1,float& ip1)
{
float d;
d=b*b-4.0*a*c;
if(d>=0.0)
{
r1=(-b+sqrt(d))/(2.0*a);
r2=(-b-sqrt(d))/(2.0*a);
}
else
{
rp1=-b/(2.0*a);
ip1=sqrt(-d)/(2.0*a);
}
}
In this case parameters are passed by both call-by-value and call-by-reference parameter passing techniques. Actual parameters
p, q and r are passed by call-by-value and actual parameters root1, root2, rp and ip are passed by call-by-reference i.e. root1
and r1, root2 and r2, rp and rp1, ip and ip1 use the same memory location. Hence in subprogram section any change in r1, r2,
rp1 and ip1 changes the value of root1, root2, rp and ip respectively. So for real roots, the function quad() calculate the values
of r1 and r2 and accordingly in main() section root1 gets the value of r1 and root2 gets the value of r2. Similarly for imaginary
roots, rp gets the value of rp1 and ip gets the value of ip1. In this way using call-by-reference parameter passing technique users
are able to return more than one value from function to the function from where it is called, which is the main advantage of
call-by-reference parameter passing technique.

You might also like