C + + Language Basics-function rules

Source: Internet
Author: User
Tags array length arrays command line function prototype numeric numeric value variables

· A function can take any number of arguments or no parameters.
· A function can return a value, but the function does not force it to return a value.
· If the function returns a void type, the value cannot be returned.
If you want a function that returns a void type to return a value, a compilation error occurs. A function that returns a void type does not need to include a return statement, but it can also contain this statement. If there is no return statement, the function returns automatically when it reaches the closing curly brace at the end.
· If the function prototype represents a function return value, the function body should contain a return statement that returns a value, and a compilation error occurs if the function does not return a numeric value.
· A function can take any number of arguments, but only a single number.
· A variable can be passed to a function by value, pointer, or reference (described later).
Syntax: the declaration (prototype) format of a function statement is as follows: Ret_type function_name (argtype_1 arg_1,argtype_2 arg_2,..., argtype_n);
A function declaration represents a function to be included in the code, and should display the return data type (Ret_type) and function name (function_name) of the function, representing the order of the data variables (arg_1,arg_2,..., arg_n) and type (Argtype_1, Argtype_2,... argtype_n).
Function statements are defined in the following format:
Ret_type function_name (argtype_1 arg_1,argtype_2 arg_2,..., argtype_narg_n);
{statements;
return ret_type; }
function definitions represent the code blocks (statements) that make up a function, you should display the return data type (RET type) and function name (function_name) of the function, including the data variables (arg_1,arg_2,..., arg_n) and type ( Argtype_1,argtype_2,... argtype_n).

Main () function
C + + programs must have a main () function. The main () function is the entry point of the program. Each sample program described earlier has the main () function. However, not all C + + programs have a traditional main () function. The Windows Program entry point function written in C or C + + is called WinMain () rather than the traditional main () function. Explains that C + + Builder GUI applications have WinMain () but are hidden. C + + Builder users do not need to consider the low-level details of Windows programs, but can focus on the program user interface and other parts of the creation. The main () function, like other functions, is a function that has the same constituent parts. In a 32-bit console application, C + + Builder generates a default main () function with the following prototypes: int main (int argc,char** argv); this main () function takes two arguments and returns an integer value. As mentioned earlier, values are passed to the function when the function is called. But for the main () function, there is no direct call, but it is executed automatically when the program is run. So how does the main () function get the argument? The approach is to get it from the command line. It is described as follows: Suppose there is a WIN32 console application to execute at DOS prompt with the following command line: grep wm_killfocus 杁-I
To start the program grep with the command line variable wm_killfocus, d, and I, we'll show you how to turn it into argc and argv in the main () function. First, the integer variable ARGC contains the number of arguments passed in the command line, at least 1, because the program name counts as a parameter. A variable argv is an array of pointers that contain strings. This array contains each string that is passed in the command line. In this example:
ARGC contains 4
Argv[0] contains C:|cbuilder|bin|grep.exe
ARGV[1] contains Wm_killfocus
ARGV[2] contains D
ARGV[3] contains I
The following is a small program to verify this fact. Generate a new console application in C + + Builder and enter the program shown in Listing 1.5. List 1.5argstest.cpp
1: #include <iostream.h>
2: #include <conio.h>
3: #pragma hdrstop
4:
5:int Main (int argc,char **argv)
6: {
7:cout << "argv =" argc << end1;
8.for (int i=0;i<argc;i++)
9. cout << "Parameter" << I << ":" << argv[i]<< end1;
cout << end1 << "Press any key to continue ...";
11:getch ();
12:return 0;
13:}
Save this item as Argstest, and then instead of clicking the Run button, select the Project| in the main menu Build all so that only the project is built and no program is executed. After the project is completed, select the Run| in the main menu Parameters, enter the following in the Runparameters dialog runparameters field: One two three "four five" six then click the Run button and the program runs with the command line arguments you specify. Another option is to run the program at DOS prompt with the following command line: Argstest One two three "four five" six The program runs, it displays the number of incoming changes, and then lists each variable. Run several times, each time to provide a different command line variable, pay attention to the resulting results.
The return value of the main () function in most programs is not important because the return value is not usually used. In fact, you can return a numeric value without requiring the main () function. The main () function has several forms, the following declarations are valid: main (); int main ();//Same as above
int main (void); Same as above
int main (int argc,char** argv);
void Main ();
void Main (int argc, char** argv);
There are more forms. If you do not want to use command line arguments, you can use the first main () function, which takes no arguments (empty in parentheses) and returns an int (returns the default return value when unspecified). In other words, the most basic form of the main () function does not take arguments and returns an int.

Array
Any C + + intrinsic data type can be placed in an array. An array is a collection of values. For example, suppose you want to save an integer array with five integer values. The array can be declared as follows: int myarray[5]; Here the compiler assigns the memory space shown in Figure 1.7 to the group. Because each int is stored in 4 bytes, the entire array occupies 20 bytes of memory space.
MARRAY[0]MARRAY[1]MARRAY[2]MARRAY[3]
MARRAY[4]
Baseaddrbasseaddr+4baseaddr+8
Baseaddr+12baseaddr+16
After declaring the array, you can fill in the value with the following foot-sign operator ([]):
Myarray[0] =-200;
MYARRAY[1] =-100;
MYARRAY[2] = 0;
MYARRAY[3] = 100;
MYARRAY[4] = 200;
Visible from above, arrays in C + + are based on a 0 base. In subsequent programs, you can access the elements of an array with the foot-sign operator:
int result=myarray[3]+myarray[4]; Result would be 300
There is also a simple way to declare and populate the entire array of contents as follows:
int myarray[5] = {-200, -100,0,100,200};
Further, if you know the number of elements in an array and populate an array when you declare an array, you can omit the length of the array when you declare the array. For example: int myarray[] = {-200, -100,0,100,200}; This is possible because the compiler can determine the number of elements in the array and the memory space allocated to the arrays from the assigned numeric table.
Arrays can be multidimensional. To generate a two-D integer array, you can use the following code:
int mdarray[3][5];
This allocates 15 int space (total 60 bytes). The elements of an array can be accessed as a one-dimensional array, providing only two foot-sign operators: int x = mdarray[1][1]+mdarray[2][1];
Figure 1.8 The appearance of two-dimensional arrays in memory warning note Do not overload the end of the array.
A powerful feature of C + + is the ability to access memory directly. Because of this feature, C + + cannot prevent you from writing to a specific memory address, even if the address is not accessible by the program. The following code is legitimate, but can cause a program or Windows crash: int array[5];array[5]=10; This is a common error because the array is based on 0 and the maximum pin should be 4 instead of 5. If you overload the end of an array, you cannot know which memory is overwritten, making the results unpredictable, or even causing the program or windows to crash. This type of problem is difficult to diagnose because the memory that is affected usually takes a long time to access, and then crashes (which makes you inexplicable). So be careful when writing to arrays.
Array rules
• The array is based on a 0 base. The first element in the array is 0, the second element is 1, the third element is 2, and so on.
• The length of the array should be a compilation constant. The compiler must know how much memory space is allocated to an array at compile time. You cannot specify an array length with a variable. So the following code is illegal and can cause a compilation error:
int x = 10;int Myarray[x]; Compiler Error Here
Be careful not to overload the end of the array.
· Large arrays are allocated from the stack (heap) instead of stacks (see later). · Arrays that are allocated from a stack can specify the length of an array with a variable. For example: int x = 10;int* myarray = new Int[x]; This is OK

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.