C ++-Functions

Source: Internet
Author: User

I. Introduction to Functions
In procedural programming, In order to smoothly implement a large function, this large function is usually decomposed. For example, to implement a student information management system, a student information management system should at least contain the student information.Add,Query,Delete,ModifyAnd other functions, these functional requirements are the initial decomposition of the system, and thenQueryThe query function can be further decomposed into the following modules:

①.Obtain user query conditions;
②.Query the student information in the database;
③.Output query results in a certain format.

If you feel it is still difficult to implement this function, continue to break down until it can be implemented smoothly. The decomposition of these functions relies on functions. functions are broadly understood as a module with specific functions. After functions are implemented, they are called to use this function.

 

 

Ii. function call Process
In any valid C ++Program-Main ()This function is also called the entry function of the C ++ program. The function is used to tell the program to start executing commands from here, that is, any c ++ program is executed from the main () function until the end command of the program is encountered during execution.

In C ++, functions can be called to each other. If there is no hierarchical relationship between functions, the function cannot be called. That is to say, the main function can call any function, including itself. A user-defined function can call other user-defined functions, or library functions. The user-defined function can also call the main function.

The call relationship is as follows:



The illustration is as follows:

In this illustration, an arrow is used to describe the call relationship. The arrow points to the person who is called, and the tail of the arrow refers to where the function is used. As shown in the figure, main ()
Functions call functions 1 (), 2 (),... functions N (), while the implementation of function 1 () calls function 3, and the implementation of function 3 calls function 2.
. Function 2 is repeatedly called twice. One is in the main () function, and the other is in function 3 (), which is equivalent to being reused twice.

This figure shows another advantage of using functions,CodeIt can be used repeatedly to write some simple basic functions into a function. When to use the function, it is called and no longer necessary to write it.

 

 

Iii. Example of a function
For example, you can call a function to output "Hello, world ":

 1 # Include <iostream> 2   3       Using  Namespace  STD;  4   5       //  Define the sayhello Function  6       Void  Sayhello ()  7   {  8           //  The braces show the function implementation.  9 Cout < "  Hello, world! \ N "  ;  10   }  11   12       Int  Main ()  13   {  14 Sayhello (); //  Call the custom sayhello Function  15   16           Return   0 ;  17 }

After compilation and running, "Hello, world" will be output on the screen, which is the same as the result of using cout directly in the main function. In this example, we define the nameSayhelloFunction, before the nameVoidIs his return value type, followed by the content in braces is the specific implementation process of the function, also knownFunction bodyBecause the function can output "Hello, world" on the screen, only one row is written in the function body.Cout <"Hello, world! \ N ";.

After defining the sayhello function, you can call it in the main function. The call process is simple. Add a pair of parentheses to the function name and a semicolon to the end.

 

 

Iv. Function Definition and calling
A function is defined as composed of a function header and function implementation. The general form is as follows:
Return Value Type Function Name (parameter list)
{
Function body
}

1>. Return Value Type
After the function is executed, a value is returned, indicating that the function has been executed. The value can be a defined value or a result obtained after the function is computed/operated, it can be char, Int, float, double, pointer type, or the custom data type that will be learned later.

To return a value, useReturnStatement. When the return statement is executed, the function returns a value and ends the execution of the function. Even if there are other statements, the function will not be executed again.
For example, we change the above sayhello function to a type with return values as follows:

 1 # Include <iostream> 2   3           Using   Namespace  STD;  4   5          //  Define the sayhello Function  6           Int  Sayhello ()  7   {  8               //  The braces show the function implementation.  9 Cout < "  Hello, world! \ N  "  ;  10               Return  100 ; //  Return Value 100 after execution  11 Cout < "  Succeed! \ N  "  ;  12   13   }  14   15           Int  Main ()  16   { 17               Int Val = 0  ;  18 Val = sayhello (); //  Call the custom sayhello function and assign the return value to the variable Val.  19 Cout < "  Return Value:  " <Val <Endl; //  Output the return value of the sayhello Function  20  21               Return   0  ;  22 }

 
Output:

 
Hello, world!Return Value:100Process returned0(0x0) Execution time:0.594S press any keyContinue.

This time, the return type of the sayhello function is changed to int, And the return value is 100. We added a sentence after the return statement.Cout <"succeed! \ N ";It is used to determine whether the return statement is actually finished when the function is executed. According to the output result, it is actually finished, followed by "succeed! "Not output.

The main function defines a variable of the int type (consistent with the return value type ).ValIt is used to save the return value of the sayhello function and output it.

2>. Function Name
The function name must comply with the requirements defined by the C ++ identifier. It can only consist of letters, numbers, and underscores and cannot start with a number. Try to make the name concise and clear on the basis of the requirements. It is best to let people look at the function name to roughly guess what the function is.

3>. Parameter List
The parameter list is used to pass information to a function. For example, you need a function to compare the two numbers and output a large one, so that the function does not know which two numbers are required.
For example, pass two int-type values to the function to produce a larger output value.

 1 # Include <iostream> 2   3           Using   Namespace  STD;  4   5           //  Define Max Functions  6          Void Max ( Int A, Int B) //  Int A, int B is the parameter list  7   {  8               If (A> B)  9 Cout <A < Endl;  10               Else  11 Cout <B < Endl;  12   }  13   14           Int  Main ()  15   {  16               Int X = 10 , Y = 20 ; //  Number of instances to be compared  17 Max (x, y ); //  Call and input the values of X and Y  18   19               Return   0  ;  20 }


When declaring the parameter list, int A and int B are two int types, indicating that two parameters will be passed in. The first parameter will be represented by,
The value of a is the value of the first input parameter. The second parameter is represented by B. Similarly, the value of B is the value of the second input parameter. A and B are called form parameters, it is also called "form parameter ".

In the main function, calling the max function passes in X, Y. Here, X and Y are called real parameters.Therefore, in this program, the value of a in the parameter list is the value of X, and the value of B is the value of Y. Therefore, we can, the value of B determines the size of X and Y.

Source: http://www.cnblogs.com/mr-wid/archive/2013/01/23/2874082.html

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.