Some details about C ++ [5: Function - Basics]

xiaoxiao2021-03-05  41

7. Function

7.1 C default Press value.

7.2 C is limited to function return value types, which cannot be an array, which can be structural and class objects, and built-in basic types. However, arrays can be returned as structural or class object components.

7.3 Usually returns the return value to the specified register or memory cell. Subsequently, the calling program will view the memory unit.

7.4 Function prototypes describe the interface of the function to the compiler. So, the compiler is high efficiency. Although the same information can be obtained by scanning a function.

7.5 In the parameter list of prototypes, the variable name can be included, or it may not be included. Its function is equivalent to occupying, so it is not necessary to be the same as the variable name in the function definition.

7.6 Ansi C Casset Optional Function Name After Brackets is empty, it means: do not point out parameters - will be defined later.

The C prototype ratio is selected, and only the parameter list is required for C functions that accept variable parameters (such as: Printf ()), using '...',

Such as: void sign_hi (...);

The C function name is empty and the keyword void is equivalent to the brackets. Means the function has no parameters.

7.7 Usually the prototype automatically converts the passable parameters to the desired type.

7.8 functions and arrays

Int Sum_arr (int Arr [], int size); int sum_arr (int * arr, int size);

The above two originals are the same.

** Note: In the function body, SIZEOF (SRR) is calculated by the size of the pointer. So it is necessary to incorporate the number of arrays.

Incoming a copy of the array address, Arr points to the original array address, so modify the array element in the function body.

7.9 Use const to protect array. Such as: void show_arr (const Int num [], int size);

This declaration indicates that the pointer NUM points to constant data and cannot modify the data with SUM.

7.10 pointer and const.

^ int A = 3;

^ Const * ps = $ a; // * ps is const, PS is not

^ INT * const pt = & a; // Pt is const, * Pt is not

* The address of the conventional variable can be assigned to the regular pointer.

* The address of the conventional variable can assign a value to the CONST. Such as: For PS, the value of A is constant, that is, the PS cannot be used to modify a value of A. The value of A can be modified using another method.

* You cannot assign a Const variable address to a regular pointer.

* You can assign a Const variable address to the Const pointer.

In all: Use const to make the function processes const and non-Const arguments, otherwise it will only accept non-Const data.

7.11 Const can only be used to point to a pointer to a basic type (built-in type, array, structure, class object). Can't be used to point to pointers.

7.12 Two-dimensional array Do the function prototype of parameters.

INT DATA [3] [4];

INT SUM (INT); / / prototype, (* Ar2) [4] indicates a pointer to an array consisting of four int.

* Ar2 [4] represents an array consisting of four pointers pointing INT.

INT SUM (int Ar2 [4], int size); // is another representation.

7.13 Function and Structure

* Structure name is only the name of the structure, you need to get its address to use '&'

* Like the basic type, you can transfer the structure by value, or the address can also be delivered, and the reference can be passed (see next article).

7.14 Function Pointer.

* Get the function address: The function name is the function address.

* Declaration function pointer: replace the original function prototype with * Pf.

Such as: Int Add (int, int); // original function prototype.

INT (* PF) (int, int); // function pointer prototype.

* Call: if: int sum = (* pf) (3, 4);

You can also use: int sum = pf (3, 4);

** Function Press Array: INT (* Pf [3]) (int, int); // prototype.

7.15 Transfer parameters are strings

INTCH_IN_STR (Const Char * STR, CHAR CH);

INTCH_IN_STR (const char str [], char ch); // 2 original equivalent

The behavior of string constants is the same as the array name.

转载请注明原文地址:https://www.9cbs.com/read-32466.html

New Post(0)