CHAPTER 10
FUNCTIONS
Modular Programming / Modularization
The process of breaking large programs into smaller sub programs is called modularization.
Merits of Modular programming
• Reduce the size of the program
• Reduce program complexity
• Less chance of error
• Improve re usability
Demerits of Modular programming
• Proper breaking down of the problem is a challenging task
• Each sub program should be independent
Function
Function is a named unit of statements in a program to perform a specific task.
main() is an essential function in C++ program .The execution of the program begins in
main().
Two types of functions
• Predefined functions / built-in functions - ready to use programs
• User defined functions
Arguments / Parameters
Arguments or parameters are the means to pass values from the calling function to the
called
function. .
Return value
The result obtained after performing the task assigned to a function. Some functions do not
return any value
Built- in functions
Type & Function Use Syntax Example
Header File
strlen( ) find the length of the int strlen( string); strlen(“COMPUTER ”) ; //
string ( number of displays 8
characters)
strcpy( ) copy a string to strcpy( string1, char str[20];
another string2); strcpy( str ,“welcome”) ; /
/ the string “welcome ”will be
String stored in the variable str
(cstring)
strcat( ) append one string to strcat( string1,string cout<<strcat(“Welcome ” , “to
another (join two 2) ; C++”) ; // displays Welcome
strings) to C ++
strcmp( ) compare two strings strcmp( string1,strin cout<<strcmp(“Hello ”,”Hello”)
g2) ; ; // displays 0
strcmpi() compare two strings strcmpi( string1,stri cout<<strcmpi(“HELLO” ,
ignoring the cases ng2) “Hello”) ; // displays 0
abs( ) find the absolute int abs( int ) ; cout<<abs(-5) ;
value of a number // displays 5
sqrt( ) Find square root of a double sqrt(double) cout<<sqrt(16) ;
Mathematica number ; // displays 4
l (cmath)
pow( ) find the power of a double pow(double, cout<<pow(3,2) ;
number double) ; // displays 9
sin() find the sine value of double sin( double); cout<<sin(90*3.14/180);
an angle. (angle must be in displays 1
radians)
cos() Find the cosine value double cos( double); cout<<cos(0*3.14/180);
of an angle (angle must be in displays 1
radians)
isupper( to check whether a int isupper (char) ; cout<<isupper('A') ;
) character is in upper // displays 1
case(capital letter) cout<<isupper('d') ;
or not. // displays 0
islower( ) check whether a int islower (char) ; cout<<islower('A') ;
character is in lower // displays 0
case(small letter) or cout<<islower('d') ;
not. // displays 1
isalpha( ) check whether a int isalpha(char) ; cout<<isalpha('B ') ;
character is an // displays 1
alphabet or not. cout<<isalpha('8') ; //displays
0
isdigit( ) check whether a int isdigit( char c) ; cout<<isdigit('8') ; // displays
Character character is digit or 1 cout<<isdigit('b') ; //
(cctype) not displays 0
isalnum() check whether a int isalnum( char) ; cout<<isalnum('8') ; //
character is displays 1
alphanumeric or not. 1cout<<isalnum('a ') ; //
displays 1
cout<<isalnum('+') ; //
displays 0
toupper() convert the given char toupper(char) ; cout<<(char)toupper('b') ; //
character into its displays B
uppercase.
tolower( convert the given char tolower( char) ; cout<<(char)tolower('B '); //
)- character into its displays b
lower case.
Conversion itoa() convert an integer itoa(int n, char c [], int n = 2345;
functions value to string type. int len); char c[10];
(cstdlib) itoa(n, c , 10);
cout << c; // displays "2345"
atoi() returns the integer int atoi(char c[]); int n;
value of the string char c[10] = "2345";
n = atoi(c);
cout << n; // displays
number 2345
I/O setw() set the width for the setw(int); char s[]="hello";
Manipulating subsequent string cout<<s<< setw(10)<<"friends
function ";
(ciomanip) // hello friends
User defined functions
The syntax of a function definition is given below:
data_type function _name (argument list )
{
statements in the body;
}
The data_type is any valid data type of C ++. The function_name is a user defined word
(identifier).
The argument list is a list of parameters, i .e .a list of variables preceded by data types
and separated by commas.
Example 1 Example 2 Example 3 Example 4
int sum(int a , int b) void sum(int a , int b) void sum(i) int sum( i)
{ { { {
int s=a+b ; int s=a+b ; cin>>a>> b ; cin>>a>> b ;
return s; cout<<”Sum=”<<s; int s=a+b ; int s=a+b ;
} } cout<<”Sum=”<<s; return s ;
} }
Prototype of functions
A function prototype is the declaration of a function
Syntax : data_type function _name (argument list );
Eg : int sum(int , int ) ;
void sum( ) ;
int sum( ) ;
Arguments of functions
• Arguments or parameters are the means to pass values from the calling function to the
called function.
• The variables used in the function definition as arguments are known as formal
arguments.
• The variables used in the function call are known as actual (original) arguments.
Methods of calling functions
• Call by value (Pass by value )method - a copy of the actual argument is passed to the
function.
• Call by reference (Pass by reference )method - the reference of the actual argument
is
passed to the function.
Difference between Call by value method and Call by reference method
Call by value method Call by reference method
• Ordinary variables are used as formal • Reference variables are used as
parameters. formal parameters.
• Actual parameters may be constants, • Actual parameters will be variables
variables or expressions. only.
• Exclusive memory allocation is • Memory of actual arguments is
required for the formal arguments. shared by formal arguments.
• The changes made in the formal • The changes made in the formal
arguments do not reflect in actual arguments do reflect in actual
arguments. arguments.
Function Sample code Procedure Output
Call Type
void change(int n)
{
n = n + 1; n = 21
cout << "n = " << n << '\n'; x = 20
}
Call void main()
by {
Value int x = 20;
change(x);
cout << "x = " << x;
}
void change(int & n)
{
n = n + 1;
cout << "n = " << n << '\n'; n = 21
Call }
x = 21
by void main()
Reference {
int x=20;
change(x);
cout << "x = " << x;
}
Scope and life of variables and functions
Local variable - declared within a function or a block of statements.
Global variable - declared outside all the functions.
Local function - declared within a function or a block of statements and defined after the
calling function.
Global function - declared or defined outside all other functions.
Recursive function
A function that calls itself is a recursive function. (The process of calling a function by itself is
known as recursion)
Typical format: int function1()
{
............
int n = function1(); //calling itself
............
}
The following is a recursive function that finds the factorial of a number , n.
int factorial( int n)
{
if((n==1)||(n==0))
return 1;
else if (n>1)
return (n * factorial(n-1));
else
return 0;
}
Creation of header files
• Create a file containing the function definitions that you wish to access from your C++
program.
• Save with extension .h
• Now include this file in your C++ program using “ ” instead of < >
eg: #include ”fact h. ”
will include the file fact. h with your C
++program and all functions defined therein can be
accessed.