Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

C++ Programming Language

Functions Cont.
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 { }

2
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;
}

3
Example of User-defined
C++ Function
Function
header

double computeTax(double income)


{
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}

4
Example of User-defined
C++ Function
Function Function
header body

double computeTax(double income)


{
if (income < 5000.0) return 0.0;
double taxes = 0.07 * (income-5000.0);
return taxes;
}

5
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;
}

6
Example #1
• Write a function to test if a number is an odd number

inline bool odd (int x)


{
return (x % 2 == 1);
}

7
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));
}

8
Recursion
• Recursive functions
– Functions that call themselves
– Can only solve a base case
• If not base case
– Break problem into smaller problem(s)
– Launch new copy of function to work on the smaller problem (recursive call/recursive
step)
• Slowly converges towards base case
• Function makes call to itself inside the return statement
– Eventually base case gets solved
• Answer works way back up, solves entire problem

9
Recursion

• Example: factorial
n! = n * ( n – 1 ) * ( n – 2 ) * … * 1
– Recursive relationship ( n! = n * ( n – 1 )! )
5! = 5 * 4!
4! = 4 * 3!…
– Base case (1! = 0! = 1)

10
Example #3
• Write a function to compute n!
– For the first time write the code by for loop
int factorial( int n)
{
int product=1;
for (int i=1; i<=n; i++) product *= i;
return product;
}

11
12

- For the second time write the code by recursive function:


// Recursive factorial function.
#include <iostream>
using std::cout;
using std::endl; Data type unsigned long can
hold an integer from 0 to 4
#include <iomanip> billion.
using std::setw;
unsigned long factorial( unsigned long ); // function
prototype

int main()
{
// Loop 10 times. During each iteration, calculate
// factorial( i ) and display result.
for ( int i = 0; i <= 10; i++ )
cout << setw( 2 ) << i << "! = "
<< factorial( i ) << endl;
return 0; // indicates successful termination
} // end main
13

// recursive definition of function factorial


The base case occurs when we
unsigned long factorial( unsigned long number )
have 0! or 1!. All other cases
{ must be split up (recursive step)
// base case .
if ( number <= 1 )
return 1;
// recursive step
else
return number * factorial( number - 1 );
} // end function factorial

The output
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Recursion vs. Iteration

• Repetition
– Iteration: explicit loop
– Recursion: repeated function calls
• Termination
– Iteration: loop condition fails
– Recursion: base case recognized
• Both can have infinite loops
• Balance between performance (iteration) and good software
engineering (recursion)

14
Function Overloading
• Function overloading
– Functions with same name and different parameters
– Should perform similar tasks
• I.e., function to square ints and function to square floats
int square( int x) {return x * x;}
float square(float x) { return x * x; }
• Overloaded functions distinguished by signature
– Based on name and parameter types (order matters)
– Name mangling
• Encodes function identifier with parameters
– Type-safe linkage
• Ensures proper overloaded function called

15
Example #4
Function Overloading
• Write functions to return with the maximum number of two numbers
An overloaded
inline int max( int x, int y) function is a
{ function that is
defined more than
if (x>y) return x; else return y; once with different
} data types or
different number of
parameters
inline double max( double x, double y)
{
if (x>y) return x; else return y;
}

16
17

// Using overloaded functions.


#include <iostream>

using std::cout;
using std::endl;

Overloaded functions have the


// function square for int values same name, but the different
int square( int x ) parameters distinguish them.
{
cout << "Called square with int argument: " << x << endl;
return x * x;

} // end int version of function square

// function square for double values


double square( double y )
{
cout << "Called square with double argument: " << y << endl;
return y * y;
} // end double version of function square
18
int main()
{
int intResult = square( 7 ); // calls int version
double doubleResult = square( 7.5 ); // calls double version

cout << "\nThe square of integer 7 is " << intResult The proper function is called
<< "\nThe square of double 7.5 is " << doubleResult based upon the argument (int
<< endl;
or double).

return 0; // indicates successful termination

} // end main

The output
Called square with int argument: 7
Called square with double argument: 7.5
 
The square of integer 7 is 49
The square of double 7.5 is 56.25
Further Reading

● Stroustrup, Bjarne. Programming Principles and Practice Using C++. Second edition.
Addison-Wesley. ISBN: 978-0321-992789. May 2014.
● Stroustrup, Bjarne. The C++ Programming Language. Fourth edition. Addison-
Wesley. ISBN: 978-0-321-56384-2. May 2013.
● C++ Language Tutorial.
● Wikiversity: Introduction to C++.
● Learn about C++ Programming.
Thank You

Next topic: Functions.

63

You might also like