Chapter - 04: "Functions": (C - Programming)
Chapter - 04: "Functions": (C - Programming)
B.Tech. 1st, 2nd, 3rd & 4th Sem, B.C.A., B.Sc. (Computers & I.T.), PGDCA,
B.Pharmacy
Teacher : Sandeep Sangwan (9417565720, 9417524702, 9463741635)
S.C.F. 204, 2nd Floor (Above Chandigarh Sweets Pvt. Ltd.), Sec-14,
Panchkula
[ C PROGRAMMING ]
Chapter - 04 : Functions
1. Define a function. What is function prototype ?
Ans. A Function is a sequence of some declaration statements and executable statements. A
function prototype is the first line of the function definition that tells the program about the type
of the value returned by the function and the number and type of arguments. With the help of
function prototype, a compiler can carefully compare each use of function with the prototype to
determine whether the function is invoked properly i.e., the number and types of arguments are
compared and any wrong number or type of the argument is reported.
2. What is function definition ?
OR
What is the syntax or general form of a function?
Ans. In C++, a function must be defined before it is used anywhere in the program. The general
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
2
invoked) by providing the function name, followed by the paremeters being sent enclosed in
parentheses. For example, to invoke a function whose prototype looks like
int sum (int,int);
the function call statement may look like as shown below :
sum (x,y);
where x, y have to be int variables. Whenever a function call statement is encountered, the
program control is transferred to the function, the statements in the function-body are executed,
and then the control returns to the statement following the function call. For example,
void main()
{
int a=10,b=20;
printf(\nSum of %d and %d is : %d,a,b,sum(a,b)); // function call
}
int sum(int x, int y) //formal parameters
{
return x+y;
}
4. What are actual and formal parameters of a function?
Ans. In functions, there are parameters in the function definition and in the statement that
invokes the function i.e. the function call statement. The parameters that appears in function
definition are called formal parameters and the parameters that appear in function call statement
are called actual parameters. For example :
int main( )
{
int len=15,br=10,area;
area=rectangle(len,br);
// len,br are actual parameters
printf(Area of rectangle is : %d,area);
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
3
return 0;
}
int rectangle(int x,int y)
{
return x*y;
}
functions that do not return a value. By declaring a functions return type void, one makes sure
that the function cannot be used in an assignment statement.
6. How is call-by-value method of function invoking different from call-byreference method? Give appropriate examples supporting you answer.
Ans. In call-by value method, the value of a variable is passed to the function being called. The
called function creates its own work copy for the passed parameters and copies the passed
values in it. Any changes that take place remain in the work copy and the original data remains
unchanged.
In call-by-reference method, a reference to the original variable is passed. A reference
stores a memory location of a variable. The called function receives the reference to the passed
parameters and through this reference, it access the original data. Any changes that take place
are reflected in the original data. For example, the following program swap values of two integers
through both types of function calling mechanism.
int x = 10, y = 20;
void main()
{
printf(Original values before swapping \n x = %d\ty : %d,x,y);
swapCallbyValue(x,y);
printf(\nAfter swapping using call by value \n x = %d\ty : %d,x,y);
swapCallbyRef(&x,&y);
printf(\nAfter swapping using call by reference \n x = %d\ty : %d,x,y);
}
void swapCallbyValue(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(\nInside call by value \n x = %d\ty : %d,x,y);
}
void swapCallbyRef(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
4
*b=temp;
printf(\nInside call by reference \n x = %d\ty : %d,x,y);
}
The output produced by above program is as given below :
Values initially, x=10, y=20
Values inside swapCallbyValue function, x=20, y=10
Values after swapCallbyValue function, x=10, y=20
Values inside swapCallbyRef function, x=20, y=10
Values after swapCallbyValue function, x=20, y=10
7.
What is the role of return statement in a function ? How many values can be
returned from a function ?
Ans. A return statement is used to terminate a function whether or not it returns a value. The
return statement is useful in two ways. First, an immediate exit from the function is caused as
soon as a return statement is encountered and the control passes back to the function call.
Second use of return statement is that it is used to return a value to the calling code.
A function may contain several return statements. However, only one of them gets
executed because the execution of the function terminates as soon as a return is encountered.
Following program code uses multiple return statements :
int maximum(int a,int b)
{
if(a>b)
return a;
else
return b;
}
# Rules about functions :
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
5
12) Functions are used to avoid the duplication of lines. Also with functions program can be
easily debugged.
Local Variables
Variables that can be declared inside a function are called local variables. Local variables
can be used only in statements that are inside the block in which the variables are declared. In
other words, local variables are not known outside their own code block where a block of code
begins with an opening brace and terminates with a closing brace.
A variable declared within one code block has no relationship to another variable with the
same name declared within a different code block. The most common code block in which local
variables are declared is the function. For example, consider the following two functions:
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = - 199;
}
The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in
func1( ) has no relationship to the x in func2( ). This is because each x is known only to the code
within the block in which they are declared.
Global Variables :
Global Variables are those variables which are known throughout the program and may
be used by any piece of code. Also, they will hold their value throughout the program execution.
We create Global Variables by declaring them outside of any function.
For example, consider the following example
int count ;
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
6
void func1();
void func2();
void main()
{
count = 100;
func1( );
}
void func1()
{
int temp;
temp = count;
func2( );
printf (Count is: %d, count); // will print 100
}
void func2()
{
int count;
for (count = 1; count < 10; count ++)
printf (.);
}
In this program, the variable count has been declared outside of all functions. Although its
declaration occurs before the main( ) function, we could have placed it anywhere before its first
use as long as it was not in a function. However, it is usually best to declare global variables at
the top of the program.
In the above program, although neither main( ) nor func1( ) has declared the variable
count, both may use it. func2( ), however, has declared a local variable called count. When
func2() refers to count, it refers to only its local variable, not the global one. If the local and the
global variable have the same name, all references to that variable name inside the code block in
which the local variable is declared will refer to that local variable and have no effect on the global
variable.
Program 1 : WAP to display the sum of two numbers using call by value
#include<stdio.h>
#include<conio.h>
int sum(int,int);
void main()
{
clrscr();
int a,b;
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
7
C-PROGRAMMING (Functions)
8
int l,b;
printf("Enter length & breadth of rectangle : ");
scanf("%d%d",&l,&b);
printf("\nArea of rectangle is : %d",area(l,b));
printf("\nPerimeter of rectangle is : %d",peri(l,b));
getch();
}
int area(int l,int b)
{
return l*b;
}
int peri(int l,int b)
{
return 2*(l+b);
}
Program 4 : WAP to display the area and circumference of a circle using functions
(M. Imp.)
#include<stdio.h>
#include<conio.h>
float area(float);
float cmf(float);
float pi=(float)22/7;
void main()
{
clrscr();
float rad;
printf("Enter radius of circle : ");
scanf("%f",&rad);
printf("\nArea of circle : %.2f ",area(rad));
printf("\nCircumference of circle : %.2f",cmf(rad));
getch();
}
float area(float rad)
{
float ar=pi*rad*rad;
return ar;
}
float cmf(float rad)
{
float cf=2*pi*rad;
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
9
return cf;
}
Program 5 : WAP to accept a number from the keyboard and display whether it is an
armstrong number or not
For e.g.
153 = 13 + 53 + 33
= 1 + 125 + 27
= 153
Armstrong numbers are 1, 153, 370, 371 & 407
#include<stdio.h>
#include<conio.h>
char armstrong(int);
void main()
{
clrscr();
int n;
printf("Enter number : ");
scanf("%d",&n);
if(armstrong(n)=='t')
printf("\n%d is armstrong",n);
else
printf("\n%d is not armstrong",n);
getch();
}
char armstrong(int n)
{
int m,rem,sum=0;
m=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==m)
return 't';
else
return 'f';
}
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
10
Program 6 : WAP to accept a number and display whether it is a prime number or not
using functions
#include<stdio.h>
#include<conio.h>
char prime(int);
void main()
{
clrscr();
int n;
printf("Enter number : ");
scanf("%d",&n);
if(prime(n)=='t')
printf("\n%d is prime",n);
else
printf("\n%d is non-prime",n);
getch();
}
char prime(int n)
{
int i,f=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}
}
if(f==0)
return 't';
else
return 'f';
}
Program 7 : Swapping of two numbers using call by value and call by reference method
#include<stdio.h>
#include<conio.h>
void swap_cbval(int,int);
void swap_cbref(int *,int *);
void main()
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
11
{
clrscr();
int a=10,b=20;
printf("Original values\n a : %d\tb : %d",a,b);
swap_cbval(a,b);
printf("\nAfter call_byval() function\n a : %d\tb : %d",a,b);
swap_cbref(&a,&b);
printf("\nAfter call_byref() function\n a : %d\tb : %d",a,b);
getch();
}
void swap_cbval(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\n\nInside call_byval() function\n a : %d\tb : %d",a,b);
}
void swap_cbref(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("\n\nInside call_byref() function\n a : %d\tb : %d",*a,*b);
}
RECURSION
When a called function in turns calls another function a process of 'chaining' occurs.
Recursion is a special case of this process, where a function calls itself. A very simple example
of recursion is presented below :
main()
{
printf("This is an example of recursion");
main();
}
When executed, this program will produce an output something like this :
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
12
C-PROGRAMMING (Functions)
13
fact = 3 * factorial(2)
= 3 * 2 * factorial(1)
=3*2*1
=6
Recursive functions can be effectively used to solve problems where solution is expressed
in terms of successively applying the same solution to subsets of the problem. When we write
recursive functions, we must have an if statement somewhere to force the function to return
without recursive call being executed. Otherwise, the function will never return.
Program 1 : WAP to display the factorial of a number using recursion (M. Imp.)
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
clrscr();
int n;
printf("Enter number : ");
scanf("%d",&n);
printf("\nFactorial of %d is : %d",n,factorial(n));
getch();
}
int factorial(int n)
{
int fact=1;
if(n==1)
return 1;
else
fact=n*factorial(n-1);
return fact;
}
Program 2 : WAP to generate fibonaci series using recursion The user enter the limit of
series . (M. Imp.)
#include<stdio.h>
#include<conio.h>
void fseries(int);
void main()
{
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-PROGRAMMING (Functions)
14
clrscr();
int limit,f0=0,f1=1;
printf("Enter the limit of the series : ");
scanf("%d",&limit);
if(limit>2)
{
printf("%d\t%d",f0,f1);
fseries(limit-2);
}
else
if(limit==2)
printf("%d\t%d",f0,f1);
else
if(limit==1)
printf("%d",f0);
else
printf("\nSeries not possible");
getch();
}
void fseries(int p)
{
int fib;
static int f0=0,f1=1;
if(p==0)
printf("\nThe series ends here");
else
{
fib=f0+f1;
f0=f1;
f1=fib;
printf("\t%d",fib);
fseries(p-1);
}
}
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)