Unit-4.2 PPT Notes
Unit-4.2 PPT Notes
by
Surendra Kr.Keshari
Assistant Professor
KIET Group of Institutions
1
Functions
A function is said to be a set of instructions that can perform a specific task. It will
be reduce the size of the program by calling and using them at different places in
the program. The advantages are: Re- usability, modularity, overall programming
simplicity. Functions are classified into library functions and user-defined
functions.
User-Defined Functions
2
Advantages of functions:
Functions are like building blocks
They allow complicated programs to be divided into manageable pieces
A programmer can focus on just that part of the program and construct it, debug
it, and perfect it
Different people can work on different functions simultaneously
Can be re-used (even in different programs)
Enhance program readability
3
Library Functions (predefined) functions )
The functions that are already available in the C++ library are called Library
functions. Some important categories of library functions are : Input/Output
functions (iostream.h), Mathematical functions (Math.h), String handling functions
(String.h), Console input/output functions (conio.h) and Manipulator functions
(iomanip.h).
Some example of the predefined mathematical functions are:
sqrt(x)
pow(x, y)
floor(x)
4
Predefined Functions
(continued)
5
sample run:
6
User-Defined Functions
A function defined by the user is called as user-defined function.
The advantage of user-defined function is that the complexity of a program is
reduced and the errors can be traced easily.
Function Prototyping (or declaration): The prototype describes the function interface
to the compiler by giving details such as the number and type of arguments and the
type of return values.
Function Definition: Function definition defines the operations of a function which is
already declared. When the function definition comes before the main() function, we
need not declare it.
Function call : The process of calling a function for execution is known as function
call. Any user defined function can call any other function. There are two types of
function call: Call by value and Call by
7
1. Function with parameters (arguments) and without return values.
#include <iostream.h>
#include <conio.h>
void main( )
{
int a=10, b=10;
void add(int,int);
clrscr();
add(a,b);
getch();
}
void add(int x, int y)
{
cout<< x+y;
} 8
2. Function with parameters (arguments) and with a return value.
#include <iostream.h>
#include <conio.h>
void main( )
{
int a=10, b=10;
int add(int,int);
clrscr();
cout << add(a,b);
getch();
}
int add(int x, int y)
{
return(x+y);
} 9
3. Function without parameter (argument) and with a return value.
#include <iostream.h>
#include <conio.h>
void main( )
{
int add();
clrscr();
cout << add();
getch();
}
int add()
{
int x=10, y=10;
return(x+y);
}
10
4. Function without parameter (argument) and without a return value.
#include <iostream.h>
#include <conio.h>
void main( )
{
void add();
clrscr();
add();
getch();
}
void add()
{
int x=10, y=10;
cout << (x+y);
}
11
Parameter Passing in Functions
Call By Value :
When a function is called by supplying values as parameters (arguments), it is called
Call By Value. The values placed in the actual parameters are copied into the formal
parameters. So any changes made to the formal parameters do not affect the actual
parameters.
Call By Reference:
When we pass parameters (arguments) by reference, the formal parameters in the
called function become aliases to the actual parameters in the calling function. So any
change made in the formal parameters affect the original value.
12
Return By Reference:
A function can also return a reference.
int & max(int &x, int &y)
{ if(x>y)
return x;
else
return y;
}
Since the return type of max( ) is int &, the function returns reference to x and y (and
not the value). Then function call such as max(a,b) will yield a reference to either a or
b depending on their values. This means that this function call can appear on the Left-
hand side of an assignment statement. That is, the statement max(a,b) = -1; is legal and
assigns -1 to a if it is larger, otherwise -1 to b.
13
Inline Function
One of the objectives of using functions in a program is to save some memory
space, which becomes appreciable when a function is likely to be called many
times.
Every time a function is called, it takes a lot of extra time is executing a series of
instructions for tasks such as jumping to the functions, saving registers, pushing
arguments into stack, and returning to the calling function.
eliminate the cost of calls to small functions, C++ proposes a new feature called
inline function.
14
Inline Function
Functions that are not actually called but, rather, are expanded in line, at the point
of each call.
Advantage
• Have no overhead associated with the function call and return mechanism.
• Can be executed much faster than normal functions.
• Safer than parameterized macros.
Disadvantage
• If they are too large and called too often, the program grows
larger.
15
Inline Function
Syntax:
Inline function-header
{
function body
}
16
Automatic in-lining
Defining a member function inside the class declaration causes the function to
automatically become an in-line function.
In this case, the inline keyword is no longer necessary.
However, it is not an error to use it in this situation.
Restrictions: Same as normal in-line functions.
17
Automatic in-lining
18
Inline Function (cont…)
Some of the situations where inline expansion may not work are:
For functions returning values, if a loop, a switch, or a goto exists.
For functions not returning values, if a return statement exists.
If functions contain static variables.
If inline functions are recursive.
19
Macros
Macros are a piece of code in a program which is given some name. Whenever this
name is encountered by the compiler the compiler replaces the name with the actual
piece of code. The ‘#define’ directive is used to define a macro. Let us now
understand the macro definition with the help of a program:
#include <iostream>
// macro definition
#define LIMIT 5
int main()
{
for (int i = 0; i < LIMIT; i++)
{
std::cout << i << "\n";
• }
return 0;
}
20
Macros with arguments
We can also pass arguments to macros. Macros defined with arguments works
similarly as functions.
#include <iostream>
// macro with parameter
#define AREA(l, b) (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
std::cout << "Area of rectangle is: " << area;
return 0;
}
21
BASIS FOR
INLINE MACRO
COMPARISON
Basic Inline functions are parsed by the compiler. Macros are expanded by the preprocessor.
Expansion The compiler may not inline and expand all the functions. Macros are always expanded.
22
BASIS FOR
INLINE MACRO
COMPARISON
The short functions, defined inside the class are
Automation Macros should be defined specifically.
automatically made onto inline functions.
An inline member function can access the data members of Macros can never be the members of the class and
Accessing
the class. can not access the data members of the class.
23
Function Overloading
Provides the mechanism by which C++ achieves one type of polymorphism
(called compile-time polymorphism).
Two or more functions can share the same name as long as either
• The type of their arguments differs, or
• The number of their arguments differs, or
• Both of the above
The compiler will automatically select the correct version.
The return type alone is not a sufficient difference to allow function overloading.
24
Function Overloading (continued)
• Correct function overloading:
• Syntax error:
25
Default Arguments
In C++, A default argument is a value provided in a function declaration that is
automatically assigned by the compiler if the caller of the function doesn’t provide a
value for the argument with a default value.
Example:
26
Thank
You
27