1
h06
CS16 F18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h06: Chapter 5: Call by value and call by reference

ready? assigned due points
true Thu 11/08 12:30PM Wed 11/14 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Read Chapter 5.1 thru 5.4. Read the lecture slides for topics on binary arithmetic. You don’t need to turn this homework in. Please turn in the physical copy of your homework in person. Write in PEN or DARK PENCIL.

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
FOR BEST RESULTS, PRINT THIS PAGE AS A PDF, THEN PRINT THE PDF

1.(2 pts) What happens if you forget the return statement in a void-function?

2.(4 pts) Can you define a function in the body of another function? And can you call a function in the body of another function?

3.(8 pts) Write a void-function definition for a function called “zero_both” with 2 parameters, both which are variables of type int, and sets the value of both variables to 0. Describe if you picked this function to be a call-by-reference or a call-by-value AND WHY?

4.(10 pts) What is the output of the program below (write it in the space to the right)?

#include <iostream>
using namespace std;

void phooey(int &z) {
  z = z / 2;
  cout << "z=" << z << endl;
  }

int main() {
  int b = 3;
  phooey(b);
  cout << "b=" << b << endl;
  return 0;
  }

5.(2 pts) What is the meaning of the ampersand character (&) in the code above?

6.(8 pts) What is the output of the following code? If there’s an error that will not allow an output, point it out.


void foo(int* a, int* b, int& c, int d) {
  *a = *a + 2;
  b = b + 2;
  c = c + 2;
  d = d + 2;
}

int main() {
  int x = 10, y = 15, z = 20, s = 30;
  foo(&x, &y, z, s);
  cout << x << ", " << y << ", " << z << ", " << s <<endl;
  return 0;
}

7.(16 pts) Write the output for each segment of code below. Assume p is always created at the location 0x8000 and x is always created at the location 0x9000. If the code prints a junk value, show that value using the symbol ?. Identify undefined behaviour or compile-time/run time errors.

int main(){
      int x; 
      int* p = &x;
      cout << p << *p << &x<< endl; 
      return 0;
}
int main(){
      int* p = 0;
      cout << p << *p <<endl; 
      return 0;
}     
int main(){
      int* p;
      cout << p << &p <<endl; 
       return 0;
}
int main(){
      int* p = new int; //Assume the int is created at location 0x9000
      cout << p << *p << &p << endl; 
             return 0;
}