Ask Question
18 May, 11:59

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.

+2
Answers (1)
  1. 18 May, 12:22
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers