SlideShare a Scribd company logo
1 of 70
C++ Functions
2
Agenda
 What is a function?
 Types of C++
functions:
 Standard functions
 User-defined functions
 C++ function structure
 Function signature
 Function body
 Declaring and
Implementing C++
functions
 Sharing data among
functions through
function parameters
 Value parameters
 Reference parameters
 Const reference
parameters
 Scope of variables
 Local Variables
 Global variable
3
Functions and subprograms
 The Top-down design appeoach is based on dividing the
main problem into smaller tasks which may be divided
into simpler tasks, then implementing each simple task
by a subprogram or a function
 A C++ function or a subprogram is simply a chunk of
C++ code that has
 A descriptive function name, e.g.
 computeTaxes to compute the taxes for an employee
 isPrime to check whether or not a number is a prime number
 A returning value
 The computeTaxes function may return with a double number
representing the amount of taxes
 The isPrime function may return with a Boolean value (true or false)
4
C++ Standard Functions
 C++ language is shipped with a lot of functions
which are known as standard functions
 These standard functions are groups in different
libraries which can be included in the C++
program, e.g.
 Math functions are declared in <math.h> library
 Character-manipulation functions are declared in
<ctype.h> library
 C++ is shipped with more than 100 standard libraries,
some of them are very popular such as <iostream.h>
and <stdlib.h>, others are very specific to certain
hardware platform, e.g. <limits.h> and <largeInt.h>
5
Example of Using
Standard C++ Math Functions
#include <iostream.h>
#include <math.h>
void main()
{
// Getting a double value
double x;
cout << "Please enter a real number: ";
cin >> x;
// Compute the ceiling and the floor of the real number
cout << "The ceil(" << x << ") = " << ceil(x) << endl;
cout << "The floor(" << x << ") = " << floor(x) << endl;
}
6
Example of Using
Standard C++ Character Functions
#include <iostream.h> // input/output handling
#include <ctype.h> // character type functions
void main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "The toupper(" << ch << ") = " << (char) toupper(ch) << endl;
cout << "The tolower(" << ch << ") = " << (char) tolower(ch) << endl;
if (isdigit(ch))
cout << "'" << ch <<"' is a digit!n";
else
cout << "'" << ch <<"' is NOT a digit!n";
}
Explicit casting
7
User-Defined C++ Functions
 Although C++ is shipped with a lot of standard
functions, these functions are not enough for all
users, therefore, C++ provides its users with a
way to define their own functions (or user-
defined function)
 For example, the <math.h> library does not
include a standard function that allows users to
round a real number to the ith digits, therefore,
we must declare and implement this function
ourselves
8
How to define a C++ Function?
 Generally speaking, we define a C++
function in two steps (preferably but not
mandatory)
 Step #1 – declare the function signature in
either a header file (.h file) or before the main
function of the program
 Step #2 – Implement the function in either an
implementation file (.cpp) or after the main
function
9
What is The Syntactic Structure of
a C++ Function?
 A C++ function consists of two parts
 The function header, and
 The function body
 The function header has the following
syntax
<return value> <name> (<parameter list>)
 The function body is simply a C++ code
enclosed between { }
10
Example of User-defined
C++ Function
double computeTax(double income)
{
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}
11
double computeTax(double income)
{
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}
Example of User-defined
C++ Function
Function
header
12
Example of User-defined
C++ Function
double computeTax(double income)
{
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}
Function
header
Function
body
13
Function Signature
 The function signature is actually similar to
the function header except in two aspects:
 The parameters’ names may not be specified
in the function signature
 The function signature must be ended by a
semicolon
 Example
double computeTaxes(double) ;
Unnamed
Parameter
Semicolon
;
14
Why Do We Need Function
Signature?
 For Information Hiding
 If you want to create your own library and share it with
your customers without letting them know the
implementation details, you should declare all the
function signatures in a header (.h) file and distribute
the binary code of the implementation file
 For Function Abstraction
 By only sharing the function signatures, we have the
liberty to change the implementation details from time
to time to
 Improve function performance
 make the customers focus on the purpose of the function, not
its implementation
15
Example
#include <iostream>
#include <string>
using namespace std;
// Function Signature
double getIncome(string);
double computeTaxes(double);
void printTaxes(double);
void main()
{
// Get the income;
double income = getIncome("Please enter
the employee income: ");
// Compute Taxes
double taxes = computeTaxes(income);
// Print employee taxes
printTaxes(taxes);
}
double computeTaxes(double income)
{
if (income<5000) return 0.0;
return 0.07*(income-5000.0);
}
double getIncome(string prompt)
{
cout << prompt;
double income;
cin >> income;
return income;
}
void printTaxes(double taxes)
{
cout << "The taxes is $" << taxes << endl;
}
16
Building Your Libraries
 It is a good practice to build libraries to be
used by you and your customers
 In order to build C++ libraries, you should
be familiar with
 How to create header files to store function
signatures
 How to create implementation files to store
function implementations
 How to include the header file to your
program to use your user-defined functions
17
C++ Header Files
 The C++ header files must have .h
extension and should have the following
structure
 #ifndef compiler directive
 #define compiler directive
 May include some other header files
 All functions signatures with some comments
about their purposes, their inputs, and outputs
 #endif compiler directive
18
TaxesRules Header file
#ifndef _TAXES_RULES_
#define _TAXES_RULES_
#include <iostream>
#include <string>
using namespace std;
double getIncome(string);
// purpose -- to get the employee
income
// input -- a string prompt to be
displayed to the user
// output -- a double value
representing the income
double computeTaxes(double);
// purpose -- to compute the taxes for
a given income
// input -- a double value
representing the income
// output -- a double value
representing the taxes
void printTaxes(double);
// purpose -- to display taxes to the
user
// input -- a double value
representing the taxes
// output -- None
#endif
19
TaxesRules Implementation File
#include "TaxesRules.h"
double computeTaxes(double
income)
{
if (income<5000) return 0.0;
return 0.07*(income-5000.0);
}
double getIncome(string prompt)
{
cout << prompt;
double income;
cin >> income;
return income;
}
void printTaxes(double taxes)
{
cout << "The taxes is $" << taxes
<< endl;
}
20
Main Program File
#include "TaxesRules.h"
void main()
{
// Get the income;
double income =
getIncome("Please enter the employee income: ");
// Compute Taxes
double taxes = computeTaxes(income);
// Print employee taxes
printTaxes(taxes);
}
21
Inline Functions
 Sometimes, we use the keyword inline to define
user-defined functions
 Inline functions are very small functions, generally,
one or two lines of code
 Inline functions are very fast functions compared to
the functions declared without the inline keyword
 Example
inline double degrees( double radian)
{
return radian * 180.0 / 3.1415;
}
22
Example #1
 Write a function to test if a number is an
odd number
inline bool odd (int x)
{
return (x % 2 == 1);
}
23
Example #2
 Write a function to compute the distance
between two points (x1, y1) and (x2, y2)
Inline double distance (double x1, double y1,
double x2, double y2)
{
return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}
24
Example #3
 Write a function to compute n!
int factorial( int n)
{
int product=1;
for (int i=1; i<=n; i++) product *= i;
return product;
}
25
Example #4
Function Overloading
 Write functions to return with the maximum number of
two numbers
inline int max( int x, int y)
{
if (x>y) return x; else return y;
}
inline double max( double x, double y)
{
if (x>y) return x; else return y;
}
An overloaded
function is a
function that is
defined more than
once with different
data types or
different number
of parameters
26
Sharing Data Among
User-Defined Functions
 There are two ways to share data
among different functions
 Using global variables (very bad practice!)
 Passing data through function parameters
Value parameters
Reference parameters
Constant reference parameters
27
C++ Variables
 A variable is a place in memory that has
 A name or identifier (e.g. income, taxes, etc.)
 A data type (e.g. int, double, char, etc.)
 A size (number of bytes)
 A scope (the part of the program code that can use it)
 Global variables – all functions can see it and using it
 Local variables – only the function that declare local variables
see and use these variables
 A life time (the duration of its existence)
 Global variables can live as long as the program is executed
 Local variables are lived only when the functions that define
these variables are executed
28
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
29
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x 0
30
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x 0
void main()
{
f2();
cout << x << endl ;
}
1
31
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x 0
void main()
{
f2();
cout << x << endl ;
}
1
void f2()
{
x += 4;
f1();
}
2
4
32
4
5
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x
void main()
{
f2();
cout << x << endl ;
}
1
void f2()
{
x += 4;
f1();
}
3
void f1()
{
x++;
}
4
33
4
5
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x
void main()
{
f2();
cout << x << endl;
}
1
void f2()
{
x += 4;
f1();
}
3
void f1()
{
x++;
}
5
34
4
5
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x
void main()
{
f2();
cout << x << endl;
}
1
void f2()
{
x += 4;
f1();
}
6
35
4
5
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x
void main()
{
f2();
cout << x << endl;
}
7
36
4
5
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
x
void main()
{
f2();
cout << x << endl;
}
8
37
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
38
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
39
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
0
x
void main()
{
x+=4;
x++;
cout << x << endl;
}
1
The inline keyword
instructs the compiler
to replace the function
call with the function
body!
40
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
4
x
void main()
{
x+=4;
x++;
cout << x << endl;
}
2
41
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
5
x
void main()
{
x+=4;
x++;
cout << x << endl;
}
3
42
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
5
x
void main()
{
x+=4;
x++;
cout << x << endl;
}
4
43
What Happens When We Use
Inline Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
44
What is Bad About Using
Global Vairables?
 Not safe!
 If two or more programmers are working together in a
program, one of them may change the value stored in
the global variable without telling the others who may
depend in their calculation on the old stored value!
 Against The Principle of Information Hiding!
 Exposing the global variables to all functions is
against the principle of information hiding since this
gives all functions the freedom to change the values
stored in the global variables at any time (unsafe!)
45
Local Variables
 Local variables are declared inside the function
body and exist as long as the function is running
and destroyed when the function exit
 You have to initialize the local variable before
using it
 If a function defines a local variable and there
was a global variable with the same name, the
function uses its local variable instead of using
the global variable
46
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
47
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 0
Global variables are
automatically initialized to 0
48
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 0
void main()
{
x = 4;
fun();
cout << x << endl;
}
1
49
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
2
void fun()
{
int x = 10;
cout << x << endl;
}
x ????
3
50
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
2
void fun()
{
int x = 10;
cout << x << endl;
}
x 10
3
51
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
2
void fun()
{
int x = 10;
cout << x << endl;
}
x 10
4
52
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
2
void fun()
{
int x = 10;
cout << x << endl;
}
x 10
5
53
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
6
54
Example of Defining and Using
Global and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun();
cout << x << endl;
}
7
55
II. Using Parameters
 Function Parameters come in three
flavors:
 Value parameters – which copy the values of
the function arguments
 Reference parameters – which refer to the
function arguments by other local names and
have the ability to change the values of the
referenced arguments
 Constant reference parameters – similar to
the reference parameters but cannot change
the values of the referenced arguments
56
Value Parameters
 This is what we use to declare in the function signature or
function header, e.g.
int max (int x, int y);
 Here, parameters x and y are value parameters
 When you call the max function as max(4, 7), the values 4 and 7
are copied to x and y respectively
 When you call the max function as max (a, b), where a=40 and
b=10, the values 40 and 10 are copied to x and y respectively
 When you call the max function as max( a+b, b/2), the values 50
and 5 are copies to x and y respectively
 Once the value parameters accepted copies of the
corresponding arguments data, they act as local
variables!
57
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 0
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
1
58
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
2
void fun(int x )
{
cout << x << endl;
x=x+5;
}
3
3
59
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
2
void fun(int x )
{
cout << x << endl;
x=x+5;
}
3
4
8
60
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
2
void fun(int x )
{
cout << x << endl;
x=x+5;
}
3
8
5
61
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
6
62
Example of Using Value
Parameters and Global Variables
#include <iostream.h>
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
x 4
void main()
{
x = 4;
fun(x/2+1);
cout << x << endl;
}
7
63
Reference Parameters
 As we saw in the last example, any changes in
the value parameters don’t affect the original
function arguments
 Sometimes, we want to change the values of the
original function arguments or return with more
than one value from the function, in this case we
use reference parameters
 A reference parameter is just another name to the
original argument variable
 We define a reference parameter by adding the & in
front of the parameter name, e.g.
double update (double & x);
64
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
1 x
? x
4
65
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
2
x
? x
4
void fun( int & y )
{
cout<<y<<endl;
y=y+5;
}
3
66
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
2
x
? x
4
void fun( int & y )
{
cout<<y<<endl;
y=y+5;
}
4 9
67
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
2
x
? x
9
void fun( int & y )
{
cout<<y<<endl;
y=y+5;
}
5
68
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
6
x
? x
9
69
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
void main()
{
int x = 4;
fun(x);
cout << x << endl;
}
x
? x
9
7
70
Constant Reference Parameters
 Constant reference parameters are used under
the following two conditions:
 The passed data are so big and you want to save
time and computer memory
 The passed data will not be changed or updated in
the function body
 For example
void report (const string & prompt);
 The only valid arguments accepted by reference
parameters and constant reference parameters
are variable names
 It is a syntax error to pass constant values or
expressions to the (const) reference parameters

More Related Content

Similar to power point presentation on object oriented programming functions concepts

Similar to power point presentation on object oriented programming functions concepts (20)

Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
DATA ABSTRACTION.pptx
DATA ABSTRACTION.pptxDATA ABSTRACTION.pptx
DATA ABSTRACTION.pptx
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
 

More from bhargavi804095

Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!bhargavi804095
 
Apache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.pptApache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.pptbhargavi804095
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...bhargavi804095
 
A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...bhargavi804095
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...bhargavi804095
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...bhargavi804095
 
Graphs in data structures are non-linear data structures made up of a finite ...
Graphs in data structures are non-linear data structures made up of a finite ...Graphs in data structures are non-linear data structures made up of a finite ...
Graphs in data structures are non-linear data structures made up of a finite ...bhargavi804095
 
power point presentation to show oops with python.pptx
power point presentation to show oops with python.pptxpower point presentation to show oops with python.pptx
power point presentation to show oops with python.pptxbhargavi804095
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionbhargavi804095
 
Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationbhargavi804095
 
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF CTHE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF Cbhargavi804095
 
Chapter24.pptx big data systems power point ppt
Chapter24.pptx big data systems power point pptChapter24.pptx big data systems power point ppt
Chapter24.pptx big data systems power point pptbhargavi804095
 
power point presentation on pig -hadoop framework
power point presentation on pig -hadoop frameworkpower point presentation on pig -hadoop framework
power point presentation on pig -hadoop frameworkbhargavi804095
 
hadoop distributed file systems complete information
hadoop distributed file systems complete informationhadoop distributed file systems complete information
hadoop distributed file systems complete informationbhargavi804095
 

More from bhargavi804095 (15)

Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!
 
Apache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.pptApache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.ppt
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...
 
Graphs in data structures are non-linear data structures made up of a finite ...
Graphs in data structures are non-linear data structures made up of a finite ...Graphs in data structures are non-linear data structures made up of a finite ...
Graphs in data structures are non-linear data structures made up of a finite ...
 
power point presentation to show oops with python.pptx
power point presentation to show oops with python.pptxpower point presentation to show oops with python.pptx
power point presentation to show oops with python.pptx
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaion
 
Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentation
 
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF CTHE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
 
Chapter24.pptx big data systems power point ppt
Chapter24.pptx big data systems power point pptChapter24.pptx big data systems power point ppt
Chapter24.pptx big data systems power point ppt
 
power point presentation on pig -hadoop framework
power point presentation on pig -hadoop frameworkpower point presentation on pig -hadoop framework
power point presentation on pig -hadoop framework
 
hadoop distributed file systems complete information
hadoop distributed file systems complete informationhadoop distributed file systems complete information
hadoop distributed file systems complete information
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

power point presentation on object oriented programming functions concepts

  • 2. 2 Agenda  What is a function?  Types of C++ functions:  Standard functions  User-defined functions  C++ function structure  Function signature  Function body  Declaring and Implementing C++ functions  Sharing data among functions through function parameters  Value parameters  Reference parameters  Const reference parameters  Scope of variables  Local Variables  Global variable
  • 3. 3 Functions and subprograms  The Top-down design appeoach is based on dividing the main problem into smaller tasks which may be divided into simpler tasks, then implementing each simple task by a subprogram or a function  A C++ function or a subprogram is simply a chunk of C++ code that has  A descriptive function name, e.g.  computeTaxes to compute the taxes for an employee  isPrime to check whether or not a number is a prime number  A returning value  The computeTaxes function may return with a double number representing the amount of taxes  The isPrime function may return with a Boolean value (true or false)
  • 4. 4 C++ Standard Functions  C++ language is shipped with a lot of functions which are known as standard functions  These standard functions are groups in different libraries which can be included in the C++ program, e.g.  Math functions are declared in <math.h> library  Character-manipulation functions are declared in <ctype.h> library  C++ is shipped with more than 100 standard libraries, some of them are very popular such as <iostream.h> and <stdlib.h>, others are very specific to certain hardware platform, e.g. <limits.h> and <largeInt.h>
  • 5. 5 Example of Using Standard C++ Math Functions #include <iostream.h> #include <math.h> void main() { // Getting a double value double x; cout << "Please enter a real number: "; cin >> x; // Compute the ceiling and the floor of the real number cout << "The ceil(" << x << ") = " << ceil(x) << endl; cout << "The floor(" << x << ") = " << floor(x) << endl; }
  • 6. 6 Example of Using Standard C++ Character Functions #include <iostream.h> // input/output handling #include <ctype.h> // character type functions void main() { char ch; cout << "Enter a character: "; cin >> ch; cout << "The toupper(" << ch << ") = " << (char) toupper(ch) << endl; cout << "The tolower(" << ch << ") = " << (char) tolower(ch) << endl; if (isdigit(ch)) cout << "'" << ch <<"' is a digit!n"; else cout << "'" << ch <<"' is NOT a digit!n"; } Explicit casting
  • 7. 7 User-Defined C++ Functions  Although C++ is shipped with a lot of standard functions, these functions are not enough for all users, therefore, C++ provides its users with a way to define their own functions (or user- defined function)  For example, the <math.h> library does not include a standard function that allows users to round a real number to the ith digits, therefore, we must declare and implement this function ourselves
  • 8. 8 How to define a C++ Function?  Generally speaking, we define a C++ function in two steps (preferably but not mandatory)  Step #1 – declare the function signature in either a header file (.h file) or before the main function of the program  Step #2 – Implement the function in either an implementation file (.cpp) or after the main function
  • 9. 9 What is The Syntactic Structure of a C++ Function?  A C++ function consists of two parts  The function header, and  The function body  The function header has the following syntax <return value> <name> (<parameter list>)  The function body is simply a C++ code enclosed between { }
  • 10. 10 Example of User-defined C++ Function double computeTax(double income) { if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); return taxes; }
  • 11. 11 double computeTax(double income) { if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); return taxes; } Example of User-defined C++ Function Function header
  • 12. 12 Example of User-defined C++ Function double computeTax(double income) { if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); return taxes; } Function header Function body
  • 13. 13 Function Signature  The function signature is actually similar to the function header except in two aspects:  The parameters’ names may not be specified in the function signature  The function signature must be ended by a semicolon  Example double computeTaxes(double) ; Unnamed Parameter Semicolon ;
  • 14. 14 Why Do We Need Function Signature?  For Information Hiding  If you want to create your own library and share it with your customers without letting them know the implementation details, you should declare all the function signatures in a header (.h) file and distribute the binary code of the implementation file  For Function Abstraction  By only sharing the function signatures, we have the liberty to change the implementation details from time to time to  Improve function performance  make the customers focus on the purpose of the function, not its implementation
  • 15. 15 Example #include <iostream> #include <string> using namespace std; // Function Signature double getIncome(string); double computeTaxes(double); void printTaxes(double); void main() { // Get the income; double income = getIncome("Please enter the employee income: "); // Compute Taxes double taxes = computeTaxes(income); // Print employee taxes printTaxes(taxes); } double computeTaxes(double income) { if (income<5000) return 0.0; return 0.07*(income-5000.0); } double getIncome(string prompt) { cout << prompt; double income; cin >> income; return income; } void printTaxes(double taxes) { cout << "The taxes is $" << taxes << endl; }
  • 16. 16 Building Your Libraries  It is a good practice to build libraries to be used by you and your customers  In order to build C++ libraries, you should be familiar with  How to create header files to store function signatures  How to create implementation files to store function implementations  How to include the header file to your program to use your user-defined functions
  • 17. 17 C++ Header Files  The C++ header files must have .h extension and should have the following structure  #ifndef compiler directive  #define compiler directive  May include some other header files  All functions signatures with some comments about their purposes, their inputs, and outputs  #endif compiler directive
  • 18. 18 TaxesRules Header file #ifndef _TAXES_RULES_ #define _TAXES_RULES_ #include <iostream> #include <string> using namespace std; double getIncome(string); // purpose -- to get the employee income // input -- a string prompt to be displayed to the user // output -- a double value representing the income double computeTaxes(double); // purpose -- to compute the taxes for a given income // input -- a double value representing the income // output -- a double value representing the taxes void printTaxes(double); // purpose -- to display taxes to the user // input -- a double value representing the taxes // output -- None #endif
  • 19. 19 TaxesRules Implementation File #include "TaxesRules.h" double computeTaxes(double income) { if (income<5000) return 0.0; return 0.07*(income-5000.0); } double getIncome(string prompt) { cout << prompt; double income; cin >> income; return income; } void printTaxes(double taxes) { cout << "The taxes is $" << taxes << endl; }
  • 20. 20 Main Program File #include "TaxesRules.h" void main() { // Get the income; double income = getIncome("Please enter the employee income: "); // Compute Taxes double taxes = computeTaxes(income); // Print employee taxes printTaxes(taxes); }
  • 21. 21 Inline Functions  Sometimes, we use the keyword inline to define user-defined functions  Inline functions are very small functions, generally, one or two lines of code  Inline functions are very fast functions compared to the functions declared without the inline keyword  Example inline double degrees( double radian) { return radian * 180.0 / 3.1415; }
  • 22. 22 Example #1  Write a function to test if a number is an odd number inline bool odd (int x) { return (x % 2 == 1); }
  • 23. 23 Example #2  Write a function to compute the distance between two points (x1, y1) and (x2, y2) Inline double distance (double x1, double y1, double x2, double y2) { return sqrt(pow(x1-x2,2)+pow(y1-y2,2)); }
  • 24. 24 Example #3  Write a function to compute n! int factorial( int n) { int product=1; for (int i=1; i<=n; i++) product *= i; return product; }
  • 25. 25 Example #4 Function Overloading  Write functions to return with the maximum number of two numbers inline int max( int x, int y) { if (x>y) return x; else return y; } inline double max( double x, double y) { if (x>y) return x; else return y; } An overloaded function is a function that is defined more than once with different data types or different number of parameters
  • 26. 26 Sharing Data Among User-Defined Functions  There are two ways to share data among different functions  Using global variables (very bad practice!)  Passing data through function parameters Value parameters Reference parameters Constant reference parameters
  • 27. 27 C++ Variables  A variable is a place in memory that has  A name or identifier (e.g. income, taxes, etc.)  A data type (e.g. int, double, char, etc.)  A size (number of bytes)  A scope (the part of the program code that can use it)  Global variables – all functions can see it and using it  Local variables – only the function that declare local variables see and use these variables  A life time (the duration of its existence)  Global variables can live as long as the program is executed  Local variables are lived only when the functions that define these variables are executed
  • 28. 28 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }
  • 29. 29 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 0
  • 30. 30 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 0 void main() { f2(); cout << x << endl ; } 1
  • 31. 31 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 0 void main() { f2(); cout << x << endl ; } 1 void f2() { x += 4; f1(); } 2 4
  • 32. 32 4 5 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl ; } 1 void f2() { x += 4; f1(); } 3 void f1() { x++; } 4
  • 33. 33 4 5 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl; } 1 void f2() { x += 4; f1(); } 3 void f1() { x++; } 5
  • 34. 34 4 5 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl; } 1 void f2() { x += 4; f1(); } 6
  • 35. 35 4 5 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl; } 7
  • 36. 36 4 5 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl; } 8
  • 37. 37 I. Using Global Variables #include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }
  • 38. 38 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; }
  • 39. 39 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; } 0 x void main() { x+=4; x++; cout << x << endl; } 1 The inline keyword instructs the compiler to replace the function call with the function body!
  • 40. 40 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; } 4 x void main() { x+=4; x++; cout << x << endl; } 2
  • 41. 41 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; } 5 x void main() { x+=4; x++; cout << x << endl; } 3
  • 42. 42 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; } 5 x void main() { x+=4; x++; cout << x << endl; } 4
  • 43. 43 What Happens When We Use Inline Keyword? #include <iostream.h> int x = 0; Inline void f1() { x++; } Inline void f2() { x+=4; f1();} void main() { f2(); cout << x << endl; }
  • 44. 44 What is Bad About Using Global Vairables?  Not safe!  If two or more programmers are working together in a program, one of them may change the value stored in the global variable without telling the others who may depend in their calculation on the old stored value!  Against The Principle of Information Hiding!  Exposing the global variables to all functions is against the principle of information hiding since this gives all functions the freedom to change the values stored in the global variables at any time (unsafe!)
  • 45. 45 Local Variables  Local variables are declared inside the function body and exist as long as the function is running and destroyed when the function exit  You have to initialize the local variable before using it  If a function defines a local variable and there was a global variable with the same name, the function uses its local variable instead of using the global variable
  • 46. 46 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; }
  • 47. 47 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 0 Global variables are automatically initialized to 0
  • 48. 48 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 0 void main() { x = 4; fun(); cout << x << endl; } 1
  • 49. 49 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 2 void fun() { int x = 10; cout << x << endl; } x ???? 3
  • 50. 50 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 2 void fun() { int x = 10; cout << x << endl; } x 10 3
  • 51. 51 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 2 void fun() { int x = 10; cout << x << endl; } x 10 4
  • 52. 52 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 2 void fun() { int x = 10; cout << x << endl; } x 10 5
  • 53. 53 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 6
  • 54. 54 Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; fun(); cout << x << endl; } 7
  • 55. 55 II. Using Parameters  Function Parameters come in three flavors:  Value parameters – which copy the values of the function arguments  Reference parameters – which refer to the function arguments by other local names and have the ability to change the values of the referenced arguments  Constant reference parameters – similar to the reference parameters but cannot change the values of the referenced arguments
  • 56. 56 Value Parameters  This is what we use to declare in the function signature or function header, e.g. int max (int x, int y);  Here, parameters x and y are value parameters  When you call the max function as max(4, 7), the values 4 and 7 are copied to x and y respectively  When you call the max function as max (a, b), where a=40 and b=10, the values 40 and 10 are copied to x and y respectively  When you call the max function as max( a+b, b/2), the values 50 and 5 are copies to x and y respectively  Once the value parameters accepted copies of the corresponding arguments data, they act as local variables!
  • 57. 57 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 0 void main() { x = 4; fun(x/2+1); cout << x << endl; } 1
  • 58. 58 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 2 void fun(int x ) { cout << x << endl; x=x+5; } 3 3
  • 59. 59 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 2 void fun(int x ) { cout << x << endl; x=x+5; } 3 4 8
  • 60. 60 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 2 void fun(int x ) { cout << x << endl; x=x+5; } 3 8 5
  • 61. 61 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 6
  • 62. 62 Example of Using Value Parameters and Global Variables #include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() { x = 4; fun(x/2+1); cout << x << endl; } x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 7
  • 63. 63 Reference Parameters  As we saw in the last example, any changes in the value parameters don’t affect the original function arguments  Sometimes, we want to change the values of the original function arguments or return with more than one value from the function, in this case we use reference parameters  A reference parameter is just another name to the original argument variable  We define a reference parameter by adding the & in front of the parameter name, e.g. double update (double & x);
  • 64. 64 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 1 x ? x 4
  • 65. 65 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; y=y+5; } 3
  • 66. 66 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; y=y+5; } 4 9
  • 67. 67 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 2 x ? x 9 void fun( int & y ) { cout<<y<<endl; y=y+5; } 5
  • 68. 68 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 6 x ? x 9
  • 69. 69 Example of Reference Parameters #include <iostream.h> void fun(int &y) { cout << y << endl; y=y+5; } void main() { int x = 4; // Local variable fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } x ? x 9 7
  • 70. 70 Constant Reference Parameters  Constant reference parameters are used under the following two conditions:  The passed data are so big and you want to save time and computer memory  The passed data will not be changed or updated in the function body  For example void report (const string & prompt);  The only valid arguments accepted by reference parameters and constant reference parameters are variable names  It is a syntax error to pass constant values or expressions to the (const) reference parameters