A Computer Science Tapestry- pages 1-21

  1. Algorithms
    1. What is an algorithm?
    2. Name some algorithms that you use.
    3. Name some algorithms that computers use.
  2. High and Low Level Languages
    1. What is the difference between a high and low level language?
    2. How are high level languages translated to low level languages?
    3. Do you suppose Basic is a higher or lower level language than C++? Why?
  3. Object Oriented Programming
    1. What is your concept of an object?
    2. What is the purpose of object oriented programming?

 

Review from Friday

  1. What does the program listed in Friday’s handout do?
  2. How does the program look after you move the calculation into a separate function? What would the purpose of this be?
  3. How does the program look after you add a function for computing the perimeter? What would main look like after a lot of other functions are added.

Functions With Parameters

In the last exercise we used functions of the form

void functionname(void)

The "voids" are placeholders for variables that could either be returned from the function (void out front) or passed to the function (void inside the parentheses). Passing variables to the function can be very useful. In the last exercise each of the two functions had to request user input for the radius. It would have been useful to get the radius once and then use the same value to compute both the area and the perimeter. This could be done by getting the value of radius in the main function and then passing it to the perimeter and area functions.

An example of passing a parameter in a function is given below for a function which squares a number.

#include <iostream.h>
 
void square(int value)
{
    int square;
    square=value*value;
    cout<<"The square of "<<value<<" is "<<square<<endl;
}
 
void main(void)
{
    int number;
    cout<<"Enter the value to be squared: ";
    cin>>number;
    square(number);
}

This function only uses the number once, so the advantage of using a function that takes a parameter is not particular large. However, this does illustrate this use. Notice that the name of the variable that square is passed in main does not need to match the name in the square function. This is because the number itself is passed, not the variable.

Take this same idea and apply it to the radius and perimeter program so that you only enter the radius once and pass this value to the both functions.

Back to Home Page House3.wmf (25540 bytes)