Write a function called swap() that interchanges two int values passed to it by the calling program. (Note that this function swaps the values of the variables in the calling program, not those in the function.) You’ll need to decide how to pass the arguments. Create a main() program to exercise the function.
#include <iostream>
using namespace std;
void swap(int &a, int &b){
int c = a;
a = b;
b = c;
}
int main(){
int a = 5, b = 8;
printf("Before swapping\t a = %d; b = %d", a, b);
swap(a, b);
printf("\nAfter swapping\t a = %d; b = %d", a, b);
return 0;
}
Comments
Leave a comment