Unit - 4
Unit - 4
1
FUNCTION
Divide a large program into the basic building blocks
known as function.
Avoid rewriting same logic/code again and again in a program.
Call C functions any place in a program.
large C program easily when it is divided into multiple functions.
Code Reusability.
3
Function Aspects
There are three aspects of a C function.
Function declaration
4
Function Aspects
5
Types
6
Pre-Defined Functions
• The pre-defined functions or library functions
are built-in functions.
• The user can use the functions, but cannot
modify the function.
• Example: sqrt()
7
User-Defined Functions
• The functions defined by the user for their
requirement are called user-defined functions.
• Whenever it is needed, The user can modify
the function.
• Example: sum(a,b)
8
Create Function
Syntax
9
How Function Works
function.
control return back to the calling function and execute the next
statement.
10
11
Parameters
Actual Parameter
These are the parameters transferred from the
calling function to the called function.
Formal Parameter
These are the parameters which is used in the
called function.
12
13
return Statement
In C function may or may not return a value from
the function.
Syntax:
return; (or)
return(expression);
14
Function Prototypes
16
17
Example
#include <stdio.h>
#include<conio.h>
void add();
void main() //calling function
{
clrscr()
add();
getch();
}
void add() //called function
{
int a,b,c;
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nSum is:%d",c);
}
18
Output
19
2. Function with arguments
and no return values
20
21
#include <stdio.h>
Example
#include<conio.h>
void add(int,int);
void main()
{
int a,b;
clrscr();
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int x,int y) //function with arguments
{
int z;
z=x+y;
printf("\nSum is:%d",z);
}
22
Output
23
3. Function with arguments
and return values
24
25
Example
#include <stdio.h>
#include<conio.h>
int add(int,int);
int main()
{
int a,b,c;
clrscr();
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("\nSum is:%d",c);
}
int add(int x,int y)
{
int z;
z=x+y;
return(z);
}
26
4. Function with no arguments
and with return values
27
28
#include <stdio.h>
#include<conio.h>
int add();
int main()
{
int d;
d=add();
printf("\nSum is:%d",d);
}
int add()
{
int a,b,c;
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
29
}
Parameter Passing Methods
• Call by value
• Call by reference
Noornilo Nafees 30
Call by value
• Actual argument passed to the formal
argument.
• Any changes to the formal argument does not
affect the actual argument.
Noornilo Nafees 31
Example
#include <stdio.h>
#include<conio.h>
int add(int,int);
int main()
{
int a,b,c;
clrscr();
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("\nSum is:%d",c);
}
int add(int x,int y)
{
int z;
z=x+y;
return(z);
}
Noornilo Nafees 32
Output
Sum is:13
Noornilo Nafees 33
Call by reference
• Instead of passing value, the address of the
argument will be passed.
• Any changes to the formal argument will affect
the actual argument.
Noornilo Nafees 34
Example
#include <stdio.h> swap(&x,&y);
#include<conio.h> getch();
void swap(int*,int*); }
void main() void swap(int *a,int *b)
{ {
int x,y; int c;
c=*a;
clrscr(); *a=*b;
printf("\nEnter value of x:"); *b=c;
scanf("%d",&x); printf("\nx=%d,y=%d",*a,*b);
printf("\nEnter value of y:"); }
scanf("%d",&y);
Output
Enter value of x:5
Enter value of y:6
x=6,y=5
Noornilo Nafees 35
Recursion
• It is a process of calling the same function
itself again and again until some condition
is satisfied.
• Syntax:
func1()
{
………..
func1();
}
Noornilo Nafees 36
Example
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int a;
clrscr();
printf("\nEnter the number:");
scanf("%d",&a);
printf("The factorial of %d! is %d",a,fact(a));
}
Noornilo Nafees 37
int fact(int x)
{
int f;
if(x==1)
return(1);
else
f=x*fact(x-1);
return(f);
}
Output:
Enter the number:5
The factorial of 5! is 120
Noornilo Nafees 38
Example: Working of 3!
Noornilo Nafees 39
Library Function
• It is pre-defined function.
• The library function provides functions like
mathematical, string manipulation etc,.
Noornilo Nafees 40
Example
sqrt(x):
It is used to find the square root of x
Example: sqrt(36) is 6
abs(x):
It is used to find the absolute value of x
Example: abs(-36) is 36
pow(x,y):
It is used to find the value of xy
Example: pow(5,2) is 25
ceil(x):
It is used to find the smallest integer greater than or
equal to x
Example: ceil(7.7) is 8
Noornilo Nafees 41
rand():
It is used to generate a random number.
sin(x):
It is used to find the sine value of x
Example: sin(30) is 0.5
cos(x):
It is used to find the cosine value of x
Example: cos(30) is 0.86
tan(x):
It is used to find the tan value of x
Example: tan(30) is 0.577
Noornilo Nafees 42
toascii(x):
It is used to find the ASCII value of x
Example: toascii(a) is 97
toupper(x):
It is used to convert lowercase character
to uppercase.
Example: toupper(‘a’) is A
toupper(97) is A
tolower(x):
It is used to convert uppercase character
to lowercase.
Example: tolower(‘A’) is a
Noornilo Nafees 43
Structure
• A Structure is a collection of different data
items, that are stored under a common name.
• Syntax:
struct structure_name
{
structure element1;
structure element2;
…………………….
};
Noornilo Nafees 44
• Example:
struct stud
{
int sno;
char name[10];
int mark;
}s;
Noornilo Nafees 45
Example
#include<stdio.h>
#include<conio.h>
struct stud
{
int regno;
char name[10];
int m1;
int m2;
int m3;
};
Noornilo Nafees 46
void main()
{
struct stud s;
float tot,avg;
printf("\nEnter the student regno,name,m1,m2,m3:"); scanf("%d
%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3);
tot=s.m1+s.m2+s.m3;
avg=tot/3;
printf("\nThe student Details are:");
printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg);
Noornilo Nafees 47
Output
Noornilo Nafees 48
Example: Array of Structures
#include<stdio.h>
#include<conio.h>
struct stud
{
int regno;
char name[10],grade;
int m1,m2,m3;
float avg,tot;
};
void main()
{
struct stud s[10];
int i,n;
printf("\nEnter the no.of students:");
scanf("%d",&n);
Noornilo Nafees 49
for(i=0;i<n;i++)
{
printf("\nEnter the student regno,name,m1,m2,m3:");
scanf("%d%s%d%d%d",&s[i].regno,&s[i].name,&s[i].m1,
&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
if(s[i].m1<35||s[i].m2<35||s[i].m3<35)
s[i].grade='f';
else
{
if(s[i].avg>=75)
s[i].grade='d';
else if(s[i].avg>=60)
Noornilo Nafees 50
s[i].grade='A';
else if(s[i].avg>=50)
s[i].grade='B';
else if(s[i].avg>=35)
s[i].grade='C';
}
}
printf("\nSTUDENT MARK LIST\n");
printf("\nREGNO\tNAME\tTOTAL\tAvg\tGRADE");
for(i=0;i<n;i++)
printf("\n%d\t%s\t%f\t%f\t
%c",s[i].regno,s[i].name,s[i].tot,s[i].avg,s[i].grade);
getch();
}
Noornilo Nafees 51
Enter the no.of students:2
Enter the student regno,name,m1,m2,m3:101
aaa
89
98
78
Enter the student regno,name,m1,m2,m3:102
bbb
59
68
76
STUDENT MARK LIST
Noornilo Nafees 52
Program to calculate employee salary slip
Using array of structures
Solution:
Noornilo Nafees 53
2. Calculation:
Noornilo Nafees 54
// Program to calculate employee salary slip using array of
structures
#include<stdio.h>
#include<conio.h>
struct esal
{
int ecode;
char ename[20];
char des[20];
float bs;
float hra;
float da;
float ma;
float ns;
};
Noornilo Nafees 55
void main()
{
int n,i;
struct esal e[n];
clrscr();
printf(“enter total number of employees to calculate
salary slip : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter employee %d code, name, designation
Noornilo Nafees 56
if((e[i].bs*0.1)<2000)
{
e[i].hra=e[i].bs*0.1;
}
else
{
e[i].hra=2000;
}
e[i].da= e[i].bs*0.25;
if(e[i].des==“executive”)
{
e[i].ma=1000;
}
else if(e[i].des==“officer”)
{
e[i].ma=700;
}
else
{
e[i].ma=500;
} Noornilo Nafees 57
e[i].ns= e[i].bs+ e[i].hra+ e[i].da+ e[i].ma;
}
printf(“\nEmployee Salary Slip\n***************************);
for(i=0;i<n;i++)
{
printf(“Employee code : %d\nEmployee name : %s\n
Deisgnation : %s\nBasic Salary : %f\n
House Rent Allowance : %f\n
Dearness Allowance : %f\n
Medical Allowance : %f\n
Nett Salary : %f”, e[i].ecode,e[i].ename,
e[i].des,e[i].bs,e[i].hra,e[i].da,e[i].ma,e[i].ns);
}
getch();
}
Noornilo Nafees 58
Union
• An Union is a collection of different data items,
that are stored under a common name. Here
same memory is shared by its members.
• Syntax:
union union _name
{
union element1;
union element2;
…………………
};
Noornilo Nafees 59
• Example:
union result
{
int mark;
float avg;
char grade;
}s;
Noornilo Nafees 60
Example
#include<stdio.h>
#include<conio.h>
union stud
{
int a;
char b[2];
};
void main()
{
union stud c;
Noornilo Nafees 61
c.a=256;
printf("\nc.a value is%d",c.a);
}
Output:
c.a value is256
Noornilo Nafees 62