PIC10A lecture 8

Reading

p103-116

Functions

How it works?

When the program reaches a function call:

void

Example

#include<iostream>
using namespace std;

void showDate(int month, int day);

int main() {
    cout << "Today is ";
    showDate(11,23);
    cout << ".\n";
    return 0;
}

void showDate(int month, int day) {
    switch(month) {
    case 1:
        cout << "Jan";
        break;
    case 2:
        cout << "Feb";
        break;
    case 3:
        cout << "Mar";
        break;
    case 4:
        cout << "Apr";
        break;
    case 5:
        cout << "May";
        break;
    case 6:
        cout << "Jun";
        break;
    case 7:
        cout << "Jul";
        break;
    case 8:
        cout << "Aug";
        break;
    case 9:
        cout << "Sep";
        break;
    case 10:
        cout << "Oct";
        break;
    case 11:
        cout << "Nov";
        break;
    case 12:
        cout << "Dec";
        break;
    }

    cout << " " << day;
}

Output

Today is Nov 23.

Writing your own functions

Example

#include<iostream>
using namespace std;

double finalScore(double midterm, double exam);
char grade(double score);

int main() {
double midterm, exam; // score for midterm and final exam
double final; // final result
cout << "Please enter your midterm and final score: ";
cin >> midterm >> exam;

final = finalScore(midterm, exam);

cout << "Your grade is " << grade(final) << ".\n";

return 0;
}


// input: scores for midterm, exam
// return: final score
double finalScore(double midterm, double exam) {
double result1 = 0.5*midterm + 0.5*exam;
double result2 = 0.4*midterm + 0.6*exam;
// the final score is the max of result1 and result2
if(result1 >= result2)
return result1;
else
return result2;
}

// input: score
// output: grade
char grade(double score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 65) {
return 'C';
} else if(score >= 50) {
return 'D';
} else {
return 'F';
}
}

Before writing your functions:

You should determine
Example: write a function to calculate tax.     

Syntax of functions

#include<iostream>
using namespace  std;

double myFunction(int parameter1, char parameter2); // function prototype

int main() {                                        // program body
    .........
    return 0;
}

double myFunction(int parameter1, char parameter2) { // function header
     // function body
}


Function Prototypes

Function prototypes consist of
Alternate form for function declarations

Function Definition

it includes

Function header

Function Body

return

return in void function

Example:
#include<iostream>
#include<cmath>
using namespace std;

void squareRoot(double num);

int main() {
    squareRoot(3);
    squareRoot(-3.5);
    return 0;
}

void squareRoot(double x) {
    if( x < 0) {
        cout << "The square root of " << x << " is meaningless.\n";
        return;
    }

    cout << "The square root of " << x << " is " << sqrt(x) <<".\n";
}



Output

The square root of 3 is 1.73205.
The square root of -3.5 is meaningless.


Function calling functions

Main as a function

Recursive function

Question: write a function to calculate
square of 1 + square of 2 + square of 3 + .... + square of n

#include<iostream>
using namespace std;

int squareSum(int n);

int main() {
    cout << squareSum(3) << endl;

    return 0;
}

int squareSum(int n) {
    if(n <= 0) {
        return 0;
    }

    return n*n + squareSum(n-1);
}

The output is 14.