Previous Lecture lect09 Next Lecture

lect09, Thu 05/02

Passing Parameters to functions; Numerical base conversions

From now on, include pre- and post- conditions for the functions that you write: * Precondition: What you expect the function to receive * Poscondition: What you expect will be true once the function has completed * These should be added as a pair of comments right before each function * If you update your function, make sure to read over your pre- and post- conditions to make sure they still make sense and match the new code. Update them if necessary as you edit your code.

Code from Lecture

First, we wrote a simple function to swap the values of two variables. This example motivates the need for the pass-by-reference mechanism, since we cannot return more than one value from a function.

// Lecture 9 functions
// Pass-by-value vs. pass-by-reference

#include <iostream>
using namespace std;

void swap(int& val1, int& val2);
// A function to swap two integers
// Precondition: val1 and val2 are integers
// Postcondition: val1 has the value of val2
// and vice versa

int main()
{
    //int size = 3;
    //int arr[size];
    /* Declaring a function inside of another function leads to an error
void swap1(int& val1, int& val2)
{
    cout << "Hi from swap1";
}
*/

    int num1, num2;
    cout << "Enter 2 numbers: ";
    cin >> num1 >> num2;
    cout << "Num1 = " << num1 << endl;
    cout << "Num2 = " << num2 << endl;

    swap(num1, num2);
    //swap1(num1, num2);

    cout << "Num1 = " << num1 << endl;
    cout << "Num2 = " << num2 << endl;

    return 0;
}
void swap(int& val1, int& val2)
// A function to swap two integers
// Precondition: val1 and val2 are integers
// Postcondition: val1 has the value of val2
// and vice versa
{
    int temp = val1;
    cout << "---" << endl;
    cout << "Val1 = " << val1 << endl;
    cout << "Val2 = " << val2 << endl;
    val1 = val2;
    val2 = temp;
    cout << "Val1 = " << val1 << endl;
    cout << "Val2 = " << val2 << endl;
    cout << "---" << endl;
    //main();
}

We then use the & symbol that allows us to print the addresses of the variables to see the effect of pass-by-value (the values are copied into a different address) vs. pass-by-reference (the function uses the values stored at the same address that they were in the main function).

// Lecture 9 functions

#include <iostream>
using namespace std;

//void swap(int &val1, int &val2);
void swap(int val1, int val2);
// A function to swap two integers
// Precondition: val1 and val2 are integers
// Postcondition: val1 has the value of val2
// and vice versa

int main()
{
    int num1, num2;
    cout << "Enter 2 numbers: ";
    cin >> num1 >> num2;
    cout << "Num1 = " << num1 << endl;
    cout << "Num2 = " << num2 << endl;
    cout << "num2& = " << &num1 << endl;
    cout << "Num2& = " << &num2 << endl;

    swap(num1, num2);  // The address locations change, depending on the formal parameters

    cout << "Num1 = " << num1 << endl;
    cout << "Num2 = " << num2 << endl;
    cout << "num2& = " << &num1 << endl;
    cout << "Num2& = " << &num2 << endl;

    return 0;
}
//void swap(int &val1, int &val2)
void swap(int val1, int val2)
// A function to swap two integers
// Precondition: val1 and val2 are integers
// Postcondition: val1 has the value of val2
// and vice versa
{
    int temp = val1;
    cout << "---" << endl;
    cout << "Val1 = " << val1 << endl;
    cout << "Val2 = " << val2 << endl;
    val1 = val2;
    val2 = temp;
    cout << "Val1 = " << val1 << endl;
    cout << "Val2 = " << val2 << endl;
    cout << "Val1& = " << &val1 << endl;
    cout << "Val2& = " << &val2 << endl;
    cout << "---" << endl;
    //main(); // fun test to see the infinite recursion
}

## Counting Numbers in Different Numerical Bases:

Positional Notation

Converting Binary to Decimal

Converting Binary to Octal or Hexadecimal

Algorithm for converting Base-10 to other bases

Why is this useful?

Practice Questions

  1. What does the following code print out?
    void foo(int& x){
    x++;
    }
    int main(){
    int a = 5;
    foo(a);
    cout << a << endl;
    return 0;
    }
    
  2. What does the following code print out? Assume it’s part of a working C++ program
    int x = 0;
    int& y = x;
    y = y + 20;
    x = x - 15;
    cout << x << endl;
    cout << y << endl;
    
  3. Convert the decimal number 166 into hexadecimal and binary