More functions and call-by-value

These are notes for 6 November.

call-graph.cpp

#include <iostream>
using namespace std;

void print_chars(int k, char c);
int ask_for_height();
void print_row(int i, int height);
void print_first_row(int height);
void print_last_row(int height);

int main()
{
    int height = ask_for_height(); // LOCAL variables
    print_first_row(height);
    for(int row = 1; row < height-1; row++)
    {
        print_row(row, height);
    }
    print_last_row(height);
    return 0;
}

void print_row(int i, int height)
{  
    cout << "o";
    print_chars(i-1, ' ');
    cout << "o";
    print_chars(height*2 - 3 - (i*2), ' ');
    cout << "o\n";
}

void print_first_row(int height)
{
    cout << "o";
    print_chars(height*2 - 3, ' ');
    cout << "o\n";
}

void print_last_row(int height)
{
    cout << "o";
    print_chars(height-2, ' ');
    cout << "o\n";
}

void print_chars(int k, char c)
{
    for(int i = 0; i < k; i++)
    {
        cout << c;
    }
}

int ask_for_height() // no parameters
{
    int height;
    while(1)
    {
        cout << "Enter height (odd > 1): ";
        cin >> height;
        if(height > 1 && height%2 == 1) return height;
        cout << "ERROR: must be odd and > 1.\n";
    }
}

call-by-value.cpp

#include <iostream>
using namespace std;

void frob(int x) // call by VALUE
{
    x *= 2;
    cout << x << "\n";
}

void grob(int& x) // call by REFERENCE
{
    x *= 2;
    cout << x << "\n";
}

int main()
{
    int z = 5;
    frob(z);                    // call by VALUE
    cout << z << "\n";

    int y = 7;
    grob(y);                    // call by REFERENCE
    cout << y << "\n";

    return 0;
}