3.3.hello world function Edition
It's time to write code. Let's write a function first.
Use the code: blocks Wizard to create a console application named helloworldfn.
Open the main. cpp file. If you want to use Chinese characters in the code, remember to set the main. cpp encoding to "System Default ".
In this section, our code will continue to evolve. First, let's take a look at the familiar original version:
001 #include <iostream>
003 using namespace std;
005 int main()
006 {
007 cout << "Hello world!" << endl;008 return 0;009
010 }
3.3.1. define functions
First, insert the following code snippet (excluding the row number) between 003 and 005 ):
005 void Hello()
006 {
007 cout << "Hello!" << endl;
008 }
Now, we have defined a function named "hello". Its function is to block the output of a sentence "Hello !". The result type of this function is void, indicating that no return value is required. Therefore, return is not found in this function.
After compiling and running this program, we find that the output result is no different from the classic version, and the screen still prints "Hello World !".
Tip: the code in the function takes effect only when it is called.
Define a function, but never call it, the code in the function will not be executed. This function definition has no effect on program execution. When the compiler is ready for optimization, it even has the ability to Remove unused functions from the final program.
3.3.2. Call a function
Next, let's call the hello function. We call it in the main function. Rewrite the main function to the following, and change the part to the 012 line. The code snippet is as follows:
010 int main()
011 {
012 Hello();
013 return 0;
014 }
Compile and run the program. The output on the screen is "Hello !". This line is not directly output in Main. On the contrary, it is to call the hello function through the main function, and then output it in the latter.
3.3.3. Repeated calls
This time, after 012, we insert a row of Hello (), that is, two consecutive calls to hello (). The code snippet is as follows:
012 Hello();
013 Hello();
Compile, run ...... This example shows an important function of a function: it facilitates code reuse. That is, if you find that a piece of code is often used, you can consider encapsulating the code into a function.
「 Easy moments 」: when do I need a function?
Imagine a bright morning when you walked into the company building, and you were in a good mood. When you saw colleague a, you smiled and nodded, "Hello !"; When you see B, you smile and nod: "Hello !"; When you see little C, you smile and nod: "Hello !"...... Your neck is a little sour. The advertisement word "at this moment, you need a hello function "!
However, you may be worried about writing a function and calling it in multiple places. Will the code of this function appear everywhere, resulting in a large program file?
The answer is: normally not, but there are exceptions ......
<Tip>: Only one function definition is available.
"Zhang shoutong" went out to work and went to a's house to clear the house in the morning.SewersTo B in the afternoonDredgingToilet. When he got home at night, his wife opened the house. Wow, two Quicklinks are back! ----This is impossible. In C ++, a functionThis is called and called, but the program does not generate two identical functions unless ......
Exception: There is a function called "inline". In order to improve service efficiency, they can be considered as Sun Wukong in every call. Inline may cause a sharp increase in the size of the program, and there are strict implementation conditions, so we usually do not use much.
3.3.4. Function with Parameters
Again, it was a bright morning. When you walked into the company building, you were in a good mood and saw small A, Small B, small C ...... You "Hello, hello, hello" keep smiling and nodding. Suddenly, there was a bald, fat, and stiff-faced person in front of you. You suddenly got nervous: "Hello, boss ......".
It's not just for the boss, but for everyone, it's really rude to be "hello" in a uniform manner. Now let's change the hello function to include parameters.
005 void Hello(string const name)
006 {
007 cout << "Hello! " << name << "." << endl;
008 }
Parameter type: String const is a constant string in C ++.
String is a class defined in the C ++ standard library. We need to include it. Add:
002 #include <string>
[Field job]: Observe compilation errors related to "function parameters ".
Compile the above Code and observe the compilation error information. If you cannot see the compilation information, press f2 to switch out the information window. The compilation error information is displayed on the "build record" page of the Information Window ).
Please (at least literally) understand the following compilation error information:
(Figure 15 compilation error message (when calling a function, the number of parameters is insufficient ))
A compilation error occurs in the job because no parameter is passed to the hello function. Please correct this question:
012 Hello("Xiao A");
013 Hello("BOSS");
Let's ask Mr. A and the boss. Compile and run the program.
(Figure 16 running result of the example with the hello function parameter)
The final code of this section is attached:
#include <iostream>
#include <string>
using namespace std;
void Hello(string const name)
{
cout << "Hello! " << name << "." << endl;
}
int main()
{
Hello("Xiao A");
Hello("BOSS");
return 0;
}
-------------- Welcome to the vernacular C ++, which will be published soon -----------------------
If you want to communicate with me, click the following link to become a friend:
Http://student.csdn.net/invite.php? U= 112600 & C = f635b3cf130f350c