Unit 3 CPDS
Unit 3 CPDS
FUNCTIONS
A function is a self contained program segment that carries out a specific,
well defined task. A large problem has to be split in the smaller tasks, so that
it can be efficiently solved. This is where the functions are useful.
Structure Of A Function:-
Function_name(argument list)
E.g.:-
argument declaration; add(x,y)
{ int x,y;
Local variable declaration; {
Statement-1; int r;
Statement-2; r=x+y;
Statement-3; return(r);
Return(expression); }
}
1
Unit - 3 CMREC - CSE Department
The function name must follow the same rules of formation as other
variable names in c. The argument list contains valid variable separated with
comma‟s, with parenthesis ().These are called formal arguments, these
receive information from the arguments given in calling function thus
providing means of data communication from the calling function to called
function.
The variables that are useful with in the functions should be declared
internally within the called function. The list of statements (statement1,
statement2,……) in function are body of function. The return statement is
used to take the result given by function to calling function or empty to
calling function. The arguments given in calling function are called actual
arguments.
E.g.
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,c;
scanf(“%d%d”,&a,&b);
c=add(a,b); /*calling function*/
printf(“%d”,c);
getch();
}
add(x,y) /*called function*/
int x,y; /* formal args declaration*/
{
int r;
r=x+y;
return(r);
}
A Function is called from the main programme by using its name, including
the parenthesis which follows the name.
2
Unit - 3 CMREC - CSE Department
NOTE:-
No data
F1 ( ) F2 ( )
{ {
-- --
-- --
F2( ); }
} No data
In return
3
Unit - 3 CMREC - CSE Department
E.g.:-
main( )
{
square( );
getch( );
}
square( )
{
int a;
scanf( )
{
int a;
scanf(“%d”,&a);
printf(“%d”,a*a);
}
o/p:-2
4
Explanation: - Whenever the compiler reaches calling function, the control
immediately goes function definition to perform the logic. The calling
function accepts internal variable „a‟ through scanf( ), and calculates “a*a”
and prints the result. Here there is no data communication between calling
function and called function. So, no arguments no return type. The square
() function receives data from terminal directly the return; is optional in
function, when the function has nothing to return.
F1 ( ) Values of
F2 ( )
{ arguments {
-- --
-- --
-- --
F2(a); no data in }
}
4
Unit - 3 CMREC - CSE Department
E.g :-
main( )
{
int a;
scanf(“%d”,&a);
square(a);
getch();
}
square(b)
int b;
{
int c;
c=b*b;
printf(“%d”,c);
}
o/p:- 2
4
Values of
F1 ( ) arguments F2(b)
{ {
-- --
-- --
-- --
result of Return(e);
F2(a); }
}
function
5
Unit - 3 CMREC - CSE Department
E.g :-
main( )
{
int a,b;
scanf(“%d”,&a);
b=square(a);
printf(“%d”,b);
getch( );
}
square(x)
int x;
{
return(x*x);
}
o/p:- 2
4
Return;
or
Return (Expression);
More than one return can be used in a function. How the function will
return when the first is encountered.
6
Unit - 3 CMREC - CSE Department
Note: A function can return only single return type though it is having
several return statements
E.g:
The functions generally returns integer but it you want the function to
return other than integer like float, double etc; then also you have to
prototype the function. Here the prototype is done in declaration.
E.g:
float div (float,float);
{
float a,b,c;
printf(“enter a, b”);
scanf(“%d%d”,&a,&b);
c=div (a,b);
printf(“%f”,c);
getch( );
}
7
Unit - 3 CMREC - CSE Department
Definition:-
float div(float x, float y)
{
return(x/y);
}
E.g :-
main( )
{
print(“welcome”);
star( );
getch( );
}
star( )
{
printf(“*****”);
}
Using Void:-
void main( )
{
void start( );
printf(“welcome”);
getch( );
}
void star( )
{
printf(“*****”);
}
The above program consists of star ( ) a user defined program but returned
nothing such functions can be declared that the qualifier “void”
This states that given function do not return values. We can also use void
for the functions not taking arguments, as
E.g. :- void star (Void) /* specifies no returned values*/
8
Unit - 3 CMREC - CSE Department
Parameter Passing :-
Call by value:-
E.g.:-
void main( )
{
int x=3,y=4;
clrscr( );
printf(“\n before swapping are %d\t%d ”,x,y);
swap(x,y);
getch( );
}
swap(x,y)
int x,y;
{
int temp;
temp=x;
x=y;
y=temp;
printf(“after swapping are %d\t%d\n”x,y);
return;
}
9
Unit - 3 CMREC - CSE Department
The actual parameters are x and y. Here we are calling the function f() by
sending x and y variables. Hence it is call by value. In function f (),
actual parameter x is copied to formal parameter x, and actual parameter
y is copied to formal parameter y, then ( x=3 and y = 4 in f ( ) at first)
x=4 and y= 3
The out put of printf in function f() is 43.when the function is exited
(completed), formal parameters are destroyed, then the control returns to the
main function. Actual parameters x and y still remains 3 & 4. Thus, charges
made of formal parameters are not reflected to actual parameters. It we want
to reflect them we have to call the function by call by reference using
pointers.
Call by Reference:-
E.g.:-
main( )
{
int a,b;
printf(“enter the values of a,b”);
scanf(“%d%d”,&a,&b);
printf(“\n before swapping a=%d \t b=%d”,a,b);
swap(&a,&b);
10
Unit - 3 CMREC - CSE Department
printf(“\n after swapping a=%d\t b=%d”,a,b);
getch( );
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
return(0);
}
Let us assume & a is 1000 & b is 2000. Here we are calling the function f1
by sending addresses hence it is known as call by reference, in the function
f1, x=&a=1000; y=&b=2000.Hence x & y contains address of x & y,
P1&P2 are called pointer variables.
a b
3 4
x=1000 y=2000
The actual parameters cannot be used in a function f1( ) and the formal
parameters cannot be used in function main. In f1( ) *x1&*x2 are inter
changed.
a b
4 3
4
x1=1000 y1=2000
11
Unit - 3 CMREC - CSE Department
Storage Classes (Or) Scope of Variables (Or) Visible Class of
Variables:-
Every „C‟ variable has a characteristic called as storage class. The
storage class derives two characteristics of the variables.
--Life time (longetivity)
--Scope (visibility)
Life Time:- The life time of the variable is the length of time it returns a
particular value.
12
Unit - 3 CMREC - CSE Department
-It assures that we may declare and use the same name variable in the
different function in the same program with out causing any confusion to the
compiler.
Storage - Memory
Default initial value- Garbage value
Scope - Local to the block in which the
variable is defined.
Life - Till the controls with in the block in
which it is defined.
E.g.:-main( )
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf(“%d\n”,i);
}
printf(“%d\n”,i);
}
printf(“%d\n,i”);
}
o/p:-3
2
1
Extern Variables:-
Same variables are alive and active through the entire
program are known as external variables (or) global variables. External
variables are declared of all functions in the program. The global variables
can be accessed by any function in the program.
-Once a variable has been declared as global any function can use it and
change its value.
-Declaration external variable should be with “Extern” key word.
13
Unit - 3 CMREC - CSE Department
Storage --- Memory
Default initial Value --- zero
Scope --- Global
Life --- As long as the programs
Execution does not come
to an end.
E.g.:-
int i;
main( )
{
printf(“%d\n”,i);
incre( );
incre( );
decre( );
}
incre( )
{
i=i+1;
printf(“%d\n”,i);
}
i=i-1;
printf(“%d\n”,i);
}
o/p:-0
1
2
1
E.g.:-
main( ) main( )
{ {
incre( ); int c;
incre( ); for (c=1;c<=3;c++)
incre( ); fun( );
} }
incre( ) fun( )
{ {
static int i=1; static int a=5;
printf(“%d\n”,i); a=a+3
i=i+1; printf(“%d\n”,a);
} }
o/p:-1 o/p:-a=8
2 a=11
3 a=14
Register variables:-
15
Unit - 3 CMREC - CSE Department
Storage - CPU register
Default initial value - Garbage value
Scope - Local to the block in which it is defined
Life - Till the control remains within the block
In which it is defined.
E.g.:-main( )
{
register int i;
for(i=1;i<=10;i++)
printf(“%d”,i);
}
Type qualifiers :-
TYPE QUALIFIERS
16
Unit - 3 CMREC - CSE Department
Constants:-
-The keyboard for the constant type qualifies is const.
-A constant object is a read only object
-A constant object must be initialized when it is declared because it cannot
be charged
later.
Volatile:-
-It tells the computer that an object value may be charged by entities other
than
this program.
-As an example a „c‟ compiler may thinks that it is more efficient to remove
an object from print memory and put is in a register.
In this case, we are telling compiler that this object may be referenced
or charged by other entity.
Volatile int x;
Volatile int x ptr;
The above example shows how an integer or a pointer to an integer can be
declared volatile.
Restrict:-
-The restrict qualifier which is used only with pointers, indicates that the
pointer is only initial way to access the deference data.
-It helps more help to the compiler for optimization.
Ex: - main ()
{
17
Unit - 3 CMREC - CSE Department
printf (“ welcome to CMREC \n”);
main( );
}
There are two important conditions that must be satisfied by any
recursive procedure.
#include<stdio.h>
#include < conio.h>
main( )
{
int n,x;
clrscr( );
printf(“enter the values of n”);
scanf(“%d”,&n);
r=fact(n);
printf(“%d”,r);
getch( );
}
fact(x)
int x;
{
int s;
if(n!=1)
{
s=*fact(x=1);
return(s);
}
else
return(1);
}
18
Unit - 3 CMREC - CSE Department
Calculating the GCD of given 2 numbers by using recursion.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,r;
clrscr( );
printf(“enter the first number:”);
scanf(“%d”,&a);
printf(“enter the second number:”);
scanf(“%d”,&b);
printf(“gcd of %d and %d is:%d\n”,a,b,r);
getch( );
}
gcd(x,y)
int x,y;
{
if(y= =0)
return x;
else
return(gcd(y,x%y));
}
Preprocessor Commands:-
#include<stdio.h>
#include<conio.h>
#define P1 3.14
void main( )
{
float r,area;
clrscr( );
printf(“\n enter radius of circle in cms”);
scanf(“%f”,&r);
area=p1*r*r;
printf(“area of circle =%2f”,area);
getch( );
}
UNDEFINING A MACRO
syntax:-
Under identifier
20
Unit - 3 CMREC - CSE Department
Program to undefined a macro.
#include<stdio.h>
#include<conio.h>
#define wait getch( )
void main( )
{
int k;
#undef wait ( ) getch( );
clrscr( );
for(k=1;k<=5;k++)
printf(“%d\t”,k);
wait( );
}
#include<stdio.h>
#include<conio.h>
#define say(m) printf(#m)
void main( )
{
clrscr( );
say(Hello);
}
o/p:-Hello
21
Unit - 3 CMREC - CSE Department
Program after conversion the statement say(Hello) is treated as
printf(“Hello”). It
Is not essential be enclose the text in quotation marks in the strigizing
operator
Write a program to find the larger of two numbers using macro with
arguments.
#include<stdio.h>
#include<conio.h>
#define max(x,y) if(x>y)c=x; else c=y;
void main( )
{
int x=5,y=8,c;
clrscr( );
max(x,y);
printf(“\nlarger of 2 numbers=%d”,c);
}
#include<stdio.h>
#include<conio.h>
22
Unit - 3 CMREC - CSE Department
#include”udf.c”
void main( )
{
clrscr( );
display( );
}
Syntax is:
#if!defined(identifier)
#error<error message”);
#end if.
# Line Directive:-
Syntax of line directive is # line<constant>[<identifier>]
--Inline Directive:-
It is important to known previously that the source code contains
assembly code.
#else
{
Stmt-3;
Stmt-4;
}
# End if
24
Unit - 3 CMREC - CSE Department
{
Stmt-1;
Stmt-2;
}
#else
{
Stmt-3;
Stmt-4;
}
#endif
--The #ifdef preprocessor tests whether the identifier has defined substitute
text or not.
--If the identifoier is defined then #ifblock is compiled and executed.
The compiler ignores #else block even if errors are intentionally made.
Error messages will not be displayed.
--If the identifier is not defined then #else block is compiled and executed.
#include<stdio.h>
#include<conio.h>
#defined line-1
void main( )
{
clrscr( );
#ifdef line
printf(“this line number 1”);
#else
printf(“this is line number 2”);
#end if
getch( );
}
Directive Function
#define defines a macro substation.
#undef undefines a macro
#include specifies the files to be included.
#ifdef test for a macro definition.
#if test a compile time condition.
3 Categories:-
1) Macro substation directives.
2) File inclusion directives
3) Compiler control directives.
In the beginning of the program the following lists gives some standard
mathematical functions.
Function Purpose
Abs( ) absolute value of given number.
Ceil( ) given value will be rounded up to
nearest integer
Cos( ) gives cosine value of given number.
Cosh( ) gives hyperbolic cosine of given
number.
Exp( ) raise e to power of even numbers.
Floor given number will be rounded down
to the nearest integer.
Fmod(d1,d2) provides the reminder of d1/d2.
Lod( ) natural logarithm of given numbers.
Pow( d1,d2) d1 raises to the power of d2.
Sign(d) sine of d
Sqrt(d) sqrt of d
Tan( ) tangent of given number.
A sin( ) arc sine of given number(means
sin-1)
A cos( ) arc cosine of given number.
A tan(d) arc tangent of d.
Functions Test
is a num( ) is it alpha numeric.
27
Unit - 3 CMREC - CSE Department
is alpha( ) is it alphabetic.
iscntral( ) is it controlled character.
is digit( ) is it a digit.
islower( ) is it a lower case.
isupper( ) is it a upper case.
isprintf( ) is it printable character.
is space( ) is it a space.
toupper( ) converts the lower case argument to
upper case.
tolower( ) converts the upper case argument to
lower case.
.User-Defined Function:-
It is a function where user has to write . The user defined function
can be called from the other function. In „C‟ language, functions can be
return either a value (or) number.
ARRAYS
DEFINITION:-
28
Unit - 3 CMREC - CSE Department
It‟s mainly used to reducing the complexity of data in your program.
Three types of arrays:-
1) One or single dimensional array.
2) Two or double dimensional array.
3) Multi dimensional array.
It contains one column and multiple rows. Each row can be identify with
row id is
called index. It always starts with zero.
Syntax:-<datatype><var name>[<size>];
|
Subscript
E.g.:- int array[5]
In the above example array is that occupied 20 bytes (5*2) in that
each row with a 2 bytes like that it allocates 5 rows with one column.
OUTPUT:-
Character Memory Location
[A] 4054
[R] 4055
[R] 4056
[A] 4057
[Y] 4058
30
Unit - 3 CMREC - CSE Department
Write program to display names of months in a year using single
dimensional array having length of 12.
O/P:-
#include<stdio.h>
#include<conio.h>
void main( )
32
Unit - 3 CMREC - CSE Department
{
int i,j;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr( );
printf(“array elements and address\n\n”);
printf(“col-0 col-1 col-2\n”);
printf(“row0”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d[%5d]”,a[i][j],&a[i][j]);
}
printf(“\n row%d”,i+1);
}
printf(“\r”);
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr( );
printf(“elements of an array\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%5d”,a[i][j]);
33
Unit - 3 CMREC - CSE Department
}
printf(“\n”);
}
}
O/P:-
Elements of an Array
123
456
789.
Write a program to display the names of the cities with their base
addresses.
Write a c program to find both the large and smallest number in a list
of integers.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,n,largest,smallest;
clrscr( );
printf(“enter the size of array:”);
scanf(“%d”&n);
for(i=0;i<n;i++)
{
printf(“enter a[%d]=”,i);
scanf(“%d”,&a[i]);
}
smallest =a[0];
largest =a[0];
for(i=1;i<10;i++)
{
if(a[i]>largest)
largest=a[i];
if(a[i]<smallest)
34
Unit - 3 CMREC - CSE Department
smallest=a[i]
}
printf(“largest integer is:%d\n”,largest);
printf(“smallest integer is :%d\n”,smallest);
getch( );
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[3][3],b[3][3],c[3][3];
int i,j,row,col;
clrscr( );
printf(“enter how many rows are there:”);
scanf(“%d”,&row);
printf( “enter how many columns are there:”);
scanf(“%d”,&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“enter a[%d][%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“enter b[%d][%d]=”,i=1,j=1);
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
c[i][j]=a[i][j]+b[i][j];
35
Unit - 3 CMREC - CSE Department
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(“%2d”,a[i][j]);
printf(“\t\t”);
for(j=0;j<col;j++)
printf(“%2d”,b[i][j]);
printf(“\t\t”);
for(j=0;j<col;j++)
printf(“%2d”,c[i][j]);
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[3][3],b[3][2],c[3][2],i,j,k;
clrscr( );
printf(“\n enter ,matrix a:\n”);
for(i=o;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“enter a[%d][%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\enter matrix b:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“enter b[%d][%d]=”,I,j);
scanf(“%d”,&b[i][j]);
36
Unit - 3 CMREC - CSE Department
}
}
for(i=0;i<3;i++);
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf(“\n the resultant matrix c is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(“ \t%d ”,c[i][j]);
printf(“ \t%d ”,c[i][j]);
printf(“\n”);
}
getch( );
}
O/P:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,a[3][3];
clrscsr( );
printf(“ a[%d][%d]= ”,i,j);
scanf(“ %d ”,&a[i][j]);
}
printf(“ the elements 3*3 matrix are :\n ”);
for(i=0;i<3;i++)
{
37
Unit - 3 CMREC - CSE Department
printf(“\n\t\t”);
for(j=0;j<3;j++)
printf(“ %d\t ”,a[i][j]);
}
getch( );
}
void main( )
38
Unit - 3 CMREC - CSE Department
{
int array_3d[3][3][3];
int a,b,c;
clrscr( );
for(a=0;a<3;a++)
for(b=0;b<3;b++)
for(c=0;c<3;c++)
array_3d[a][b][c]=a+b+c;
for(a=0;a<3;a++)
{
printf(“\n”);
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
printf(“%d”,array_3d[a][b][c]);
printf(“\n”);
}
}
}
O/P:-
012
123
234
123
234
345
234
345
456
The arrangement of array elements in two rows and column is only true-
since in memory there are no rows and columns. Hence even 2-d array are
arranged linearly in memory.
The arrangement is shown below.
39
Unit - 3 CMREC - CSE Department
4002 4004 4006 4008 4010 4012 4014 4016
Multidimensional arrays:-
C allows of three or more dimensions. The exact limit is
determined by the compiler. The general form of multi dimensional array is
<datatype><array_name><[S1][S2]…………[S3]
Where Si is the size of ith dimensions.
Column 0 1 2
R
O 0 1 2 3
W 1 4 5 6
40
Unit - 3 CMREC - CSE Department
{2, 5, 8}
{3, 6, 9}
};
{
{1, 4, 4}
{2, 4, 7}
{6, 6, 3}
};
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[3][3][3];
int a,b,c;
clrscr( );
for(a=0;a<3;a++)
for(b=0;b<3;b++)
for(c=0;c<3;c++)
a[a][b][c]=a+b+c;
for(a=0;a<3;a++)
{
printf(“\n”);
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
printf(“%3d”,a[a][b][c]);
printf(“\n”);
}
}
getch();
}
41
Unit - 3 CMREC - CSE Department
Passing Array Elements To Function:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[5]={15,20,25,30,35};
int i;
for(i=0;i<5;i++)
write(i,a[i]);
getch();
}
write(int c,int n)
{
printf(“\t a a[%d]=%d\n”,c;n);
}
O/P: - a[0]=15
a[1]=20
a[2]=25
a[3]=30
a[4]=35
42
Unit - 3 CMREC - CSE Department