Chapter 6 Function in C++ Progrogramming
Chapter 6 Function in C++ Progrogramming
Function in C++
3
(true or false) Compiled By Dagne Walle
Why Functions( methods/behaviors)
◼ Huge codes are difficult to understand and debug.
◼ Makes the code look simpler, neater and makes
your task easier.
◼ Re-usability of code.
◼ Boss to worker analogy: A boss (the calling
function or caller) asks a worker (the called
function) to perform a task and return (i.e., report
back) the results when the task is done
◼ Divide and conquer: Construct a program from
smaller pieces or components. Each piece more
manageable than the original program
Compiled By Dagne Walle 4
Terminologies
◼ Function Definition:
int func ( int a, int b){
return (a + b);
}
Function
header
Function
Function body/definition
header
double computeTaxes(double) ;
#include <iostream>
void DisplayMenu(void);
int main( ) {
function prototype consisting of
<type> function name (types of parameters);
if(x>=y)
maximum = x;
else
maximum = y;
return maximum;
}
void DisplayMenu(void) {
cout << “*********** MENU **************\n”;
cout <<endl;
cout << “ 1. Man United” << endl;
cout << “ 2. Chelsea” << endl;
cout << “ 3. Arsenal” << endl;
cout << “ 4. Quit” << endl;
cout << endl;
cout << “Please choose 1, 2, 3 or 4 : “;
}
Compiled By Dagne Walle 20
Calling a function
void DisplayMenu(void);
Function call
int main() {
int response; from within
main()
DisplayMenu();
return 0;
}
void DisplayMenu(void) {
cout << “*********** MENU **************\n”;
cout <<endl;
cout << “ 1. Man United” << endl;
cout << “ 2. Chelsea” << endl;
cout << “ 3. Arsenal” << endl;
cout << “ 4. Quit” << endl;
cout << endl;
cout << “Please choose 1, 2, 3 or 4 : “;
Compiled By Dagne Walle 23
}
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.
◼ make the customers focus on the purpose of the function, not its
24 implementation Compiled By Dagne Walle
Example
#include <iostream>
#include <string> double computeTaxes(double income)
using namespace std; {
if (income<5000) return 0.0;
// Function Signature return 0.07*(income-5000.0);
double getIncome(string); }
double computeTaxes(double);
void printTaxes(double); double getIncome(string prompt)
int main() {
{ cout << prompt;
// Get the income; double income;
double income = getIncome("Please cin >> income;
enter the employee income: ");
return income;
// Compute Taxes }
double taxes = computeTaxes(income); void printTaxes(double taxes)
{
// Print employee taxes cout << "The taxes is $" << taxes <<
printTaxes(taxes); endl;
} }
❑ A scope (the part of the program code that can use it)
❑ A scope (the part of the program code that can use it)
1. Function overloading
2. Operator overloading
3. Dynamic binding
◼ Example: factorial
n! = n * ( n – 1 ) * ( n – 2 ) * … * 1
❑ Recursive relationship ( n! = n * ( n – 1 )! )
5! = 5 * 4!
4! = 4 * 3!…
4! = 4 × 3 × 2 × 1
❑ Base case (1! = 0! = 1)
template<class someType>
void Print (SomeType val)
{
cout<<“***Debug***” << endl;
cout<<“Value is ” << val << endl;
}
◼ Syntax
template<templateparamList>
FunctionDefinition
85
Question
86
To calculate the area of circle, rectangle and
triangle using function overloading.
◼ S1: Start the program.
◼ S2: Declare the class name as fn with data members and member
functions.
◼ S3: Read the choice from the user.
◼ S4: Choice=1 then go to the S5.
◼ S5: The function area() to find area of circle with one integer
argument.
◼ S6: Choice=2 then go to the S7.
◼ S7: The function area() to find area of rectangle with two integer
argument.
◼ S8: Choice=3 then go to the S9.
◼ S9: The function area() to find area of triangle with three
arguments, two as Integer and one as float.
◼ S10: Choice=4 then stop the program.
Compiled By Dagne Walle 87
End Of Chapter
? QUESTIONS ?