1. c + + statement
This section refers to the procedure section:
#include <iostream> int main() { using namespace std; // Using namespace std, int a, b; // The declaration statement declares two variables A and B, gives them storage space named a and B, and indicates that they can store integer (int) data. a = 5; // In the assignment statement, "=" is the assignment operator, which means that the data or value on the right is assigned to the left. Here, 5 is assigned to storage space a, that is, the value in space a is 5. b = 1; // Assignment statement, assign 1 to storage space b cout << "i have" << a << "apples" << endl; a = a - b; // Assignment statement, indicating that the data on the right of the assignment symbol can be operated or changed. cout << "she has" << a << "apples" <<endl; return 0; }
1.1 declaration statements and variables
First, let's take a case: in python, the creation of variables does not need to be declared. That is, when you want to create variables, write directly: myage = 22. In this way, the variable "myage" is created, and its value is 22. Next, you want to operate on this variable again: myvge = myage + 1;, According to my original intention, I want to add 1 to myage, but myage is misspelled as myvge, which goes against my meaning. When the program outputs: print(myage), it will output: 22, and myvge becomes another variable I created by mistake.
In this way, you can see the importance of declaring the creation of variables. If you do not declare, there are hidden bug s in the program.
The declaration statement format of c + + is: declaration type + declaration variable name, such as int myname, int is the type of the declared variable, int is integer, that is, the declared variable is integer, and myname is the name of the declared variable. Here, it tells the computer that I need a space (memory unit) called myname to store an integer data.
The declaration of c + + variables does not have to be at the beginning of the function, but for convenience, it should be declared before the variable is used for the first time.
1.2 assignment statement
int myage; int hisage; int herage; myage = 22; hisage = herage = myage;
The assignment statement assigns the value to the storage unit, "=" is called the assignment operator, and the value on the right is assigned to the storage space on the left. c + + allows the continuous use of assignment symbols to assign the values on the right to the variables on the left.
1.3 input statement
The input statement uses the command cin to indicate that the data flow flows from the outside to the inside.
#include <iostream> int main() { using namespace std; int num; cout << "How much do you have?" << endl; cin >> num; // Input numbers from the keyboard; cout << "Then i will give you two yuan" << endl; cout << "Now you have "<< num +2 << " yuan" << endl; //Use multiple < < symbol splicing statements. return 0; }
The operation result is:
2. Function introduction
There are two kinds of c + + functions, one with return value and the other without return value. After a function with a return value passes data, the function returns a result. If there is no return value, it operates on the data but does not return. Let's introduce it in detail.
In addition, c + + has its own standard libraries, in which functions can be called directly. Users can also write a function to realize the predetermined function.
2.1 functions with return values
A function with a return value generates a value that can be assigned to a variable or used in other expressions. For example, the standard c + + library contains a sqrt() function that returns the square root. The code is as follows:
double x; //Declare the variable x as double, that is, X can be decimal. x = sqrt(4); //After the function processing, the result of the input parameter 4 is 2, and this value is passed to the variable x.
The expression sqrt(4) is called a function call, and the called function is called a called function (sqrt()).
The value in parentheses (4 here) is the information sent to the function, which is called passing to the function. The value sent to the function in this way is called the parameter. The result obtained by the function sqrt() is 2.5, and the value sent back is called the return value of the function.
Before using a function, the c + + compiler needs to know the parameter type and return value type of the function. The way c + + provides this information is to use the function prototype statement.
A function prototype is to a function what a variable declaration is to a variable, indicating the type involved. For example, the prototype of sqrt():
double sqrt(double);
The first double indicates that the function will return a double value, and the second double indicates that sqrt() requires a double parameter. The usage method is as follows:
double x; x = sqrt(4.0);
The x variable of the code block is first declared as double, which meets the requirement that the return value of the function in the sqrt() prototype is double, and the input data is 4.0, which meets the requirement that the input parameter in the prototype is double.
2.2 functions without return value
Some functions have printed or operated the required results in the function body, so they do not need to return values.
In addition, some functions can also not accept incoming parameters, such as rand(), and directly return a random value without passing parameters.
2.3 user defined functions
If the library functions of c + + can not meet the user's needs, users can write their own functions. First, each c + + program must have a main () The function is called the main function, and the user must define it. All operations to be completed by the user will be carried out in this main function. Whether using library functions or user-defined functions, the prototype needs to be provided before main(). For user-defined functions, the code body is generally placed after main().
Function format: function header + function body in square brackets.
Note: c + + does not allow function definitions to be nested in another function. Each function definition is independent and all functions are created equally.
Instance 1 (function without return value):
// your func.cpp --- defining your own function #include <iostream> void money(int); //A user-defined function prototype. The function name is money. You need to enter shaping parameters. The function has no return value. int main() { using namespace std; int n; cout << "how much do you have: "; cin >> n; money(n); // Call function return 0; } void money(int n) // Define function { using namespace std; cout << "i have " << n << " yuan" << endl; }
Instance 2 (modified based on instance 1, function with return value):
// your func.cpp --- defining your own function #include <iostream> int dollar_to_rmb(int); //A user-defined function prototype. The function name is dollar_to_rmb. You need to enter shaping parameters. The return value of the function is of type int. int main() { using namespace std; int n, rmb; cout << "how much do you have(dollars): "; cin >> n; rmb = dollar_to_rmb(n); // Call function cout << "So, you have " << rmb << " yuan" << endl; return 0; } int dollar_to_rmb(int n) // Define function { using namespace std; return 7*n; // Use return to return parameters }