Fan Lie C + + 3rd Chapter preliminary understanding of Functions

Source: Internet
Author: User

1 //fan Lie C + + 3rd Chapter2 3#include"stdafx.h"4#include"iostream"5 6 //3.1 a simple function7 voidShow ()8 {9std:: cout <<"Hello word!\n" ;Ten } One voidFUN1 ()///3.1 a simple function A { -std:: cout <<"function starts \ n" ;  - Show (); thestd:: cout <<"function end \ n" ; -  - } -  + //3.2 parameter of the function - using namespacestd; + intAddintXinty) A { at     returnX +y; - } - intfun2 () - { -     intA; -     intb; in     intC; -  tocout <<"Please enter a total of two integers:" ; +CIN >>A; -CIN >>b; thec =Add (A, b); *cout <<"A + b ="<< C <<Endl; $ Panax Notoginseng     return 0; - } the  + //3.4 Declaration and definition of functions A voidA ();//declare function A. the voidB ();//Declare function B. + intMain_fun4 ();//declares the function main_fun4. - voidFun4 () $ { $Main_fun4 ();//The function main_fun4 is called in the Fun4 function. - /*int Main_fun4 ()//This is a wrong way to call the function. -     {                  the A (); If you want to implement the function of another function (F2) within a function (F1), you must only invoke it. - B (); A function (F2) cannot be declared directly inside a function (F1) and defined.Wuyi          the return 0; -     }  */     Wu } - voidA ()//defines the specific contents of function A. About { $Std::cout <<"function a\n" ;  - } - voidB ()//defines the specific contents of function B. - { AStd::cout <<"function b\n" ;  + } the intMain_fun4 ()//define the specific contents of the function Main_fun4. - { $A ();//function A was called in Main_fun4. theB ();//function B was called in Main_fun4. the  the     return 0; the } -  in //3.5 Local Variables the //The variable declared inside the function is a local variable, only the function is present, and the local variable ends when the function ends. the //For example, there are two variables, a, B in the function F1. They can only be stocked in the function F1, and when they leave F1, A and B are meaningless and unusable. About voidSwapint,int); the intfun5 () the { the     intx; +     inty; -      thex =3;Bayiy =4; the  thecout <<"In the fun5 function, before the swap function is called, the value of X is:"<<x <<", the value of Y is:"<< y <<Endl; -Swap3,4); -cout <<"In the fun5 function, before the swap function is called, the value of X is:"<<x <<", the value of Y is:"<< y <<Endl; the  the     return 0; the } the  - voidSwapintXinty) the { thecout <<"In the swap function, before the swap function is called, the value of X is:"<<x <<", the value of Y is:"<< y <<Endl; the 94     intZ; thez =x; thex =y; they =Z;98  Aboutcout <<"In the swap function, before the swap function is called, the value of X is:"<<x <<", the value of Y is:"<< y <<Endl; - }101 102 //3.6 Global Variables103 voidfunc ();104 intx =3;//when a global variable is defined, it is assigned a value. Otherwise the compilation cannot pass. the inty =4;106 //x = 3; //Global variables, you cannot define them first, and then assign values.107 //y = 4;108 /*109 Global Variables Although int a = 1 and int b = 2, they can be compiled. the 1. However, if there are no programs in the entire file that use global variables, the compiler will not allocate memory space for global variables.111 2. If the F1 function uses global variables, F1 's function framework does not generate more than 8 bytes of memory space for global variables. the */113 intfun6 () the { thecout <<"in the FUN6 function: \ n" ; thecout <<"X ="<< x <<", Y ="<< y <<Endl;117 func ();118 119     return 0; - }121 voidfunc ()122 {123cout <<"in the Func function: \ n" ;124cout <<"X ="<< x <<", Y ="<< y <<Endl; the }126 127  - intMainintargcChar*argv[])129 { theFun1 ();//3.1 a simple function131std:: cout <<"-----------------------\ n" ; the 133     //fun2 (); //3.2 parameters for function transfer134std:: cout <<"-----------------------\ n" ;135 136Fun4 ();//3.4 Declaration and definition of functions137std:: cout <<"-----------------------\ n" ;138 139Fun5 ();//3.5 Local Variables $std:: cout <<"-----------------------\ n" ;141 142Fun6 ();//3.6 Global Variables143std:: cout <<"-----------------------\ n" ;144 145     return 0;146}

Fan Lie C + + 3rd preliminary understanding of functions

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.