C ++ function declaration and definition

Source: Internet
Author: User

1. function declaration
In C and C Programs, a typical way to complete a job is to call a function to do that. Defining a function is a way for programmers to portray how to complete an operation. A function can be called only after being declared first.

In the life of a function, you need to give the name of the function, the type of the return value of the function, and the number and type of parameters that must be provided when the function is called. See the following statement:
Void fun (x, y); // This declaration is meaningless because the parameter type is unknown.

2. Function Definition
To call a function in a program, you must first define it in a certain place (only once ). The definition of a function is the function declaration of the function body. For example:
Extern void swap (int *, int *); // declare
Void swap (int * p, int * q)
{
Int t = * p;
* P = * q;
* Q = t;
}
The Function Definition and all its declarations must describe the same type. Because the parameter name is not part of the type, the parameter names do not need to be consistent.

3. Static variables
Local variables are initialized when the thread reaches its definition. By default, this happens when a function is called, and each function call has its own copy of a local variable. However, when a local variable is declared as static, it will only have a unique static object, and its initialization only occurs when the thread executes its definition for the first time.

See the following program:
# Include <iostream>
Using namespace std;
Void showstatic (int)
{
While (--)
{
Static int n = 0; // initialize once
Int x = 0; // initialize a time for each showstatic () call
Cout <"n =" <n <", x =" <x <endl;
}
}

Int main ()
{
Showstatic (3 );
}

The output of this program is:
N = 0, x = 0
N = 1, x = 0
N = 2, x = 0

As shown in the preceding output, static int n is initialized to 0 only when the function is called for the first time, and local variable x is initialized to 0 every time the function is executed to int x.

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.