1. #include <iostream>                      //Line 1

using namespace std;                       //Line 2

 

int func1(int& a, int b);                  //Line 3

void func2(int& r, int& s);                     //Line 4

 

void main()                                //Line 5

{                                          //Line 6

   int num1 = 5;                                //Line 7

   int num2 = 10;                          //Line 8

  

   cout << "func1 returns "               

<< func1(num1, num2) << endl;              //Line 9

   cout << "num1 = " << num1              

      << " num2 = " << num2 << endl;       //Line 10

 

   func2(num2, num1);                      //Line 11

   cout << "num1 = " << num1              

<< " num2 = " << num2 << endl;       //Line 12

}                                         

 

int func1(int& a, int b)                   //Line 13

{                                          //Line 14

    int f;                               //Line 15

     f = a + b;                           //Line 16

     a++;                                 //Line 17

     b++;                                 //Line 18

return f;                            //Line 20

}                                          //Line 21

 

void func2(int& r, int& s)                 //Line 22

{                                         

     int k;                               //Line 23

    

     k = r;                               //Line 24

     r = s;                               //Line 25

     s = r + k;                           //Line 26

      }

 


a)                  Which lines contain function headers?

 

 

b)                  Which line contains the function prototype for function func2?

 

 

c)                  List variable(s) that is/are local to function func1.

 

 

d)                 List all reference parameters .

 

 

e)                  List all value parameter(s).

 

 

f)                   What is printed when the program is executed?

 

 

 

2. Write a prototype for a fuction called swap  that has two parameters.  The function switches the values of the two parameters.

 

3. Write a prototype for a function that has two parameters.  The first is of type ifstream, and the second is an integer.  The function counts the number of lines that are in the file associated with the stream variable.

 

 

4.  SCOPE:

#include<iostream>

using namespace std;

int g1 = 25;

const int T = 100;

void fun1(int&,int,int,int,int);

void main()

{

      int x,y,z;

      x = -8;

      y = x;

      ++x;

      z = abs(x + y);

      fun1(x,y,z,g1,T);

{

   int g1 = 15;

         fun1(x,y,z,g1,T);

      }

      fun1(x,y,z,g1,T);

      {

        int fun1 = 9;

        cout<<fun1;

      }

}

void fun1(int& a, int b,int c,int d,int e)

{    

    int x = 16;

      a = x;

      x++;

      cout<<"x is: "<<x<<endl;

      cout<<"g1 is: "<<g1<<endl;

      cout<<"a: "<<a<<endl;

      cout<<"b:"<<b<<endl;

      cout<<"c: "<<c<<endl;

      cout<<"d: "<<d<<endl;

      cout<<"e: "<<e<<endl;

 

}

 

 

 

 

5.  Default parameters:

 

#include<iostream>

#include<cmath>

#include<cassert>

using namespace std;

double defaultfun(int x,char z = 'n',double y = 2.5);

 

void main()

//showing some function calls

{

      int num1=5,num2=10;

      char letter;

letter = 'Y';

cout<<defaultfun(num1 + num2)<<endl;

      num2 = pow(num1,2);

      cout<<defaultfun(num1,'y')<<endl;

      num2 = defaultfun(num1,'y',double(num2));

      cout<<num2;

      num2 = 0;

      cout<< defaultfun(num1,'y',double(num2));

}

 

double defaultfun(int x, char z, double y )

//function to deal with two variables

{

      if (static_cast<char>(toupper(z)) == 'N')

                return x * y;

      else

            {    

                assert(y);

                return x / y;

            }

}

 

 

 

37.5

2

0Assertion failed: y, file C:\Program Files\Microsoft Visual Studio\MyProjects\filetry\filetry.cpp, line 31

Press any key to continue