FUNCTIONS
SHAHIJA P V
HSST COMPUTER APPLICATION
ANJARAKANDY HSS
KANNUR
SHAHIJA PV
HSST COMPUTER APPLICATION
ANJARAKANDY HSS,KANNUR
Concept of modular
programming
• In C++, function is a way to divide large
programs into smaller sub programs.
• The process of breaking large programs into
smaller sub programs is called
modularization. The kind of approach is
known as modular programming.
• The sub programs are generally called
functions.
• Every C++ program has at least one function,
which is main(),start and end of the C++
program within main().
Merits of modular
programming
• Reduces the size of the program:
The modular approach helps to isolate the repeating task. Thus
program size is reduced.
• Less chance of error occurrence:
When the size of program is reduced, naturally syntax errors
will be minimized because in a modularized program
• Reduces programming complexity:
If we properly divide the program into smaller conceptual
units, the development of the program logic will be simpler.
• Improves reusability:
A function written once may be used later in many other
programs.
Demerits of modular
programming
• proper breaking down of the problem is a
challenging task.
• Each sub problem must be independent of
others.
• Time consuming(at the time of breaking
complex program into sub programs).
Functions in C++
A function is a subprogram that acts on data and often return
value.
syntax of function in C++ :
returntype functionname(argument_list)
{
function body;
}
return Type − A function may return a value. The return type is the
datatype used in the function.
function Name − This is the actual name of the function used for
identify the function.
argument list −The values or variables passed into a function also
known as argument (signature). Parameters are optional;.
function Body − The function body contains a collection of
statements that define what the function does.
Functions are mainly classified into
two.
• Predefined functions(built in functions):
The tasks to be compiled and their
definitions are stored in files called header files. Such
ready-to-use sub programs are called predefined
functions or built-in functions.
• User defined functions:
C++ provides the facility to create our own
functions for some specific tasks, known as user defined
functions.
A function definition provides the actual body of
the function.
Predefined functions
1. Console functions for character input/output
2. Stream functions for input /output operation
3.String functions
4. Mathematical functions
5. Character functions
Console functions for character
input/output
These functions require the inclusion of header file cstdio in
the program.
Input Functions Output functions
• getchar(): • putchar():
This function displays the character.
This function returns the
The argument may be a character
character that is input constant or a variable.
through the keyboard. • char ch = 'B'; //assigns 'B' to the
eg: char ch = getchar(); variable
ch
putchar(ch); //displays 'B' on the
screen
If an integer value convert
to ASCII value and the
corresponding character will be
displayed.
Stream functions for I/O operations
This function perform input/output operations on character
and strings. These functions require the inclusion of header
file iostream in the program.
Input Functions
• get(): • getline():
It can accept a single It is used to read a string or
character or multiple a line from an input stream.
characters (string) through I)cin.getline(line,size);
the keyboard. line is the variable name
char ch, str[10]; size is the maximum no of
ch=cin.get(ch); //accepts a
character and stores in ch. characters
cin.get(ch); //same as above II)cin.getline(line,size,ch);
To accept a string, an array line,size is same as above,
name and size are to be ch is the delimiting character.
given as arguments. In getline() function inputting
cin.get(str,10); characters are [size-1] .
Sample Program using getline()
I) #include<iostream> • Input:
using namespace std; Hello welcome
int main() • Output
{
Hello welcome
char name[20];
cin.getline(name,20);
cout<<name<<endl;
return 0;
}
I) #include<iostream> •Input:
using namespace std; Hello welcome
int main() •Output
{ Hello wel
char name[20];
Note: Here the Delimiting
cin.getline(name,20,’c’); character is ‘c’
cout<<name<<endl;
return 0;
Stream functions for I/O operations
Output functions
• put(): • write():
It is used to display a This function displays the
character constant or the string contained in the
content of a character argument.
variable given as argument.
char str[10]="hello";
char ch='c';
cout.write(str,10);
cout.put(ch); //character 'c'
is The above code segment will
displayed dispaly the string hello
cout.put(65); //character 'A' is followed by 5 white spaces.
printed
String functions.
• The header file cstring is to be included
in the program to use these
functions.String functions are:
a) strlen( )
b) strcpy( )
c) strcat( )
d) strcmp( )
e) strcmpi( )
Mathematical functions
• abs():It is used to find the absolute value of an
integer. It takes an integer as the argument (+ve or
–ve) and returns the absolute value.
syntax :
int abs(int);
The following is an example to show the output of
this function:
int n = -25;
cout << abs(n);
The above program code will display 25. If we want
to find the absolute value of a floating point
number, use fabs() function.
• sqrt()
It is used to find the square root of a
number. The function returns the non-
negative square root of the argument.
syntax :
double sqrt(double);
int n = 25;
float b = sqrt(n);
cout << b; output will be 5.
• pow()
This function is used to find the power of
a number.
Syntax:
double pow(double, double);
The following example shows the working of
this function.
int x = 5, y = 4, z;
z = pow(x, y);
cout << z;
The above program code will display 625.
5*5*5*5 i.e. 625
Character Functions
These functions are used to perform
various operations on characters. The
header file cctype is to be included in a
program.
Character functions are:
i) isupper( )
ii) islower( )
iii) isalpha( )
iv) isdigit(
v)isalnum()
vi)toupper()
vii)tolower()
User-defined functions
• Syntax of userdefined function:
datatype functionname(argument_list)
{
statements in the body;
}
For Eg:
void saywelcome()
{
cout<<"Welcome to the world of functions";
}
Creating user-defined functions
Four different types of definitions
1.Function with no argument and no return value
2.Function with argument and no return value
3.Function with argument and with return value
4.Function with no argument and with return
value
• Function with no argument
and no return value
• Function with no argument
and return value
• Function with argument
and no return value
• Function with argument
and return value
user-defined function in a C++
program
Prototype of functions
• . A function prototype is the declaration of a
function by which compiler is provided with the
information about the function such as the name of
the function, its return type, the number and type
of arguments, and its accessibility.
• Syntax:
datatype functionname(argument_list);
Eg:
int add(int a, int b);
int product(int, int);
The declaration statement may be given outside the
main() function.
Arguments of functions
Arguments are also known as parameters
(signature).These are in two types:
• Formal Arguments
The variables used in the function
definition as arguments are known as
formal arguments.
• Actual Arguments
The constants, variables or expressions
used in the function call are known as
actual (original) arguments.
#include <iostream>
using namespace std;
int sum (int x, int y); //declaring
the function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling the
function
(a,b actual parameters)
cout << c;
}
int sum (int x, int y) // function
definition (x,y are the
formal parameters)
Functions with default arguments
• The initialized formal arguments are called
default arguments.
Eg:
int TimeSec(int H, int M=0, int S=0)
{
int sec = H * 3600 + M * 60 + S;
return sec;
}
Note that the two arguments M and S are
given default value 0.
Methods of calling
functions
Functions are called by their names. If the
function is without argument, it can be
called directly using its name. But for
functions with arguments, we have two
ways to call them,
1. Call by Value
2. Call by Reference
Call by Value
• This method copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
Output:
n = 21
x = 20
When we increase the value of n, it will
not affect the value of the variable x.
Call by Reference
• This method copies the reference of an
argument into the formal parameter.
Inside the function, the reference is used
to access the actual argument used in the
call. This means that changes made to the
parameter affect the argument.
• An ampersand symbol (&) is placed in
between the data type and the variable in
the function header.
Eg:Call by Reference
Output:
n = 21
x = 21
when the argument x is passed to the change() function,
the variable n gets the address of x so that the location will
be shared, so n and x will change
Call by value v/s Call by
reference
Eg for calling a function:
To swap the value of two variables
#include <iostream>
using namespace std;
void swap(int & x, int & y) Output:
{
int t = x;
x = y; Before swapping
y = t;
}
int main()
m= 10 and n= 20
{
int m, n;
m = 10;
After swapping
n = 20;
cout<<"Before swapping m= "<< m <<" and n=
"<<n;
m= 20 and n= 10
swap(m, n);
cout<<"\nAfter swapping m= "<< m <<" and n=
"<<n;
return 0;
Scope and life of variables and functions
Scope of a variable is that part of the program in which it is used.It
is classified into two local scope and global scope.
Illustrate the Scope and life of
variables and functions
Thank you
SHAHIJA PV
HSST COMPUTER APPLICATION
ANJARAKANDY HSS,KANNUR