Parameters

If the toaster is a method: then the bread slice is a parameter to the toaster. The toaster converts a bread slice into a toasted bread slice. Different inputs (e.g., wheat bread, oat bread, etc.) will provide different outputs. Some inputs are illegal, e.g., cannot give a spoon as an input to a toaster.

Similarly, can provide parameters to methods. The methods use the values of the parameters as inputs to their computation. Example:

void printGreeting(string myname)
{
  cout << "Hello " << myname << endl;
  cout << "I hope you had a great day." << endl;
}

int main()
{
  string name = getLine("Next name? ");
  while (name != "")
  {
    printGreeting(name);
    name = getLine("Next name? ");
  }
  return 0;
}

What will happen if you execute the following?

void printGreeting(string myname)
{
  cout << "Hello " << name << endl;
  cout << "I hope you had a great day." << endl;
}

int main()
{
  string name = getLine("Next name? ");
  while (name != "")
  {
    printGreeting(name);
    name = getLine("Next name? ");
  }
  return 0;
}
This will result in a compile error! Note that variable name is local to the scope of the main function. Passing it as a parameter creates a copy of the variable and stores it in myname inside the scope of the printGreetings function.

General syntax

type name(type name, type name, ...)
{
  statement1;
  statement2;
  ...
}

Another example:

printGreeting(5);
Prints a greeting a certain number of times. What would the method declaration and definition look like.

Multiple Arguments

In general, a function can have multiple arguments.

ret_type function(type_1 arg_1, type_2 arg_2, ... , type_n arg_n){
  ..
  ..
}

Write a function

printGreeting("abc", 5);
Prints greeting to abc 5 times.

Parameter values can be derived from constant values, variables, or arbitrary expressions. Show previous examples where variables are used for parameter values.

Show a run involving main function and printGreeting function for the following code:

void printGreeting(int times)
{
  for (int i = 0; i < times; i++) {
    cout << "Hello, world!" << endl;
  }
}
int main()
{
  int repeats = 5;
  printGreeting(repeats);
}
There will be one variable (box) for repeats in main function. Similarly, there will be one variable (box) for times in printGreeting function. The printGreeting's scope will get created only when control transfers to it, and will get destroyed when control returns from it back to main.

Parameters are copies!

void addFive(int x) {
     x = x + 5;
     cout<< "(Inside addFive): New x is "<< x;
}

int main() {
  int x=3;
  addFive(x);
  cout<< "(Inside main): New x = "<< x;
}
What will the output of the above program? How would you change it to have the desired output? We will come to back to this soon when we talk about return values.

Revisiting function declaration and function call syntax: A method can accept multiple parameters separated by commas; when calling it, you must pass values for each parameter.

Declaration:

type name(type name, ..., type name)
{
  statements;
}

Call:
name(value, value, ..., value);
Exercise: Write a function drawBox(height,width) which takes two arguments 'height' and 'width' and draws of a box of the corresponding size. For example, drawBox(3,5) should draw a box as follows:
*****
*   *
*****
And drawBox(4,4) should draw a box as follows:
****
*  *
*  *
****

Potential solution: define one top-level function called drawBox(height, width) and two functions drawLine(width) (for top and bottom lines) and drawSide(width) for middle lines. Show implementation of all three functions. We will do this as one of our lab assignment questions!

Return Values

Return values let you give back some information when a method is finished, e.g., toasted bread in our toaster method.

Some examples we have already seen: simpio functions getInteger, getLine, etc.

Return Types

The function declarations specify the type of the return value (just like they specify the type of the parameter values). Types say what is legal and what is illegal.

Example:

double metersToCm(double meters)
{
  return 100*meters;
}

int main()
{
  double meters = getDouble("meters?");
  double cm = metersToCm(meters);
  cout << cm << " centimeters." << endl;
  return 0;
}
Show execution: how variables are created, how parameter values are copied, how the metersToCm function uses values in the parameter variables, how return value is copied back.

If a method returns something, you can use it directly in an expression!

int main()
{
  double meters = getDouble("meters?");
  cout << metersToCm(meters) << " centimeters." << endl;
  return 0;
}

Parameters vs. Return

Method operates on its parameters and computes a value to be returned (show using a diagram).

metersToCm Example Above: Another Exmaple:
#include <iostream>
#include <cmath>
int main() {
 int x=-42;
 int x_abs=abs(x);
 cout<<"Absolute value of " << x << " = " << x_abs << endl;
 double y=2.03;
 double y_rnd=round(y);
 cout<<"The rounded value of " << y << " = " << y_rnd << endl;
}
Explain parameter/return with respect to abs and round functions.

Another example: Returning Boolean values.

bool checkEven(int number) {
  if(number % 2 == 0) 
    return true;
  else
    return false;
}

int main() {
    n = getInteger("n?");
    bool is_even=checkEven(n);
    cout << "The number is even is " << is_even << endl;
}
Note: Function ends as soon as it encounters a reutrn statement.
bool checkEven(int number) {
  if(number % 2 == 0) 
    return true;
  else
    return false;
  cout << "This line will never be executed!" << endl;
}

What happens to the last cout statement? It will neven be excuted: why? Talk about the flow of the program and the return statements.

Fixing the addFive program: use a return value.

int addFive(int x) {
     x = x + 5;
     cout<< "(Inside addFive): New x is "<< x;
     return x;
}

int main() {
  int x=3;
  x=addFive(x);
  cout<< "(Inside main): New x is "<< x;
}

What will be the output of the program above?

Question: Why use a return statement? Why not simply print in the original function itself?
Answer: We may need the value for future computations in the caller. See below.

int main() {
  int x=3;
  x=addFive(x);
  x=2*x;
  cout<< "(Inside main): New x is "<< x;
}