Chapter 6 - Modular Concept
Chapter 6 - Modular Concept
CHAPTER 6:
Modular Concept
INTRODUCTION TO FUNCTION
• 2 types of functions :-
• Predefined function or built-in or library function
• Independent function or user-defined function
PREDEFINED FUNCTION
• iostream.h
Name Description
cin For console input (keyboard)
cout For console output (screnn)
PREDEFINED FUNCTION
• iomanip.h
Name Description
setw(x) Set field width to x
setfill(x) Set the fill character with x
setprecision(x) Set the floating point precision to x
setiosflags(ios::fixed) Display floating point values in decimal
notation
setiosflags(ios::left) Left justify output
• string.h
Name Description
• char.h
Name Description
toupper(c) Converts character c from lowercase to
uppercase letter
tolower(c) Converts character c from uppercase to
lowercase letter
isupper(c) Returns true if c is an uppercase letter
islower(c) Returns true if c is an lowercase letter
isdigit(c) Returns true if c is a digit
isalpha(c) Returns true if c is an alphanumeric character
isspace(c) Returns true if c is a space character
PREDEFINED FUNCTION
#include <iostream.h>
#include <ctype.h>
void main()
{ char letter;
cout<<"Enter a charater: ";
cin>>letter;
if (isalpha)
{ if (isdigit(letter))
cout<<"The character is a digit\n";
else if (isupper(letter))
{ cout<<"The character is an uppercase.\n"
<< "The lowercase is "
<< char(tolower(letter)) <<endl;
}
else
{ cout<<"The character is an lowercase.\n"
<<"The uppercase is " <<
PREDEFINED FUNCTION
• math.h
Name Description
#include <iostream.h>
#include <math.h>
void main()
{
int number;
cout<<"Enter a number: ";
cin >> number;
• stdlib.h
Name Description
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
void main()
{
unsigned seed;
float randValue;
cout<< "Enter the value of seed ";
cin>>seed;
srand(seed);
cout<<"\n 5 random generated numbers are:\n";
for (int i= 1; i<=5; i++)
{
randValue = rand();
cout<<setw(8) <<randValue <<endl;
}
}
INDEPENDENT FUNCTION
• Known as user-defined / programmer-defined functions
• Whose tasks are determined by programmer
• Tasks are define within the program in the function is used
Syntax form:
#include “<filename>.cpp”
E.G
#include”myfile.cpp”
PROGRAM STRUCTURE USING
INDEPENDENT FUNCTION
#include <iostream.h>
Function prototype declarations;
Syntax:
Function-Name(actual parameters);
findMin(num1, num2);
getSum(firstNum, secNum);
AFTER COMPLETING A
FUNCTION CALL
• Two possibilities:
• Function call that returns no value
• Does no return any value to the calling function.
void main()
{ Actual Parameter
Function
int x = 4;
call
calSum(10, 8, x+3);
}
RULES FOR PARAMETER
PASSING
• The type of actual must be the same with the formal
parameter.
Void main()
{ Function
statement; Call
…………. Actual Parameter
int num1 = 10; 10 20 30
int net_total = sum3Num(num1, 20, 30);
cout<<net_total;
}
Formal Parameter a=10 b=20 c=30
Function sum3Num
int sum3Num(int a, int b, int c) Total = 10+20+30
{
int total = a+b+c;
return total;
} 60
Types of Independent
Functions
1. Function with no return value and no parameter
void swap(int &, int { int x, y; void swap(int &a, int &b)
&); cin >> x >>y; { int temp;
swap(x, y); temp = a;
} a = b;
b = temp;
}
void calc(int, int, int { int x, y, total; Void calc(int a, int b, int &jum, float &purata)
&, float &); float avg; {
cin >> x >>y; jum = a + b;
calc(x, y, total, purata = jum/2;
avg); }
}