Unit Iii
Unit Iii
FUNCTIONS
SAMPLE QUESTIONS
{ {
} }
Advantages of functions:-
Function components:-
Every function has the following components or elements associated with it.
i) Function prototype
ii) Function parameters
iii) Function call(calling function)
iv) Function definition(called function)
v) Return statement.
Example:
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
a=10;
b=20;
printf("Before swapping a =%d b=%d\n",a,b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a =%d b=%d",a,b);
}
Categories of functions:-
Functions are classified in three types, Depending on whether the arguments are present or
not and Whether a value is returned or not.
Type-1:
Type-2:
Type-3:
Parameter passing:-
1) Call-by-value:- It means the values are passed from calling function to called
function.
Example :-
swap(a,b);
}
Write a C program to explain the concept of call-by-value(swapping of two
numbers).
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
a=10;
b=20;
printf("Before swapping a =%d b=%d\n",a,b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a =%d b=%d",a,b);
}
Output:-
Before swapping a =10 b=20
After swapping a =20 b=10
Example :- swap(&a,&b);
Storage classes:
Syntax auto datatype var; exten datatype var; statc datatype var; register datatype var;
void display()
{
int b=5;
static int c=5;
b--;
c--;
printf("b=%d c=%d\n",b,c);
}
Output:- a =10 d=20
b=4 c=4
b=4 c=3
Block structure:-
The block structure indicates the scope of the variable.
Example:-
The variable ‘c’ is available to only block 1 and ‘a’ is available to block2 and block1.
Write a C program to explain block structure.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a=10;
clrscr();
{
int b=20,sum;
sum=a+b;
printf("sum is: %d",sum);
}
getch();
}
Output:- sum is : 30
Example:- display(A,n);
}
In the above example we can say that the called function has two parameters:
a) Array name with data-type and
b) No .of elements of the array being passed.
Write a C program to display the 1 D array elements.
#include<stdio.h>
#include<conio.h>
void display(int A[],int n);
void main()
{
int A[10],i,n;
clrscr();
printf("Enter n value:\n");
scanf("%d",&n);
printf("Enter values into matrix A:\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
display(A,n);
getch();
}
void display(int A[],int n)
{
int i;
printf(“values of matrix A are:\n”);
for(i=0;i<n;i++)
{
printf("%d ",A[i]);
}
}
Output:-
Enter n value:
4
Enter values into matrix A:
1234
values of matrix A are:
1 234
Example:- display(A,m,n);
In the above example we can say that the called function has three parameters:
a) Array name with constant value in the second square of brackets.
b) Row size and
c) Column size.
Write a C program to display the 1 D array elements.
#include<stdio.h>
#include<conio.h>
void display(int A[][10],int m,int n);
void main()
{
int A[10][10],m,n,i,j;
clrscr();
printf("Enter m and n values:\n");
scanf("%d%d",&m,&n);
printf("Enter values into matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
display(A,m,n);
getch();
}
void display(int A[][10],int m,int n)
{
int i,j;
printf(" Values of matrix A are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",A[i][j]);
}
printf("\n");
}
}
Library functions in C language are inbuilt functions which are grouped together and
placed in a common place called library. Now a list of the most common libraries and a
brief description of the most useful functions they contain follows:
Write a C program to implement the library functions and header files concept.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
void main()
output:
{
Square root of x is :3.000000
float x=9.0,y=9.43;
Ceil of y is:10.000000
char ch='a';
a is lower case letter
clrscr();
printf("Square root of x is : %f\n",sqrt(x));
printf("Ceil of y is: %f\n",ceil(y));
if(isupper(ch))
{
printf("%c is upper case letter",ch);
}
else
{
printf("%c is lower case letter",ch);
}
getch();
}
C Preprocessor
Pre-processor is the program which expands the source program. That means it attach the
header files code to the given program. Finally the expanded source program is submitted to
the C compiler.
}#endif
#ifdef----#else----#end if directives:
#ifdef <identifier>
{
}
#else
{
}
#endif
Write a C program to explain the pre-processor directives(finding the area of a circle
and sum of two numbers).
#include<stdio.h>
#include<conio.h>
#define pi 3.14
#define sum
void main()
{
int r=5,x=10,y=20,z;
float area;
clrscr();
area=pi*r*r;
#ifdef sum
z=x+y;
#else
z=x-y;
#endif
printf("Area of circle = %f\n",area);
printf("sum =%d",z);
getch();
}
Output:
Area of circle =78.500000
Sum=30
Recursive functions
Definition:- “ The function which calls itself is called recursive function”.
Recursive functions are classified into two types:
i) Direct recursive (function calls directly)
ii) In-direct recursive (function calls indirectly)
The following are the conditions(criteria ) of recursive function:
a) Base criteria and
b) Recursive criteria.
The recursive function which satisfies the above two criteria is called well-defined
recursive function.
Rules for recursive function:-
i) Function should be called itself otherwise recursion does not takes place.
ii) Only user-defined functions are involved in the recursion but not pre-defined.
iii) To stop the recursion, the function should met the base criteria.
iv) It needs the stack memory for implementing the recursion.
Advantages of recursion:-
i) It is very flexible in data structures like stacks, ques , linked lists and trees.
ii) Using recursion, the length of the program is reduced.
Disadvantages of recursion:-
The following are the seven minimum number of moves takes place to win the game TOH.
Output:-
Move disk1 from tower1 to tower3
Move disk 2from tower1 to tower2
Move disk1 from tower3 to tower 2
Move disk3 from tower1 to tower 3
Move disk1 from tower2 to tower 1
Move disk 2from tower2 to tower 3
Move disk1 from tower1 to tower 3
Fibonacci sequence
Def:- “The sequence which starts with 0,1 and successive elements are obtained by summing
the preceding two numbers “ is called Fibonacci sequence.
0 1 1 2 3 5 8 13 21 34 etc
0 1 1 2 3 -----------------------
#include<stdio.h>
#include<conio.h>
int fibonacci(int n);
void main()
{
int n,fibno,i;
clrscr();
printf(" Enter number of elements to be generated:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
fibno=fibonacci(i);
printf("%d ",fibno);
}
getch();
}
int fibonacci(int n)
{
if(n==0)
{
return 0;
}
else
{
if(n==1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
}
Output:-
Enter number of elements to be generated:
5
0 1 1 2 3