Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to formulate the parameters of C++ function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to develop C++ function parameters, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

C++ function is the main part of C++ language program, an internal function can call other functions. In well-designed programs, each function has a specific purpose, which also confirms the object-oriented characteristics of C++ language.

However, if there are operators with side effects in the argument expression, ambiguity may occur due to the different order of evaluation. For example, int z = add_int (+ + x, x _ compiler);, so that different compilers may produce different results.

Set the default value of the parameter

In the C++ language, it is allowed to specify a default value for one or more parameters when the C++ function is described or defined. However, it is required that a parameter without a default value cannot appear to the right of a parameter with a default value specified. For example:

Add_int (15)

In the above description of the function add_int (), the default value is specified for the rightmost argument of the C++ function. When the C++ function is called, the compiler combines the actual participating arguments in the order from left to right. When the number of arguments is insufficient, the compiler will fill in the missing arguments with the default values in the description or definition in the same order. For example, if you have the following function call expression:

Int add_int (int x, int 10)

When assigning a default value to a parameter, it can be not only a numeric value, but also an arbitrarily complex expression. Using the array as the C++ function parameter, the array as the function parameter can be divided into the following three cases: (the results of these three cases are the same, but the calling mechanism is different)

1. Arrays are used for both formal and actual parameters

The argument of the calling function uses the array name, and the formal parameter of the called function uses the array. The mechanism of this call is that the formal parameter and the argument share the same array in memory. Therefore, a non-prime value in the array is changed in the called C++ function, and the element value of the array in the calling function is also changed, because they share the same array.

two。 Both parameters and arguments use pointers to the corresponding array.

In the C++ language, the array name is defined as a pointer, which is the pointer to the first element of the array, and its value is the address value of the first element of the array, so the array name is a constant pointer. In practice, it is possible to use a pointer for one parameter and an array for the other. You can use an array name when using a pointer, or you can use another defined pointer to an array.

3. Arguments are referenced by array names

How to use references to array types is explained here: first, an int array type is defined with a type definition statement, and then arrays and references are defined using array.

Example:

# include typedef int array [8]; int a [8] = {1, 3, 5, 7, 9, 11, 13}; void fun (array & b, int n) {for (int irid0; I b [7] + = b [I];} void main () {int massi8; fun (a, m); cout <}

In this program, in the fun () function, a reference is used as a formal parameter, and the argument corresponding to the call should be an array name, and the reference here is to give the array an individual name. The operation on array b in the fun () function is equivalent to the operation of array a referenced by b. This method of calling is commonly used in the C++ language.

After reading the above, do you have any further understanding of how to formulate the parameters of the C++ function? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report