63.8k views
2 votes
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.

1 Answer

3 votes

Answer:

Following are the program in the C++ Programming Language.

#include <iostream>

//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 : "<<j<<endl;

std::cout << "Before the swapping, value of k : "<<k<<endl;

/* calling a function to swap the values using variable reference.*/

swapints(j, k);

std::cout << "After the swapping, value of j : "<<j<< endl;

std::cout << "After the swapping, value of k : "<<k<< endl;

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 <iostream> 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.

User Seaky Lone
by
5.1k points