CSC211

Handout #3 Functions

 

 

  1. What is printed by the following code?

 

#include <iostream>

#include <cmath>

using namespace std;

 

void display_mix(int x,  float  & z);            

int main( )

{

      float w = 3.0;

      int y=5 ;

     

      display_mix(2, w);                  

      cout<<"First time function is called, w="<<w<<endl;

      display_mix(y, w);            

      cout<<"Second time function is called, w="<<w<<endl;

      cout<<"Done !"<<endl;

      return 0;                     

 

}

 

void display_mix(int x,  float & z)              

{

      float result;                 

      result= pow(x, 2.0) + z;      

      cout<<"Result is:\t"<<result<<endl;              

}

 

 

  1. Write the definition of a value-returning function that accepts two integer parameters and returns the larger one divided by the smaller one.

 

 

 

 

 

 

  1. Write the definition of a void function that has one double parameter and changes the sign of the parameter (if it is negative it becomes positive, and if it is positive it becomes negative)

 

 

 

 

 

  1. Write the definition of a void function that has two parameters: an array, and an integer parameter that specifies the number of elements in the array.  The functions swaps the first and last elements of the array.