Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.4 Functions

Checkpoint 1.4.1.

    True or False: The following is a definition of the function f1:
    int f1(int x, float y);
    
  • True.

  • False. It is the declaration/prototype of f1, not the definition. The definition of the function would include the code that executes when f1 is called.
  • False.

  • False. It is the declaration/prototype of f1, not the definition. The definition of the function would include the code that executes when f1 is called.
Hint.
What is the difference between a declaration and a definition?

Checkpoint 1.4.2. Function Arguments.

    Which of the following are arguments to function f2 in the snippet of code below?
    int f2(char c1, char c2) {
      ...
    }
    
    int main(void){
        ...
        f2('a', '9');
        ...
    }
    
  • ’a’
  • Correct!
  • ’9’
  • Correct!
  • c1
  • No, c1 is a parameter in the function declaration.
  • c2
  • No, c2 is a parameter in the function declaration.
  • f2
  • No, f2 is the name of the function.
Hint.
Arguments refer to when the function is called

Checkpoint 1.4.3. Code reading.

Given the partial listing of C code shown below annotated with line numbers:
. . . 
 4:     int main(void){
11:         . . .
12:         add(a, b);
13:         . . .
16:     } 
. . . 
40:     int add(int x, int y) {
41:         return x + y; 
42:     }
The function add is defined on line , and called on line .

Checkpoint 1.4.4.

    Given the following functions, which ones return a value?
    void f4() {
      printf("hello\n");
      return;
    }
    
    void f5(int a, int b) {
      printf("The sum is %d\n", a + b);
      return;
    }
    
  • f4
  • Incorrect! f4 only prints to the console.
  • f5
  • Incorrect! f5 only prints to the console.
  • Both f4 and f5
  • Incorrect!
  • Neither f4 or f5
  • Correct! Both f4 and f5 only prints to the console.

Checkpoint 1.4.5.

    For which of the following is space allocated on the stack?
  • Local variables
  • Global variables
  • Parameters
  • Assembly code for the function
  • None

Checkpoint 1.4.6. Function Returns.

int do_math(int x, int y) {
    if (x > y) {
        return x;
    }
    y = x + 6;
    return y;
}
When function do_math is called, how many times does it return?

Checkpoint 1.4.7. Parameter Passing Methods.

int dec(int x, int y) {
    if (x >= y) {
        x = x -1;
    } else {
        x = y - x;
    }
    // draw the stack here
    return x;
}

int main(void) {
    int x, y;

    x = 12;
    y = x;
    y = dec(x, y);

    printf("x is: %d\n", x);

   return 0;
}
What value will main print for x after the function call to dec?
Draw that stack at the point in dec right before the return statement. You can check your work with the ’Answer’ link below this question.
Answer.
Below is an example drawing of the stack diagram:
Stack diagram for problem 1.4.7 part b.

Checkpoint 1.4.8. Code Tracing.

Suppose the the function mystery (defined below) is called with the argument 123.
What value does it return?
int mystery(int val) {
    if (val < 10) {
         return val;
    } else {
      return (val % 10) + mystery(val / 10); 
    }
}

Checkpoint 1.4.9. C Programming Practice.

Write a recursive function that generates the nth fibonacci number, for some number n. The nth fibonacci number is defined as:
\begin{equation*} f(n) = f(n-1) + f(n-2) \end{equation*}
where \(f(0) = 0\) and \(f(1)\) = 1.

Checkpoint 1.4.10. C programming practice.

Implement and test a power function (for positive integer exponents only). A call to your function should compute base\(^{exp}\text{.}\)