What is swapping?
Calling Methods:
C++ code which take two numbers from user and swap using built in swap function. Program takes two input from user and displays numbers before and after swapping
It means exchange things with each otherFor more information have a look at swapping concept by Wikipedia
Calling Methods:
- swap();
- std:swap();
C++ code which take two numbers from user and swap using built in swap function. Program takes two input from user and displays numbers before and after swapping
- #include <iostream>
- using namespace std;
- void swap()
- {
- cout<<"this is my swap";
- }
- int main()
- {
- int firstNum , secondNum;
- cout<<"Enter value for First Number: ";
- cin>>firstNum;
- cout<<"Enter value for Second Number: ";
- cin>>secondNum;
- cout<<"\n\n";
- cout<<"Values BEFORE Calling Built in Swap Function"<<endl<<endl;
- cout<<"\tFirst Number = "<<firstNum<<endl;
- cout<<"\tSecond Number = "<<secondNum<<endl;
- cout<<"\n\n";
- std::swap( firstNum , secondNum );
- cout<<"Values AFTER Calling Bulit in Swap Function"<<endl<<endl;
- cout<<"\tFirst Number = "<<firstNum<<endl;
- cout<<"\tSecond Number = "<<secondNum<<endl;
- return 0;
- }
Note: If we remove std:: from swap at line number 20 than built in function will not run but The Function which has written above main will run
If the function above the main is not written and we remove std:: from built in function than built in function will run
When we call a function in a program compiler look into our code if compiler not found the function body which has called it look into standard library where he found the body it runs it.
Image View OF Code: (click on image for large view)
If the function above the main is not written and we remove std:: from built in function than built in function will run
When we call a function in a program compiler look into our code if compiler not found the function body which has called it look into standard library where he found the body it runs it.
Image View OF Code: (click on image for large view)
swap function example in c++ programming
sample output |
Nice for understanding .
ReplyDelete