User Defined Function

[Set – 1]

1. Write a program using function which accept two integers as an argument and return its sum. Call this function from main( ) and print the results in main( ). solution

2. Write a function to calculate the factorial value of any integer as an argument. Call this function from main( ) and print the results in main( ). solution

3. Write a function that receives two numbers as an argument and display all prime numbers between these two numbers. Call this function from main( ). solution

4. Raising a number to a power p is the same as multiplying n by itself p times. Write a function called power that takes two arguments, a double value for n and an int value for p, and return the result as double value. Use default argument of 2 for p, so that if this argument is omitted the number will be squared. Write the main function that gets value from the user to test power function. solution

5. Write a function called zero_small() that has two integer arguments being passed by reference and sets the smaller of the two numbers to 0. Write the main program to access the function. solution

6. Write the output of the following program :

#include <iostream>
using namespace std;

void X(int &A, int &B)
{
    A = A + B;
    B = A - B;
    A = A - B;
}

int main()
{
    int a = 4, b = 18;
    X(a,b);
    cout << a << ", " << b;
    
    return 0;
}
       

7. Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)
{
    A = A + B;
    B = A - B;
    A = A - B;
}

int main()
{
    int a = 4, b = 18;
    X(a,b);
    cout << a << ", " << b;
    
    return 0;
}
        

8. Write the output of the following program:

#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)
{
    int temp = B + C;
    B += temp;
    if (C == 100)
        cout << temp << " " << B << " " << C << endl;
}

int main()
{ 
    int M = 90, N = 10;
    Execute(M);
    cout << M << " " << N << endl;
    Execute(M, N);
    cout << M << " " << N << endl;
    
    return 0;
}

9. Give the output of the following program

#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)
{
    x = x - y;
    y = x * 10;
    cout << x << ", " << y << '\n';
}

int main()
{
    int global = 7;
    func (::global, global);
    cout << global << ", " << ::global << '\n';
    
    func(global, ::global);
    cout << global << ", " << ::global << '\n';
    
    return 0;
} 
      

10. Write the output of the following program :

#include <iostream>
using namespace std;

static int i = 100;
void abc()
{
    static int i = 8;
    cout << "first = " << i++ << endl;
}

int main()
{
    static int i = 2;
    abc();
    cout << "second = " << i << endl;
    abc();
    
    return 0;
}

11. Write the output of the following program:

#include <iostream>
using namespace std;

int func(int &x, int y = 10)
{
    if (x % y == 0)
        return ++x;
    else 
        return y--;
}

int main()
{
    int p = 20, q = 23;

    q = func(p, q);
    cout << p << " " << " " << q << endl;
    
    p = func (q);
    cout << p << " " << " " << q << endl;

    q = func (p);
    cout << p << " " << " " << q << endl;
    
    return 0;
}

«