# Dive Into Systems: Exercises

## Section1.4Functions

### Checkpoint1.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?

### Checkpoint1.4.2.Function Arguments.

What are the arguments for f2 in the following snippet of code (separated by commas)?
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, f1 is the name of the function.
Hint.
arguments refer to when the function is called

Given the partial listing of C code shown below annotated with line numbers:
. . .
4:     int main(void){
11:         . . .
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 .

### Checkpoint1.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", 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.

### Checkpoint1.4.5.

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

### Checkpoint1.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?

### Checkpoint1.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);
return 0;
}

What is the value of x after the function call?
Draw that stack at the point in dec right before the return statement.
Below is an example drawing of the stack diagram: ### Checkpoint1.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);
}
}


### Checkpoint1.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.
int fibo(n){

return 0;
}

int main(void){
//test cases
printf(“Fibo of 3: %d (should be 2)\n”, fibo(3));
printf(“Fibo of 4: %d (should be 3)\n”, fibo(4));
printf(“Fibo of 5: %d (should be 5)\n”, fibo(5));
printf(“Fibo of 6: %d (should be 8)\n”, fibo(6));
printf(“Fibo of 7: %d (should be 13)\n”, fibo(7));
printf(“Fibo of 10 is: %d (should be 55)\n”, fibo(10));

return 0;
}


### Checkpoint1.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{.}$$
int power(base, exp){

return 0;
}

int main(void){
//test cases
printf(“3**2: %d (should be 9)\n”, power(3,2));
printf(“4**4: %d (should be 256)\n”, power(4,4));
printf(“2**6: %d (should be 64)\n”, power(2,6));
printf(“10**3: %d (should be 1000)\n”, power(10,3));
printf(“5**0: %d (should be 1)\n”, fibo(5,0));

return 0;
}