C + + Learning Notes (vii): functions

Source: Internet
Author: User
Tags function prototype

Function General format:

1 typeName functionname (parameterlist) 2 {3     // Statements 4     return value; // value is type cast to type TypeName 5 }

Function Prototypes:

C + + functions must provide a function prototype;

1 intAddintAintb);//function Prototypes2 intAddint,int);//You can also omit the parameter name3 4 //function Definition5 intAddintAintb)6 {7     returnA +b;8}

Detailed function parameters

Formal parameters and arguments:

1 voidAddintAintb)2 {3     intc = A +b;4cout <<C;5 }6 7 voidMain ()8 {9     intI=1, j=2;Ten Add (i, j); One}

In the example above, A and B are formal parameters, and I and J are arguments.

Parameter passing:

There are 3 ways to pass the parameters in C + +;

(1) Value transfer

This is the value of the variable passed to the parameter, and the pass is one-way. If the value of the parameter changes during the execution of the function, it is not passed back to the argument. Because parameters and arguments are not the same storage unit when a function is called.

1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7     voidSwapint,int);//function Declaration8     intI=3, j=5;9Swap (I,J);//Call function SwapTencout<<i<<" "<<j<<endl;//the values of I and J are not interchangeable One     return 0; A } -  - voidSwapintAintb//attempt to interchange values of arguments I and j by exchanging values of parameters A and B the { -     inttemp; -Temp=a;//The following 3 lines are used to exchange values for a and B -A=b; +b=temp; -}

(2) pointer passing

A parameter is a pointer variable, an argument is the address of a variable, and a parameter (pointer variable) points to a real parametric cell when the function is called.

This combination of real and false is still the "value-passing" method, but the value of the argument is the address of the variable. Variables (i and J) in the main function are accessed through parameter pointer variables, and their values are changed. This will get the right result, but conceptually it's a circle.
, not so straightforward.

1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7     voidSwapint*,int*);//function Declaration8     intI=3, j=5;9Swap (&AMP;I,&AMP;J);//Argument is the address of a variableTencout<<i<<" "<<j<<endl;//the values of I and J have been interchanged One     return 0; A } -  - voidSwapint*P1,int*P2)//formal parameters are pointer variables the { -     inttemp; -TEMP=*P1;//The following 3 lines are used to exchange values for a and B -*p1=*P2; +*p2=temp; -}

(3) Reference passing

The parameter becomes an alias of the argument, both the formal parameter and the argument are the same object, and the Java language non-primitive data type parameter has the same effect, the use is more convenient than the pointer pass;

1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7     voidSwapint&,int&);8     intI=3, j=5;9 swap (I,J);Tencout<<"i="<<i<<" "<<"j="<<j<<endl;//the values of I and J have been interchanged One     return 0; A } -  - voidSwapint&a,int&AMP;B)//formal parameters are reference types the { -     inttemp; -temp=A; -A=b; +b=temp; -}

function parameters use the const modifier:

Indicates that the value of the parameter cannot be modified in the body of the function (including the value of the parameter itself or the value contained in the parameter).

1 voidfunctionConst intVar);//the passed arguments cannot be changed within the function (meaningless because Var itself is a formal parameter) .2 3 voidfunctionConst Char* Var);//the contents of the parameter pointer are constant immutable4 5 voidfunctionChar*ConstVar);//The parameter pointer itself is a constant invariant (also meaningless, since char* var is also a formal parameter)6 7 voidfunctionConstclass& Var);//reference parameters cannot be changed within a function8 9 voidfunctionConsttype& Var);//reference parameters are constants within a function immutable

function return detailed

The return value of main function main:

As mentioned here, returning 0 indicates that the program ran successfully.

Returns a non-reference type:

The return value of the function is used to initialize the temporary object created in the Skip function. Initializing a temporary object with a function return value is the same as the method of initializing the taxiing parameter with an argument. If the return type is not a reference, the function return value is copied to the temporary object where the function is called.
And its return value can be either a local object or a result of solving an expression.

Return reference:

When a function returns a reference type, no return value is copied. Instead, the object itself is returned.

Considerations for returning references:

(1) Returns a reference that requires a parameter to be returned in a function's argument, including a reference or pointer. Like what:

1 int& ABC (intAintBintCint&result)2 {3result = A + B +C;4     returnresult;5 }6 //This form can also be rewritten as:7 int& ABC (intAintBintCint*result)8 {9*result = a + B +C;Ten     return*result; One } A //However, the following form is not possible: - int& ABC (intAintBintc) - { the     returnA + B +C; -}

(2) Never return a reference to a local object. When the function finishes executing, the storage space allocated to the local object is freed. At this point, a reference to the local object points to the indeterminate memory. Such as:

1 Const string &manip (conststring &s)2{3     string ret = s; 4     return ret; // wrong:returning reference to a local object 5 }

Functions and const explanations

1 Const int foo (int  a); 2 Const int foo (intconst;

When the const is in front of the function name, it modifies the function return value, loses the function of the left value, but saves the copy, increases the efficiency.

After the function name is a constant member function, the function cannot modify any members within the object, only read operations can occur, and write operations cannot occur.

C + + Learning Notes (vii): 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.