SlideShare a Scribd company logo
1 of 49
Download to read offline
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1
Haresh Jaiswal
Rising Technologies, Jalna.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2
Function Overview
 Function plays an important role in Program
development.
 Dividing program logic in functions/sub-programs is one
of the major principles of top-down, structured
programming.
 Another advantage of using function is that it is possible
to reduce the size of program by calling & using them at
multiple times in the program.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3
Function Overview
void show(); // declaration/prototype
main()
{
...
show(); // function call
...
}
void show() // function definition
{
... // function body
...
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4
Functions in C++
 C++ has added many new features to functions to make
them more reliable and flexible.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5
What is new in C++ Functions
 Call by Reference using Reference Variable.
 Return by Reference.
 Inline Functions.
 Default Arguments.
 Const Arguments.
 Function Overloading.
 Friend & Virtual Functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6
Call By Reference vs. Call By value
 In traditional C, a function call passes arguments by its
value. The called function creates new set of variables
and copies the value of arguments into them.
 The called function does not have access to the actual
variables in the calling program and can only work on
the copies of values.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7
Example of Call By Value.
int sum(int, int);
main()
{
int a = 4, b = 3, c;
c = sum(a, b);
cout << “Add is : “ << c;
}
int sum(int x, int y)
{
int addition = x + y;
return addition;
}
Add is : 7
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8
Call By Reference vs. Call By value
 This mechanism is fine if the function does not need to
modify the values of the original variables in the calling
program.
 But there may arise some situations where we would
like to change the values of variables in the calling
program.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9
Call By Reference.
 Provisions of the reference variable in c++ permits us to
pass parameters to the function by reference.
 When we pass arguments by reference, the ‘formal’
arguments in the called function became aliases of
‘actual’ arguments in the calling program.
 This means when the called function is working with its
own arguments, it is actually working on the original
variables of calling function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10
Call By Reference.
void swap(int&, int&);
main()
{
int a = 10, b = 20;
swap(a, b);
cout << “A : “ << a << endl << “B : “ << b;
}
// x and y are references
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
A : 20
B : 10
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11
Call By Reference.
 When the function call like
swap(a, b);
executes, then the following initialization occurs.
int &x = a;
int &y = b;
 Any changes made to variable ‘x’ and ‘y’ in swap function
will reflect to variables ‘a’ and ‘b’ of main, because ‘x’ and
‘y’ are simply aliases of ‘a’ and ‘b’
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12
Call By Reference using pointers.
void swap(int*, int*);
main()
{
int a = 10, b = 20;
swap(&a, &b);
cout << “A : “ << a << endl << “B : “ << b;
}
// x and y are now pointers
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
A : 20
B : 10
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13
Return By Reference.
 In C++, not only you can pass arguments by reference but
also you can return a reference from a function.
 When a function returns a reference, it returns an implicit
pointer to its return value. This way, a function can be
used on the left hand side of an assignment operator.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
Return By Reference.
int n; // a global variable
int& test(); // function prototype
main()
{
test() = 5;
cout << “N : “ << n;
}
int& test()
{
return n;
}
N : 5
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15
Return By Reference : Explanation
 In this example, the return type of function test() is int&.
Hence this function returns a reference.
 The return statement is return n; but unlike return by
value. This statement doesn't return value of n, instead it
returns variable n itself.
int& test()
{
return n;
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
Return By Reference : Explanation
 When the function is called from main the yield of the
function call is variable n itself, so when the following
statement in main executes it assigns 5 to variable n
test() = 5;
 In simple words, variable n is assigned at the left hand
side of statement test() = 5;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17
Points to keep in mind.
 You cannot return a local variable (which is non-static)
from a function which returns a reference.
 Following piece of code will generate a compile error.
int& test()
{
int n; // n is local variable
return n; // compile error
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18
Points to keep in mind.
 But if I change int n; to static int n; then
 Following piece of code will be successfully compiled and
run perfectly.
int& test()
{
static int n; // n is now a static
// local variable
return n; // perfectly fine
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19
Points to keep in mind.
 Ordinary function returns value but this function doesn't.
Hence, you can't return constant from this function.
 Following piece of code will generate a compile error.
int& test()
{
return 2; // compile error
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20
Return By Reference.
 A function can also return a reference.
int& max(int &x, int &y)
{
if(x>y)
return x;
else
return y;
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21
Return By Reference.
 Since the return type of max() is int &, the
function returns reference to x or y.
 The function call such as max(a, b) will return a
reference either to a or b depending on their
values. That means the function call can
appear on the left hand side of an assignment
operator.
 max(a, b) = 0;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22
Inline Functions
 Every time a function get called it takes a lot of extra
time in executing a series of instructions for tasks, such
as jumping to the function, saving registers, pushing
arguments into the stack, and returning to the calling
function.
 solution to solve this problem is to use inline functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23
Inline Functions
 An Inline function is a function that is expanded in line
when it is invoked.
 The compiler replaces the function call with the
corresponding function code.
 Inline expansion makes program run faster because the
overhead of a function call and return is eliminated.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24
Inline Functions
inline double cube(double n)
{
return (n*n*n);
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25
Inline Functions
 Remember that inline keyword sends a request, not a
command, to the compiler.
 The compiler may ignore this request if the function
definition is too long or too complicated.
 In such cases compiler will compile the function as a
normal function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26
Inline Functions
 Some of the situations where inline expansion may not
work.
 If function contains a loop, switch or goto statement.
 If function contains static variables.
 If function is a recursive function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27
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 parameters.
 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
default values.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 The default value is specified in a manner similar to a
variable initialization.
 Above prototype declares a default value of 7.2 to the
argument irate.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 A function call like
x = interest(5000, 8); // one argument missing
 Passes the value 5000 to amt, and 8 to period and let the
function use default value of 7.2 for irate.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 A function call like
x = interest(5000, 8, 6.3); //no missing argument
 Passes the value 5000 to amt, and 8 to period, and 6.3 for
irate.
 Passes an explicit value of 6.3 to irate
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31
Default arguments
 One important point to note is that the only trailing
arguments can have default values.
 We must add defaults from right to left.
 We cannot provide a default value to a particular
argument in the middle of an argument list.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32
Default arguments
 Some Examples :
int mul(int i, int j=5, int k=10); // legal
int mul(int i, int j=5, int k); // illegal
int mul(int i=2, int j, int k=2); // illegal
int mul(int i=5, int j=2, int k=4); // legal
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33
Default arguments
 Default arguments are useful in situations where some
arguments always have the same value.
 For example bank interest may retain same for all
customers for a particular period of deposits.
 It also provides a greater flexibility to the programmers by
sending all arguments explicitly.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34
const arguments
 In C++, an argument to a function can be declared as
const,
int length(const char p[]);
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 35
const arguments
 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. This type of declaration is
significant only when we pass arguments by
reference or pointers.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 36
Introduction to Class
 Like structures in C, class is a user defined data type in
C++.
 A Class is extension of the idea of structures used in C.
 It is a new way of creating and implementing a user
defined data type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 37
Structures Revised
 Structures in C provides a method of packing different
type of data together.
 A structure is a convenient tool for handling a group of
logically related data items.
 Once the structure type has been defined, we can
create any number of variables of that type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 38
Structures Revised
 Consider following structure.
struct student
{
int rollno;
char name[25];
int marks;
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 39
Structures Revised
 Consider following Declaration.
struct student a;
 ‘a’ is a variable of type ‘student’ and
contains 3 member variables, which
can be accessed by using ‘.’ operator.
a.rollno = 15;
strcpy(a.name, “Aditya”);
a.marks = 435;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 40
Limitations of C Structures
 C structures do not provide the concept of data
hiding.
 Structure members can be directly accessed by the
structure variables by any function in their scope.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 41
Extensions to Structures in C++
 C++ supports all features of structures as defined in C,
in addition C++ has expanded its capabilities to suit its
OOP philosophy.
 It attempts to bring the user-defined types as close as
possible to built in data types, and also provides a
facility to hide the data which is the major principle of
OOP.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 42
Extensions to Structures in C++
 In C++ a structure can contain variables and functions
both as its member.
 It can also declare some of its members as ‘private’ so
that they cannot be accessed directly by the external
functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 43
Extensions to Structures in C++
 In C++, the structure names are stand alone and can
be used like any other type names.
 In other words the keyword ‘struct’ can be omitted
from the declaration of the structure variables.
 For example.
student a; // in c++
struct student a; // in c
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 44
Introduction to Class
 C++ incorporates all of these extensions in
another user defined data type ‘class’.
 There is a very little syntactical difference
between structure and classes in c++.
 The only difference is that by default the
members of a class are private, while, by default,
the members of a structure are public.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 45
Specifying a Class
 A class is a way of binding data and its associated
functions together, it allows the data (and
functions) to be hidden from external world, if
necessary.
 While defining a class we are creating a new
abstract data type, that can be treated like any
other built in data type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 46
Specifying a Class
 Generally a class specifications has two parts.
 Class declaration.
 Class function definitions.
 The class declaration describes the type and the
scope of its members.
 The class function definitions describes how the
class functions are implemented.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 47
General form of a Class
class class_name
{
private: // optional
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 48
General form of a Class
 The functions and variables declared in a class
are called as class members.
 The class members declared as private can be
accessed only within the class, and the
members declared as public can be accessed
from outside of the class also.
 The variables declared inside a class are called
as data members, and the functions are called
as member functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 49
Public Area
Data
Functions
General form of a Class
Class
Entry not allowed for
outside world
X
Entry allowed for
outside world
Private Area
Data
Functions

More Related Content

What's hot

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 

What's hot (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
C++ book
C++ bookC++ book
C++ book
 
Pointer and Object in C++
Pointer and Object in C++Pointer and Object in C++
Pointer and Object in C++
 
Functions
FunctionsFunctions
Functions
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
 
Qno 1 (a)
Qno 1 (a)Qno 1 (a)
Qno 1 (a)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Viewers also liked (6)

Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
functions of C++
functions of C++functions of C++
functions of C++
 

Similar to 02. functions & introduction to class

Inline function
Inline functionInline function
Inline function
Tech_MX
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
Haresh Jaiswal
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
Vivek Singh
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 

Similar to 02. functions & introduction to class (20)

Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Inline function
Inline functionInline function
Inline function
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
11 functions
11 functions11 functions
11 functions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ functions
C++ functionsC++ functions
C++ functions
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

02. functions & introduction to class

  • 1. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.
  • 2. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2 Function Overview  Function plays an important role in Program development.  Dividing program logic in functions/sub-programs is one of the major principles of top-down, structured programming.  Another advantage of using function is that it is possible to reduce the size of program by calling & using them at multiple times in the program.
  • 3. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3 Function Overview void show(); // declaration/prototype main() { ... show(); // function call ... } void show() // function definition { ... // function body ... }
  • 4. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4 Functions in C++  C++ has added many new features to functions to make them more reliable and flexible.
  • 5. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5 What is new in C++ Functions  Call by Reference using Reference Variable.  Return by Reference.  Inline Functions.  Default Arguments.  Const Arguments.  Function Overloading.  Friend & Virtual Functions.
  • 6. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6 Call By Reference vs. Call By value  In traditional C, a function call passes arguments by its value. The called function creates new set of variables and copies the value of arguments into them.  The called function does not have access to the actual variables in the calling program and can only work on the copies of values.
  • 7. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7 Example of Call By Value. int sum(int, int); main() { int a = 4, b = 3, c; c = sum(a, b); cout << “Add is : “ << c; } int sum(int x, int y) { int addition = x + y; return addition; } Add is : 7 Output
  • 8. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8 Call By Reference vs. Call By value  This mechanism is fine if the function does not need to modify the values of the original variables in the calling program.  But there may arise some situations where we would like to change the values of variables in the calling program.
  • 9. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9 Call By Reference.  Provisions of the reference variable in c++ permits us to pass parameters to the function by reference.  When we pass arguments by reference, the ‘formal’ arguments in the called function became aliases of ‘actual’ arguments in the calling program.  This means when the called function is working with its own arguments, it is actually working on the original variables of calling function.
  • 10. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10 Call By Reference. void swap(int&, int&); main() { int a = 10, b = 20; swap(a, b); cout << “A : “ << a << endl << “B : “ << b; } // x and y are references void swap(int &x, int &y) { int temp = x; x = y; y = temp; } A : 20 B : 10 Output
  • 11. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11 Call By Reference.  When the function call like swap(a, b); executes, then the following initialization occurs. int &x = a; int &y = b;  Any changes made to variable ‘x’ and ‘y’ in swap function will reflect to variables ‘a’ and ‘b’ of main, because ‘x’ and ‘y’ are simply aliases of ‘a’ and ‘b’
  • 12. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12 Call By Reference using pointers. void swap(int*, int*); main() { int a = 10, b = 20; swap(&a, &b); cout << “A : “ << a << endl << “B : “ << b; } // x and y are now pointers void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } A : 20 B : 10 Output
  • 13. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13 Return By Reference.  In C++, not only you can pass arguments by reference but also you can return a reference from a function.  When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left hand side of an assignment operator.
  • 14. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14 Return By Reference. int n; // a global variable int& test(); // function prototype main() { test() = 5; cout << “N : “ << n; } int& test() { return n; } N : 5 Output
  • 15. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15 Return By Reference : Explanation  In this example, the return type of function test() is int&. Hence this function returns a reference.  The return statement is return n; but unlike return by value. This statement doesn't return value of n, instead it returns variable n itself. int& test() { return n; }
  • 16. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16 Return By Reference : Explanation  When the function is called from main the yield of the function call is variable n itself, so when the following statement in main executes it assigns 5 to variable n test() = 5;  In simple words, variable n is assigned at the left hand side of statement test() = 5;
  • 17. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17 Points to keep in mind.  You cannot return a local variable (which is non-static) from a function which returns a reference.  Following piece of code will generate a compile error. int& test() { int n; // n is local variable return n; // compile error }
  • 18. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18 Points to keep in mind.  But if I change int n; to static int n; then  Following piece of code will be successfully compiled and run perfectly. int& test() { static int n; // n is now a static // local variable return n; // perfectly fine }
  • 19. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19 Points to keep in mind.  Ordinary function returns value but this function doesn't. Hence, you can't return constant from this function.  Following piece of code will generate a compile error. int& test() { return 2; // compile error }
  • 20. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20 Return By Reference.  A function can also return a reference. int& max(int &x, int &y) { if(x>y) return x; else return y; }
  • 21. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21 Return By Reference.  Since the return type of max() is int &, the function returns reference to x or y.  The function call such as max(a, b) will return a reference either to a or b depending on their values. That means the function call can appear on the left hand side of an assignment operator.  max(a, b) = 0;
  • 22. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22 Inline Functions  Every time a function get called it takes a lot of extra time in executing a series of instructions for tasks, such as jumping to the function, saving registers, pushing arguments into the stack, and returning to the calling function.  solution to solve this problem is to use inline functions.
  • 23. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23 Inline Functions  An Inline function is a function that is expanded in line when it is invoked.  The compiler replaces the function call with the corresponding function code.  Inline expansion makes program run faster because the overhead of a function call and return is eliminated.
  • 24. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24 Inline Functions inline double cube(double n) { return (n*n*n); }
  • 25. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25 Inline Functions  Remember that inline keyword sends a request, not a command, to the compiler.  The compiler may ignore this request if the function definition is too long or too complicated.  In such cases compiler will compile the function as a normal function.
  • 26. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26 Inline Functions  Some of the situations where inline expansion may not work.  If function contains a loop, switch or goto statement.  If function contains static variables.  If function is a recursive function.
  • 27. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27 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 parameters.  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 default values.
  • 28. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  The default value is specified in a manner similar to a variable initialization.  Above prototype declares a default value of 7.2 to the argument irate.
  • 29. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  A function call like x = interest(5000, 8); // one argument missing  Passes the value 5000 to amt, and 8 to period and let the function use default value of 7.2 for irate.
  • 30. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  A function call like x = interest(5000, 8, 6.3); //no missing argument  Passes the value 5000 to amt, and 8 to period, and 6.3 for irate.  Passes an explicit value of 6.3 to irate
  • 31. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31 Default arguments  One important point to note is that the only trailing arguments can have default values.  We must add defaults from right to left.  We cannot provide a default value to a particular argument in the middle of an argument list.
  • 32. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32 Default arguments  Some Examples : int mul(int i, int j=5, int k=10); // legal int mul(int i, int j=5, int k); // illegal int mul(int i=2, int j, int k=2); // illegal int mul(int i=5, int j=2, int k=4); // legal
  • 33. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33 Default arguments  Default arguments are useful in situations where some arguments always have the same value.  For example bank interest may retain same for all customers for a particular period of deposits.  It also provides a greater flexibility to the programmers by sending all arguments explicitly.
  • 34. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34 const arguments  In C++, an argument to a function can be declared as const, int length(const char p[]);
  • 35. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 35 const arguments  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. This type of declaration is significant only when we pass arguments by reference or pointers.
  • 36. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 36 Introduction to Class  Like structures in C, class is a user defined data type in C++.  A Class is extension of the idea of structures used in C.  It is a new way of creating and implementing a user defined data type.
  • 37. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 37 Structures Revised  Structures in C provides a method of packing different type of data together.  A structure is a convenient tool for handling a group of logically related data items.  Once the structure type has been defined, we can create any number of variables of that type.
  • 38. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 38 Structures Revised  Consider following structure. struct student { int rollno; char name[25]; int marks; };
  • 39. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 39 Structures Revised  Consider following Declaration. struct student a;  ‘a’ is a variable of type ‘student’ and contains 3 member variables, which can be accessed by using ‘.’ operator. a.rollno = 15; strcpy(a.name, “Aditya”); a.marks = 435;
  • 40. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 40 Limitations of C Structures  C structures do not provide the concept of data hiding.  Structure members can be directly accessed by the structure variables by any function in their scope.
  • 41. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 41 Extensions to Structures in C++  C++ supports all features of structures as defined in C, in addition C++ has expanded its capabilities to suit its OOP philosophy.  It attempts to bring the user-defined types as close as possible to built in data types, and also provides a facility to hide the data which is the major principle of OOP.
  • 42. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 42 Extensions to Structures in C++  In C++ a structure can contain variables and functions both as its member.  It can also declare some of its members as ‘private’ so that they cannot be accessed directly by the external functions.
  • 43. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 43 Extensions to Structures in C++  In C++, the structure names are stand alone and can be used like any other type names.  In other words the keyword ‘struct’ can be omitted from the declaration of the structure variables.  For example. student a; // in c++ struct student a; // in c
  • 44. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 44 Introduction to Class  C++ incorporates all of these extensions in another user defined data type ‘class’.  There is a very little syntactical difference between structure and classes in c++.  The only difference is that by default the members of a class are private, while, by default, the members of a structure are public.
  • 45. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 45 Specifying a Class  A class is a way of binding data and its associated functions together, it allows the data (and functions) to be hidden from external world, if necessary.  While defining a class we are creating a new abstract data type, that can be treated like any other built in data type.
  • 46. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 46 Specifying a Class  Generally a class specifications has two parts.  Class declaration.  Class function definitions.  The class declaration describes the type and the scope of its members.  The class function definitions describes how the class functions are implemented.
  • 47. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 47 General form of a Class class class_name { private: // optional variable declarations; function declarations; public: variable declarations; function declarations; };
  • 48. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 48 General form of a Class  The functions and variables declared in a class are called as class members.  The class members declared as private can be accessed only within the class, and the members declared as public can be accessed from outside of the class also.  The variables declared inside a class are called as data members, and the functions are called as member functions.
  • 49. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 49 Public Area Data Functions General form of a Class Class Entry not allowed for outside world X Entry allowed for outside world Private Area Data Functions