Experience function parameter transfer (Superior task in week 17th)

Source: Internet
Author: User

Program 1: [cpp] # include <iostream> using namespace std; void jiaohuan (int x, int y); // defines the int main () {int a, B; cin> a> B; if (a <B) jiaohuan (a, B); // call the exchange function // output the values of a and B, because the value of the form parameter is changed in the exchange function and the value of the real parameters a and B is not changed, the output is the original value cout <", B = "<a <", "<B; return 0;} // The void jiaohuan (int x, int y) {int t; result: Program 2: [cpp] # include <iostream> using namespace std; void jiaohuan (int * x, int * y); // defines the exchange function int ma In () {int a, B; cin> a> B; if (a <B) jiaohuan (& a, & B ); // call the exchange function // because the exchange function calls the address values of a and B, the function can change the value of the real parameter by changing the address value, so that a and B exchange. Cout <"a, B =" <a <"," <B; cout <endl; return 0 ;} // void jiaohuan (int * x, int * y) {int t; t = * x; * x = * y; * y = t;} result: program 3: [cpp] # include <iostream> using namespace std; void jiaohuan (int & x, int & y); // defines the int main () {int, b; cin> a> B; if (a <B) jiaohuan (a, B); // call the exchange function. // The function calls references of a and B, the values of a and B are exchanged by exchanging references of a and B in the function. Cout <"a, B =" <a <"," <B; cout <endl; return 0 ;} // The void jiaohuan (int & x, int & y) {int t; t = x; x = y; y = t;} result of the function exchange:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.