Functions


  1. Explain the difference between a function declaration and a functions definition? Where is the function declarataion placed?

  2. Given the following function declarations, explain the emaning of each.
    1. int function1(int a, float b);
    2. void function2(float a, floatb, float c);
    3. int function3(void);
    4. float function4(float x);
    5. void function5(void);

  3. Write an appropriate function call for each of the function declarations given above.

  4. What does the following function do?
    float function(float a, float b, float c)
    {
    	float tmp;
    
    	tmp = (a + b + c )/ 3.0 ;
    
    	return tmp;
    }
    

  5. Give an example function call for the above function.

  6. Give the function declaration for the function given in question 4.


Answers









































  1. The declaration is contained within the program before the function is called outside any function (including the main program. It contains details of the function name, the return type and the types of the function arguments.

    the definition conatins the actual code of what is to be implemented when the function is called.

    1. The function is sent an integer argument which it will refer to as a within the function and a floating point varaible, which it will refer to as b within the function. The function will return an integer quantity.
    2. The function is sent 3 floating point arguments and returns no value to the calling program.
    3. The function is sent no arguments but returns an integer.
    4. The function is sent one floating point argument and returns a floating point argument.
    5. The function is sent no varaibale and returns nothing.

  2. Example calls are given below,
    1. int x, z;
      float y;
      
      /* x and y are assigned some values in the program */
      
      z = function1(x, y);
      
    2. 
      float	x,y,z;
      
      /* x, y and z assigned values in program */
      
      function2(x, y, z);
      
    3. int a;
      
      a = function3();
      
    4. float a, b;
      
      /* b assigned a value in program */
      a = function4(b);
      
    5. function5();
      

  3. It returns the average value of the three values sent to it.

  4. x = function(a,b,c);

  5. float function(float, float, float); or float function1(float a, float b, float c);


Back to Questions Back to Main Page for Lecture 6