Ask Question
13 March, 07:05

Write the definition of a function named swapints that is passed two int variables. The function returns nothing but exchanges the values of the two variables. So, if j and k have (respectively) the values 15 and 23, and the invocation swapints (j, k) is made, then upon return, the values of j and k will be 23 and 15 respectively.

+3
Answers (1)
  1. 13 March, 07:06
    0
    Following are the program in the C+ + Programming Language.

    #include

    //header file

    using namespace std;

    //using namespace

    void swapints (int &j, int &k) {

    //definition of the function

    int temp;

    temp = j;

    j = k;

    k = temp;

    }

    / / function declaration

    void swapints (int &j, int &k);

    int main () {

    / / local variable declaration:

    int j = 15;

    int k = 23;

    std::cout << "Before the swapping, value of j : "<
    std::cout << "Before the swapping, value of k : "<
    / * calling a function to swap the values using variable reference.*/

    swapints (j, k);

    std::cout << "After the swapping, value of j : "<
    std::cout << "After the swapping, value of k : "<
    return 0;

    }

    Output:

    Before the swapping, value of j : 15

    Before the swapping, value of k : 23

    After the swapping, value of j : 23

    After the swapping, value of k : 15

    Explanation:

    Here, we define the header fine and namespace and "std".

    Then, we define a function named "swapints () " and pass two reference variables in parameter.

    Then, inside the function, we perform the codes of swapping.

    Then, we define the main () function inside it we define two integer variable "j" and "k" assign value "j" to 15 and "k" to 23 and print both the variables.

    Then, inside main () function we call the function "swapints () " and pass value to its parameters.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write the definition of a function named swapints that is passed two int variables. The function returns nothing but exchanges the values ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers