0% found this document useful (0 votes)
19 views62 pages

Unit - 4

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

Unit - 4

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

UNIT - 4

FUNCTIONS AND pointers

1
FUNCTION
 Divide a large program into the basic building blocks

known as function.

 The function contains the set of programming

statements enclosed by {}.

 A function can be called multiple times to provide

reusability and modularity to the C program. 


2
Advantage of User-Defined Functions


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 

1. A function must be declared globally.


 Function call 

1.Function can be called from anywhere in the program.

2.The parameter list must be same in function calling and declaration.


 Function definition

1. It contains the actual statements which are to be executed.

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

datatype function_name (parameters list)


{
local variable declaration;
…………………………
body of the function;
…………………………
return(expression);
}

9
How Function Works

• Once a function is called the control passes to the called

function.

• The working of calling function is temporarily stopped.

• When the execution of called function is completed then the

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.

 If you don't have to return any value from the


function, use void for the return type.

 Syntax:
return; (or)
return(expression);
14
Function Prototypes

1. Function with no arguments and no return values.

2. Function with arguments and no return values.

3. Function with arguments and return values.

4. Function with no arguments and with return values.


15
Function with no arguments
and no return values
• No data transfer take place between the calling function and
the called function.

• These functions act independently, i.e. they get input and


display output in the same block.

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

Enter two number:


3
4
Sum is:7

19
2. Function with arguments
and no return values

• Here data transfer take place between the calling


function and the called function.

• It is a one way data communication, i.e. the called


program receives data from calling program but it
does not return any value to the calling program.

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

Enter two number:


2
4
Sum is:6

23
3. Function with arguments
and return values

• Here data transfer take place between the calling


function and the called function as well as between
called function and calling function .

• It is a two way data communication, i.e. the called


program receives data from calling program and it return
some value to the calling program.

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

• Data transfer take place between the called function


and the calling function.

• It is a one way data communication, i.e. the called


program does not receives data from calling program
but it return some value to the calling program.

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

Enter two number:6


7

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

Enter the student regno,name,m1,m2,m3:100


aaa
87
98
78

The student Details are:


100 aaa 263.000000 87.666664

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

REGNO NAME TOTAL Avg GRADE


101 aaa 265.000000 88.333336 d
102 bbb 203.000000 67.666664 A

Noornilo Nafees 52
Program to calculate employee salary slip
Using array of structures
Solution:

1. Create structure with following members

‫ غ‬int ecode for employee code


‫ غ‬char ename[20] for employee name
‫ غ‬char des[20] for employee designation
‫غ‬ Designation should be executive, officer and assistant
‫غ‬ float bs for employee basic salary
‫غ‬ float hra for house rent allowance
‫غ‬ float da for dearness allowance
‫غ‬ float ma for medical allowance
‫غ‬ float ns of nett salary

Noornilo Nafees 53
2. Calculation:

‫ غ‬hra: If 10% of basic salary is higher than Rs 2000, than HRA


will Rs 2000. Else If 10% of basic salary is lower than Rs 2000,
then HRA will 10% of Basic Salary
‫ غ‬da: 25% of Basic Salary 
‫ غ‬ma: Executive get MA Rs 1000, Officer get MA Rs 700 &
Assistant get MA Rs 500 
‫ غ‬ns: Total of bs+hra+da+ma

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

and basic salary:\n”,i);


scanf(“%d%s%s%f”,&e[i].ecode,&e[i].ename,
&e[i].des,&e[i].bs);

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

You might also like