C ++ journey-declaration and definition (1)

Source: Internet
Author: User

Recently, when I was working on a project, I found that some features close to the underlying operating system were not powerful, and many third-party things were used in the project, A large part of my work is dealing with hardware and I think it is necessary to review c ++ (abandon "she" for three years). I think it is better to return to it!

This trip records the beauty I have lost! Don't talk nonsense. Write it!

Concept Review: "Declaration" and "Definition"

The two concepts must be explained. First, we must know the difference between "Declaration" and "Definition", because these two terms often appear in our usual talking, it is the responsibility to understand them.

"Declaration" introduces the name to the computer and says, "What does this name mean ".

"Definition" assigns a bucket for this name.

 

Both variables and functions have the same meaning. In either case, the compiler allocates storage space in the "definition. For variables, the compiler determines the number of storage units occupied by the variables and generates space for them in the memory. For a function, the compiler generates code and allocates storage space for it. A function bucket contains a pointer generated by a function name without a parameter table or an address operator.

In addition, the definition can also be declaration. If the compiler has not seen the name A, and the programmer defines int A, the compiler immediately allocates A storage address for the name. Declarations often use the e x t e r n keyword. If we declare a variable rather than define it, we need to use e x t e r n. For function declaration, e x t e r n is optional. The function name without the function body, together with the parameter table or return value, is automatically declared.

The function prototype includes all information about the parameter type and return value. Int f (float, char); is a function prototype, because it not only introduces the name of the function f, but also tells the compiler what parameters and return values this function has, this allows the compiler to properly process parameters and return values. C ++ requires that a function prototype be written because it adds an important security layer.

The following are some declaration examples.

// Declare Variables
Extern int varible;
// Declare a function
Extern float foo (float, float );

Float B;
Float c;

// Define a function
Float foo (float a, float B)
{
Return a + B;
}

// Define Integer Variables
Int varible;
// Define a function
Int h (int x)
{
Return x + 1;
}

Void main (int argc, _ TCHAR * argv [])
{
Varible = 2;
B = 1.0;
C = 2.3;
Foo (B, c );
H (varible );
}

The parameter name can be provided or not given during function declaration. They are required in definition. This is true in C, but not necessarily in C ++.

In fact, it is very simple. In summary, the Declaration does not allocate memory space. The definition is opposite. The definition can be declaration, but the Declaration must not be definition!

 

Related Article

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.