C++ Functions
C++ Functions
C TIO
U N
F
+ +
C
AGENDA
Sharing data among
functions through
What is a function? function
Types of C++ functions: parameters
Standard functions Value parameters
User-defined functions Reference parameters
C++ function structure Const reference
Function signature parameters
Function body Scope of variables
Declaring and Local Variables
Global variable
Implementing C++
functions
2
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)
3
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>
4
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;
}
5
EXAMPLE OF USING
STANDARD C++ CHARACTER
FUNCTIONS
#include <iostream.h> // input/output handling
#include <ctype.h> // character type functions
void main()
{ Explicit casting
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";
}
6
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
7
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
8
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 { }
9
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;
}
10
EXAMPLE OF USER-DEFINED
C++ FUNCTION
double computeTax(double income) Function
{ header
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}
11
EXAMPLE OF USER-DEFINED
C++ FUNCTION
double computeTax(double income) Function Function
{ header body
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}
12
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 Semicolon
Parameter ;
13
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
14
EXAMPLE
16
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
17
TAXESRULES HEADER FILE
#ifndef _TAXES_RULES_ double computeTaxes(double);
#define _TAXES_RULES_ // purpose -- to compute the
taxes for a given income
#include <iostream> // input -- a double value
representing the income
#include <string> // output -- a double value
using namespace std; representing the taxes
18
TAXESRULES IMPLEMENTATION FILE
#include "TaxesRules.h"
double computeTaxes(double
income)
{ void printTaxes(double taxes)
if (income<5000) return 0.0; {
return 0.07*(income-5000.0); cout << "The taxes is $" <<
taxes << endl;
} }
19
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);
20
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;
}
21
EXAMPLE #1
22
EXAMPLE #2
Write a function to compute the distance
between two points (x1, y1) and (x2, y2)
23
EXAMPLE #3
Write a function to compute n!
24
EXAMPLE #4
FUNCTION OVERLOADING
Write functions to return with the maximum number
of two numbers
25
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
26
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
27
I. USING GLOBAL VARIABLES
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
28
I. USING GLOBAL VARIABLES
#include <iostream.h>
int x = 0; 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; x 0
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
void main()
{
1 f2();
cout << x << endl ;
}
30
I. USING GLOBAL VARIABLES
#include <iostream.h>
int x = 0; x 0
4
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2(); void f2()
{
cout << x << endl; 2 x += 4;
} f1();
}
void main()
{
1 f2();
cout << x << endl ;
}
31
I. USING GLOBAL VARIABLES
#include <iostream.h>
int x = 0; x 5
4
void f1() { x++; }
void f1()
void f2() { x+=4; f1(); } {
void main() 4 x++;
{ }
#include <iostream.h> x 5
4
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
void main()
cout << x << endl; {
} f2();
7 cout << x << endl;
}
35
I. USING GLOBAL VARIABLES
#include <iostream.h> x 5
4
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
void main()
cout << x << endl; {
} f2();
cout << x << endl;
8 }
36
I. USING GLOBAL VARIABLES
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
37
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;
}
38
WHAT HAPPENS WHEN WE USE
INLINE KEYWORD?
#include <iostream.h> x 0
int x = 0;
Inline void f1() { x++; } The inline keyword
instructs the compiler
Inline void f2() { x+=4; f1();} to replace the function
call with the function
void main() body!
{
f2(); void main()
{
cout << x << endl; 1 x+=4;
} x++;
cout << x << endl;
}
39
WHAT HAPPENS WHEN WE USE
INLINE KEYWORD?
#include <iostream.h> x 4
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2(); void main()
{
cout << x << endl; x+=4;
} 2 x++;
cout << x << endl;
}
40
WHAT HAPPENS WHEN WE USE
INLINE KEYWORD?
#include <iostream.h> x 5
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2(); void main()
{
cout << x << endl; x+=4;
} x++;
3 cout << x << endl;
}
41
WHAT HAPPENS WHEN WE USE
INLINE KEYWORD?
#include <iostream.h> x 5
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2(); void main()
{
cout << x << endl; x+=4;
} x++;
cout << x << endl;
4 }
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;
}
43
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!)
44
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
45
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;
}
46
EXAMPLE OF DEFINING AND
USING GLOBAL AND LOCAL
VARIABLES
#include <iostream.h> x 0
int x; // Global variable
Void fun(); // function signature
Global variables are
automatically initialized to 0
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> x 0
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void main()
void fun()
{
x = 4; x 10
fun();
{
cout << x << endl;
int x = 10;
}
4 cout << x << endl;
}
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} 2 fun();
cout << x << endl;
} 51
EXAMPLE OF DEFINING AND USING
GLOBAL AND LOCAL VARIABLES
#include <iostream.h>
x 4
int x; // Global variable
Void fun(); // function signature
void main()
void fun()
{
x = 4; x 10
fun();
{
cout << x << endl;
int x = 10;
}
cout << x << endl;
5 }
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} 2 fun();
cout << x << endl;
} 52
EXAMPLE OF DEFINING AND USING
GLOBAL AND LOCAL VARIABLES
#include <iostream.h>
x 4
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} fun();
6 cout << x << endl;
} 53
EXAMPLE OF DEFINING AND USING
GLOBAL AND LOCAL VARIABLES
#include <iostream.h>
x 4
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} fun();
cout << x << endl;
7 } 54
II. USING PARAMETERS
55
VALUE PARAMETERS
56
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 0
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4; void main()
fun(x/2+1); {
cout << x << endl; 1 x = 4;
fun(x/2+1);
}
cout << x << endl;
} 57
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x )
} {
3 cout << x << endl;
void main()
x=x+5;
{ }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
x = 4; 3
}
2 fun(x/2+1);
cout << x << endl;
} 58
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x 8
3 )
} {
cout << x << endl;
void main()
4 x=x+5;
{ }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
x = 4;
}
2 fun(x/2+1);
cout << x << endl;
} 59
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x 3
8 )
} {
cout << x << endl;
void main()
x=x+5;
{ 5 }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
x = 4;
}
2 fun(x/2+1);
cout << x << endl;
} 60
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
x = 4;
}
fun(x/2+1);
6 cout << x << endl;
} 61
EXAMPLE OF USING VALUE
PARAMETERS AND GLOBAL
VARIABLES
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
x = 4;
}
fun(x/2+1);
cout << x << endl;
7 } 62
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);
63
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()
{
1 int x = 4; ?
4 x
fun(x);
cout << x << endl;
}
64
EXAMPLE OF REFERENCE
PARAMETERS
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5; void fun( int & y )
{
} 3 cout<<y<<endl;
void main() y=y+5;
{ }
int x = 4; // Local variable
void main()
fun(x); {
cout << x << endl; int x = 4; ?
4 x
} 2 fun(x);
cout << x << endl;
}
65
EXAMPLE OF REFERENCE
PARAMETERS
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5; void fun( int & y )
{
} cout<<y<<endl;
void main() 4 y=y+5;
9
{ }
int x = 4; // Local variable
void main()
fun(x); {
cout << x << endl; int x = 4; ?
4 x
} 2 fun(x);
cout << x << endl;
}
66
EXAMPLE OF REFERENCE
PARAMETERS
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5; void fun( int & y )
{
} cout<<y<<endl;
void main() y=y+5;
{ 5 }
int x = 4; // Local variable
void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
} 2 fun(x);
cout << x << endl;
}
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
void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
} fun(x);
6 cout << x << endl;
}
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
void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
} fun(x);
cout << x << endl;
7 }
69
CONSTANT REFERENCE PARAMETERS