100% found this document useful (1 vote)
44 views46 pages

CE144 OOPC Unit-04

This document discusses different types of functions in C++. It covers the main function, simple functions, call by reference, return by reference, inline functions, and overloaded functions. The key topics include function prototypes, passing arguments by value, reference, and address, returning values by reference, and using the inline keyword to expand function calls inline for improved performance of small functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
44 views46 pages

CE144 OOPC Unit-04

This document discusses different types of functions in C++. It covers the main function, simple functions, call by reference, return by reference, inline functions, and overloaded functions. The key topics include function prototypes, passing arguments by value, reference, and address, returning values by reference, and using the inline keyword to expand function calls inline for improved performance of small functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

CE144

OBJECT ORIENTED PROGRAMMING


WITH C++

UNIT-4
Functions

N. A. Shaikh
[email protected]
Topics to be covered
 The main function
 Simple functions
 Call by reference
 return by reference
 Inline functions
 Overloaded functions
 Default arguments

Unit 4:Functions Prepared By: Nishat Shaikh


2
Function
 A function is a group of statements that together
perform a task.
 Functions are made for code reusability and for saving
time and space.

Unit 4: Functions Prepared By: Nishat Shaikh


3
Categories of User-defined Functions
 Function with no argument and no return value.
 Function with no argument but return value.
 Function with argument but no return value.
 Function with argument and return value.

Unit 4: Functions Prepared By: Nishat Shaikh


4
Elements of User-defined Functions

Unit 4: Functions Prepared By: Nishat Shaikh


5
The main function
 main() function is the entry point at
which execution of program is started.
 C allows main() function type to be
void. On the other hand C++ does not
allow that.
 In C++, main() returns an integer type
value to the operating system.
Therefore, every main() in C++ should
end with a return(0) statement.
 The explicit use of a return(O)
statement will indicate that the
program was successfully executed
Unit 4: Functions Prepared By: Nishat Shaikh
6
Function Prototyping / Declaration
 Describes number and type of
arguments and the type of return
values.
 When a function is called, the compiler
ensure that proper arguments are
passed, and the return value is treated
correctly.
 Any violation in matching the arguments
or the return types will be caught by the
compiler at the time of compilation NOTE:
Try above
itself. program in C
and C++. Notice
 These checks and controls did not exist the difference
in the conventional C functions.
Unit 4: Functions Prepared By: Nishat Shaikh
7
Function Prototyping / Declaration
 It was introduced first in C++, ANSI C adopt it later on.
 While C++ makes the prototyping essential, ANSI C
makes it optional.

NOTE:
Try above program in C and C++. Notice the difference

Unit 4: Functions Prepared By: Nishat Shaikh


8
Simple Function
 Function Prototyping / declaration
Syntax:

Example:

 Function Calling

 Function Definition

Unit 4: Functions Prepared By: Nishat Shaikh


9
Simple Function

Unit 4: Functions Prepared By: Nishat Shaikh


10
Call by Reference
 Provision of the reference variables in C++ permits us to
pass parameters to the functions by reference.

 When we pass arguments by reference, the formal


arguments become aliases of the actual arguments.

 This means that when the function is working with its


own arguments, it is actually working on the original
data.

Unit 4: Functions Prepared By: Nishat Shaikh


11
Call by /Pass by Value VS Reference VS Address

Basis for Call by Value Call by Reference Call by


Comparison Pointer/Address
Basic A copy of actual Reference of actual Address of actual
parameters is passed parameters is passed parameters is passed
into formal into formal into formal
parameters. parameters. parameters.
Effect Changes in formal Changes in formal Changes in formal
parameters will not parameters will result parameters will result
result in changes in in in
actual parameters. changes in actual changes in actual
parameters. parameters.
Memory Separate memory Same memory Same memory
allocation location is allocated location is allocated location is allocated
for actual and formal for for
parameters. actual and formal actual and formal
parameters. parameters.

Unit 4: Functions Prepared By: Nishat Shaikh


12
Call by /Pass by Value VS Reference VS Address

Basis for Call by Value Call by Reference Call by


Comparison Pointer/Address
Calling
Parameters
Receiving
Parameters

Unit 4: Functions Prepared By: Nishat Shaikh


13
Call by Value Example: Swapping the values of the two variables

Unit 4: Functions Prepared By: Nishat Shaikh


14
Call by Address Example: Swapping the values of the two variables

Unit 4: Functions Prepared By: Nishat Shaikh


15
Call by Reference Example: Swapping the values of the two variables

Unit 4: Functions Prepared By: Nishat Shaikh


16
Return by Reference
 A function can also return a reference.

 Since the return type of max() is int &, function returns


reference to x or y(and not the values).

 Above function call assign -1 to a if it is larger, otherwise


-1 to b.
Unit 4: Functions Prepared By: Nishat Shaikh
17
Return by Reference

Unit 4: Functions Prepared By: Nishat Shaikh


18
Practical 11 : Return by Reference
Write a function called tonLarge() that takes two integer arguments call by
reference and then sets the larger of the two numbers to 100 using Return
by reference.

Unit 4: Functions Prepared By: Nishat Shaikh


19
Return by Reference
Declare an array of 5 double elements and assign some values to it. Create a function
setvalue( ) which takes the index of the array as an input and Returns by Reference
double value and change the value of the array element stored in that index.

Unit 4: Functions Prepared By: Nishat Shaikh


20
Inline functions
 One of the objectives of using functions in a program is
to save some memory space.
 However, every time a function is called, it takes a lot of
extra time in executing a series of instructions for task
such as
• Jumping to functions, saving registers, pushing
arguments into the stack, and returning to the calling
function.
 When a function is small, a substantial percentage of
execution time may be spent in such overheads.

Unit 4: Functions Prepared By: Nishat Shaikh


21
Inline functions
 To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function.
 An inline function is a function that is expanded in line
when it is invoked. That is, the compiler replaces the
function call with the corresponding function code
(something similar to macros expansion).

Syntax:

Unit 4: Functions Prepared By: Nishat Shaikh


22
Inline functions

Unit 4: Functions Prepared By: Nishat Shaikh


23
Inline functions
NOTE:
 All inline functions must be defined before they are
called.
 To make a function inline, prefix the keyword inline to
the function definition.
 Usually, functions are made inline when they are small
enough to be defined in one or two lines.
 If the function grows in size, the speed benefit of inline
function diminish.

Unit 4: Functions Prepared By: Nishat Shaikh


24
Inline functions
 Remember, inlining is only a request to the compiler, not
a command.
 Compiler can ignore the request for inlining if function
definition is too long or too complicated and compile the
function as a normal function.
Some situations where inline expansion may not work:
 If a function contains a loop. (for, while, do-while)
 If a function contains static variables.
 If a function is recursive.
 If a function return type is other than void, and the
return statement doesn’t exist in function body.
 If a function contains switch or goto statement.
Unit 4: Functions Prepared By: Nishat Shaikh
25
Inline functions

Unit 4: Functions Prepared By: Nishat Shaikh


26
Default Arguments
 C++ allows us to call a function without specifying all its
arguments.
 In such cases, the function assigns a default value to the
parameter which does not have a matching argument in
the function call.
 Default values are specified when the function is
declared.
 The compiler looks at the prototype to see how many
arguments a function uses and alerts the program for
possible default values.

Unit 4: Functions Prepared By: Nishat Shaikh


27
Default Arguments

Unit 4: Functions Prepared By: Nishat Shaikh


28
Default Arguments
NOTE: Only the trailing arguments can have default values
and therefore we must add defaults from right to left.

 Default arguments are useful in situations where some


arguments always have the same value.
 Example: bank interest may remain the same for all
customers for a particular period of deposit.

Unit 4: Functions Prepared By: Nishat Shaikh


29
Practical 12 : Default Arguments
Write a function called power () that takes two arguments: a double value for n and
an int for p, and returns the result as double value. Use default argument of 2 for p,
so that if this argument is omitted, the number will be squared. Write a main ()
function that gets values from the user to test this function.

Unit 4: Functions Prepared By: Nishat Shaikh


30
Default Arguments

Volume of Ellipsoid = (4/3) × pi × radius1 × radius2 × radius3. Write a program


having function volume () which takes three float arguments: radius1, radius 2 and
radius3 and returns the volume of an Ellipsoid. Use default argument of 2 for
radius1, 3 for radius2 and 4 for radius3 so that if arguments are omitted then the
volume of Ellipsoid is always 100.48. Write a main ( ) function that gets values from
the user to test this function.

Unit 4: Functions Prepared By: Nishat Shaikh


31
const Arguments
 An argument to a function can be declared as const.
 The qualifier const tells the compiler that the function
should not modify the argument.
 The compiler will generate an error when this condition
is violated.

Unit 4: Functions Prepared By: Nishat Shaikh


32
const Arguments

Unit 4: Functions Prepared By: Nishat Shaikh


33
Overloaded functions
 Overloading refers to the use of the same thing for
different purposes.
 Function overloading is a feature in C++ where two or
more functions can have the same name but different
parameters.
 Function overloading is also known as Compile Time
Polymorphism or static binding.
 The correct function to be invoked is determined by
checking the number and type of the arguments but
not on the function type.

Unit 4: Functions Prepared By: Nishat Shaikh


34
Overloaded functions

Unit 4: Functions Prepared By: Nishat Shaikh


35
Overloaded functions
The function selection involves:
1. First, compiler tries to find an exact match. This is the
case where the actual argument exactly matches the
parameter type of one of the overloaded functions
2. If no exact match is found, C++ tries to find a match
through promotion. (eg: char to int, float to double)
3. If no promotion is possible, C++ tries to find a match
through standard conversion.
4. Finally, compiler tries to find a match through user-
defined conversion
Reference:
https://www.learncpp.com/cpp-tutorial/function-
overloading/
Unit 4: Functions Prepared By: Nishat Shaikh
36
Overloaded functions

Unit 4: Functions Prepared By: Nishat Shaikh


37
Overloaded functions

As per C++ standard, floating point literals


(compile time constants) are treated as
double unless explicitly specified by a suffix

Rectifying the error: We can simply tell the compiler that the literal is a float
and NOT double by providing suffix f.

Unit 4: Functions Prepared By: Nishat Shaikh


38
Practical 10 : Overloaded functions
Define three functions named
divide(). First function takes
numerator and denominator as
an input argument and checks
it’s divisible or not, Second
function takes one int number
as input argument and checks
whether the number is prime
or not and Third function takes
3 float number as argument
and finds out average of the
numbers. Use concept of
Function Overloading / static
binding.

Unit 4: Functions Prepared By: Nishat Shaikh


39
Practical 10 : Overloaded functions

Unit 4: Functions Prepared By: Nishat Shaikh


40
Overloaded functions
Define four function void swap ()
which accepts two arguments by
reference and swap the values.
First function swaps two
characters, second function
swaps two integers, third function
swaps two floats values and
fourth function swaps two double
values. Use the concept of call by
reference in all four functions and
function overloading and inline
function.

Unit 4: Functions Prepared By: Nishat Shaikh


41
Overloaded functions

Unit 4: Functions Prepared By: Nishat Shaikh


42
Overloaded functions
Define three functions
named convert (). First
function takes rupees as an
input argument and converts
into paisa, second function
takes meter as an input
argument and converts into
centimeter and last function
takes character as an input
argument and converts
entered small character into
capital. Here, rupee is in
integer and meter is in float.
Use concept of Function
Overloading.

Unit 4: Functions Prepared By: Nishat Shaikh


43
Unit 4: Functions Prepared By: Nishat Shaikh
44
Math Library Functions

NOTE:
• Include header file math.h
/ cmath
• X and y are of type double
• All function return data
type double
Unit 4: Functions Prepared By: Nishat Shaikh
45
End of Unit-4

You might also like