Zerglurker C language tutorial 006 -- first function, zerglurker language tutorial

Source: Internet
Author: User

Zerglurker C language tutorial 006 -- first function, zerglurker language tutorial

Well, after the previous courses, you should have a preliminary understanding of the basic concepts of main functions, variables, and naming.

Next we can start our first UDF.

Add a new project Lession006 to the solution, following the operations of Lesson 1 and Lesson 2.

Copy main. cpp and public. h of Lesson 1

Then join the project as instructed:

Right-click a project and select an existing item in the Add column.

Select the copied code and click Add.

Although it is the first custom function, it should not be too difficult. Let's get a function that shows the Fibonacci series.

First, set the project to start the project to facilitate debugging:

Right-click the project and select set as startup project.

Enter the following code in main. cpp:

# Include <stdio. h> int main (int argc, char * argv [], char * env []) {maid (10); return 0;} void maid (unsigned int nTotal) {int nFirst = 1; int nSecond = 1, nThird; unsigned int I; if (nTotal = 0) {return; // if the number of outputs required by the user is 0, then nothing is output} printf ("% d \ n", nFirst); if (nTotal = 1) {// if the number of outputs required by the user is 1, return;} // output the first two numbers to printf ("% d \ n", nSecond); if (nTotal <= 2) // If you specify no more than 2 outputs, return; for (I = 2; I <nTotal; I ++) {nThird = nFirst + nSecond; printf ("% d \ n", nThird); nFirst = nSecond; nSecond = nThird ;}}
Select project-generate or press F7

If everything is normal, you can see the following compilation errors:

1> ------ generated: Project: Lession006, configuration: Debug Win32 ------ 1> main. cpp1> e: \ vsproject \ c-c ++ lession \ lession006 \ main. cpp (5): error C3861: "ononacci": unable to find the identifier ========== generated: 0 successful, 1 failed, 2 latest, skip 0 ============
Why is this error reported?

In fact, this kind of mistake will always accompany you, even throughout your entire career-from you or your colleagues.

In the main function of the code, we call the function Fibonacci, which is the code: Fibonacci (10 );

It tells the program that I want to generate a ten-digit Fibonacci Series

However, when the compiler explains this, Fibonacci has not been found. Yes, it is still behind, and the compiler is too late to see it.

So it reported an error because I don't know what the name of Fibonacci is, so it politely said:

1> e: \ vsproject \ c-c ++ lession \ lession006 \ main. cpp (5): error C3861: "ononacci": unable to find the identifier

In row 5th of the e: \ vsproject \ c-c ++ lession \ lession006 \ main. cpp file, I don't know what to do with this identifier.

Learn to read the error information because it helps you quickly locate the error and analyze the cause.

What should I do now? One of these methods is to move the Fibonacci code to the front of the main. But this is very bad, because for a project with hundreds of thousands of functions, the main function should not be written until the end.

But you don't have to worry about it. Another way is to declare a function.

Find public. h

Enter the following content:

Add an Include statement before main. cpp:

Now it's okay to compile everything.

Because when the compiler interprets Fibonacci (10);, it knows that this is a function call and the parameter is an unsigned integer.

All of this is what the public. h file tells it.

# The meaning of include is to tell the compiler that you first read the specified file and then read down my code.

Press ctrl + F5 to see the following content:

Exactly 10 Fibonacci numbers

If you want more, you only need to change the value of "10" to "10 ".

You can try other numbers, including negative numbers to see what the results are (this is the content of the next lesson)

For a detailed explanation of the code of this lesson, I will talk about it next time and it will be here today.


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.