C/C++ Programming
Tutorial 5. Functions

1. Discuss an important problem with the following code. Run the program and check the output of the program

#include<stdio.h>

int *zoo() {
        int x;
        x = 2;
        return &x;
     }

int zoo1(){
	int y=675;
	int z=90;
	return y;
}
   
void main() {
       int *x, *zoo();

       x = zoo();
	   zoo1();
       printf("%d", *x);
     }

2. Consider the following C function:

  void multiply (int m, int n) {
    m = m*n;
    cout <<m<< "," <<n<< endl;
  }

Suppose the function is called with actual parameters i,j where i=2,j=3. If we are using call-by-value show what is printed when called with:

  (a) multiply (i,j) and (b) multiply (i,i).

Now suppose that the parameters could be replaced by call-by-value-result parameters. Repeat parts (a) and (b) and explain the different effect (if any). Assume that the instructions are not executed sequentially.

3. In what unusual place can you use a function call when a function returns a values by reference? (hint: function call is usually used at the right hand side of equal sign =). Give a simple example.

4. (a) What is the principle reason for passing arguments by reference?

(b) Suppose a function has the following declaration: void person(char* name, int age); Modify the declaration so that default value for name is unknown and age 20.

5. Write C program generated a weight conversion table using function. What is the difference between program with function and without function? Hint: the ratio of kilo/pound = 0.45359.

6. Write a recursive function to calculate a power of a number.

7. Write a recursive function that prints out a table of Fibonacci numbers. Fibonacci numbers are defined by a series in which any element is the sum of the previous two elements.