Real questions for software designers, special exercises in programming language (3)

Video explanation

Software Designer Morning Questions Programming Topic

The concepts of call by value and call by reference

When the function is called, the basic parameter passing methods include passing by value and passing by address, (20). (First half of 2009)
(20)
A. In the value-passing mode, the formal parameter passes the value to the actual parameter
B. In the pass-by-value mode, the actual parameter cannot be an array element
C. In the address transmission mode, data can be transferred in both directions between formal parameters and actual parameters.
D. In the address transmission mode, the actual parameters can be any variables and expressions.

The basic parameter passing methods when calling a function are pass-by-value and pass-by-address. In the pass-by-value method, the value of the actual parameter is passed to the formal parameter, so the actual parameter can be an expression (or constant). It can also be a variable (or array element). This kind of information transfer is one-way, and the formal parameter can no longer transfer the value to the actual parameter. When passing the address, N'H is the address passed to the formal parameter. Therefore, the actual parameter must be a variable (array name or array element), not an expression (or constant). In this way, the modification of the formal parameters in the called function is actually the modification of the actual parameters, so objectively, two-way transfer of data can be achieved.

Passing by value and passing address are information transfer methods often used in function calls, (50). (First half of 2011)
(50)
A. In the value-passing mode, the value of the formal parameter is passed to the actual parameter< a i=3> B. In the value-passing mode, the formal parameter can be an expression of any form

C. In the address passing mode, the address of the actual parameter is passed to the formal parameter.
D. In the address transmission mode, the actual parameter can be an expression in any form

When a function is called, it may need to accept data information passed in from the outside. Calling by value and calling by reference (passing address) are the information transfer methods commonly used in function calls. Call by value is to pass the value of the actual parameter to the formal parameter of the called function. The essence of call by reference is to pass the address of the actual parameter to the formal parameter of the called function.

When a function (procedure) is called, the two methods of passing by value and passing by address are often used to transfer information between actual and formal parameters. In the following statement, the correct one is (50). (First half of 2012)
(50)
A. In the value-passing mode, the value of the formal parameter is passed to the actual parameter. Therefore, the formal parameter must Is a constant or variable
B. In the value-passing mode, the value of the actual parameter is passed to the formal parameter. Therefore, the actual parameter must be a constant or variable
C . In the address passing mode, the address of the formal parameter is passed to the actual parameter. Therefore, the formal parameter must have an address
D. In the address passing mode, the address of the actual parameter is passed to the formal parameter. Therefore, the actual parameter must have an address.

When a function is called, it may need to receive data information passed in from the outside. Calling by value and calling by reference (passing address) are the information transfer methods commonly used in function calls. Call by value is to pass the value of the actual parameter to the formal parameter of the called function, so the actual parameter can be a constant, variable, expression or function call, while the essence of the call by reference is to pass the address of the actual parameter to the called function. formal parameters, so the actual parameters must have addresses.

Among the following descriptions of call by value and call by reference, the correct one is (21). (First half of 2013)
①In the call-by-value method, the effect of bidirectional data transfer between formal parameters and actual parameters can be achieved
②In the call-by-value method In the mode, the actual parameters can be variables, constants and expressions
③In the reference call mode, the effect of bidirectional data transfer between formal parameters and actual parameters can be achieved< a i=4> ④In the reference call mode, the actual parameters can be variables, constants and expressions (21) A. ①③ B. ①④

C. ②③ D. ②④

There are two main ways to exchange information between the calling function and the called function: one is for the called function to return the return value to the calling function, and the other is to pass information through parameters. The basic methods for exchanging information between real and formal parameters during function calls are call-by-value and call-by-reference.
If the actual parameter passes a value of the corresponding type to the formal parameter when calling a function, it is called a call by value. In this way, formal parameters cannot pass information to actual parameters. Actual parameters can be variables, constants, and expressions.
The essence of the reference call is to pass the address of the actual parameter variable to the formal parameter. Therefore, the formal parameter is a pointer type, and the actual parameter must have an lvalue. Variables have lvalues, constants have no lvalues. The access and modification of formal parameters by the called function are actually the access and changes made to the corresponding actual parameters, thus achieving the effect of bidirectional data transfer between formal parameters and actual parameters.

Parameters are often used to transfer information between functions (procedures) during the running of a program. Actual parameters are passed in calls by reference (20). (Second half of 2013)
(20) A.AddressB. Type C. Name D. Value

When making a function call, it is often necessary to pass the data in the calling environment to the called function, which will be processed by the called function as an input parameter. The basic calling methods are call by value (or call by value) and call by reference. Among them, the value call
mode transfers the value of the actual parameter to the formal parameter of the called function one-way. The reference calling mode passes the address of the actual parameter to the formal parameter. In the calling function, indirect access and modification of the actual parameter variable data are achieved through pointers, thereby
achieving the effect of "passing back" the modified value.

Function calling in reference calling mode is (21). (First half of 2014)
(21)
A. The value of the actual parameter is passed to the formal parameter B. The address of the actual parameter is passed to the formal parameter
C. The value of the formal parameter is passed to the actual parameter. D. The address of the formal parameter is passed to the actual parameter.

Calling by value and calling by reference are the two basic ways to pass parameters when calling functions. In the value calling mode, the value of the actual parameter is passed to the formal parameter. In the reference calling mode, the address of the actual parameter is passed to the formal parameter.

The commonly used methods of passing function parameters are pass-by-value and pass-by-reference. (twenty one) . (Second half of 2016)
(21)
A. In the value-passing mode, formal and actual parameters pass values ​​to each other
B. In the pass-by-value mode, the actual parameter cannot be a variable
C. In pass-by-reference mode, modifying the formal parameter essentially changes the value of the actual parameter.
D. In pass-by-reference mode, the actual parameters can be any variables and expressions.

Calling by value and calling by reference are two commonly used methods of parameter passing. In the call-by-value method, the value of the actual parameter is passed to the formal parameter. This transfer is one-way. After the call is completed, the value of the formal parameter will not be passed to the actual parameter. In the call-by-reference mode, the address of the actual parameter is essentially passed to the formal parameter, and the formal parameter is modified in the called function in the indirect data access mode with the help of a pointer (or the formal parameter is regarded as an alias of the actual parameter). In essence, it is a modification of the actual parameters.

Call by value and call by reference calculation

The definitions of functions t and f are as follows, where a is an integer global variable. Suppose the value of a before calling function t is 5, then when calling function f in function t by call by value, the output is (49) When called in function t by call by reference When function f is used, the output is (50). (Second half of 2011)
Insert image description here

(49) A. 12 B. 16 C. 20 D. 24
(50) A. 12 B. 16 C. 20 D. 24

When a function call occurs, the main methods for exchanging information between the calling function and the called function are call by value and call by reference.
If the actual parameters are in the form of AHun when calling a function, the formal parameters cannot transfer information to the actual parameters.
In C language, to modify the actual parameters of the called function, a pointer must be used as a formal parameter.
With
, you need to first perform an address operation on the actual parameters, and then transfer the address of the header to the remote call.
Reference is a data type added in C++. When the form is used, the modification is essentially the access and change to the corresponding actual parameter variable.
In this question, the value-passing calling method is: "X=.1(a)T0", the value of the last variable a is from 5 to r, and the function is executed in this way When f, the initial value of r is 5, and after N a -ITI19 is assigned the same value, v. Therefore, the execution becomes 6, and then "r=r2" changes the value of r to 10, "return r" returns 10 and assigns it to x,
片一
"print a+x" outputs 16.
In the call-by-value mode, f H is called in the expression "x= f(a)", I determine a a5 and then "r =r
2" will r's. Therefore, after After the "a= r+1" operation, the value of a
, 24 is output.

The definitions of functions t() and f() are as follows. If the value passed to x is 5 when calling function t, and when calling function f(), the first parameter adopts the call by value method. The two parameters adopt the call by reference method, and the return value of function t is (50). (Second half of 2015)
Insert image description here

(50) A. 33B. 22 C. 11 D. Negative number

Insert image description here

The definitions of functions main() and f() are as follows. When calling function f(), the first parameter adopts the call by value method, and the second parameter adopts the call by reference method. main The value output after execution of "print(x)" in the function is (50). (First half of 2016)

(50) A. 1 B. 6 C. 11 D. 12

When a function call is executed, call by value means passing the value of the actual parameter to the formal parameter, and the formal parameter variable gets a copy of the actual parameter value. Calling by reference essentially passes the address of the actual parameter variable to the formal parameter variable. In the calling function, the actual parameter variables are accessed indirectly through pointers. In this way, the modification of the formal parameters is actually the modification of the actual parameter variables. In this question, when function f is executed, the value of its formal parameter x is 5. The statement "x=2*x+1;" changes the value of x in function f to 11, and the formal parameter a actually refers to main. For x in a function, if main_x is used to represent the variable x in the surface function, and f_x is used to represent the formal parameter variable x in the function f, then the essence of the statement "a=atx;" is "main_x = main_x + f_x;", so The result is that the value of x in the main function is changed to 12.

The definitions of functions main() and f() are as follows. When calling function f(), the first parameter adopts the call by value method, and the second parameter adopts the call by reference method. main The value output after execution of "print(x)" in the () function is (49). (First half of 2017)
Insert image description here

(49) A. 11 B. 40 C. 45 D. 70

If the value of the actual parameter is passed to the corresponding formal parameter when calling a function, it is called a call by value. In this way, formal parameters cannot pass information to actual parameters. The essence of call-by-reference is to pass the address of the actual parameter to the formal parameter. The access and modification of the formal parameter in the function are actually the access and changes made to the corresponding actual parameter variable.
According to the title description, when calling f, the first parameter is passed by value, and the second parameter is referenced. Therefore, in the main function, first add 1 to the value of its local variable x. (i.e. 6) is passed to the first parameter x of f, and the address of x in main is passed to the second parameter a of f. Therefore, the modification of a in f is equivalent to the modification of x in main.
In f, the initial value of x is 6. After the "x=x*x -1" operation, it is modified to 35. After the "a=x+a" operation, the value of a is Change to 40. What needs to be noted here is that x in f and x in main are two different and independent variables.

The definitions of functions main() and f() are as follows. When calling function f(), the first parameter adopts the call by value method, and the second parameter adopts the call by reference method. The output value of the function main() after execution is (50). (Second half of 2017)
Insert image description here

(50) A. 10 B. 19 C. 20 D. 29

When implementing function calls, formal parameters have independent storage space. In the pass-by-value mode, the value of the actual parameter is copied to the formal parameter; in the pass-by-reference mode, the address of the actual parameter is passed to the formal parameter, or it can be understood as an alias of the formal parameter named as the actual parameter in the called function. Therefore, modifications to formal parameters are essentially modifications to actual parameters.
The variable x in the main function in the question is a local variable. When calling function f, the first formal parameter x of f is a local quantity of function f, and its initial value is the actual parameter passed in. 10, modified to 19 after the operation "x= 2*x-1". The second formal parameter of function f is a reference parameter. Accessing a in f is essentially accessing x in the main function. After the operation "a= a+x", a is modified to 10+19=29, which is also That is, the local variable x of the main function was changed to 29.

The definitions of functions f and g are shown in the figure below. If function g(a) is called by reference (call by reference) when executing function f, the return value of function f is (50). (Second half of 2018)
Insert image description here

(50) A. 14 B. 18 C. 24 D. 28

When calling g in function f, the actual parameter is a. In the reference calling mode, the formal parameter x of g can be regarded as an alias of a in f in function g. Therefore, when g is executed, its expression The corresponding calculation of "m=xm" is 52, which is assigned to m. The value of m is changed to 10, and the expression "x=m-1" The corresponding calculation is that 10-1 is assigned to x, the value of x is changed to 9 (that is, the value of a in f is changed to 9), and when g ends, the value of x+m (that is, 9+10) is returned, 19. After returning to function f, the value of c is changed to the result 19 of calling function g. When f ends, the result 28 of a+c (i.e. 9+19) is returned.

The definitions of functions f and g are as follows. When executing the operation of the expression "y=f(2)", the function call g(la) adopts the call by reference method and the call by value method respectively, then The value of y after the evaluation of this expression is completed is (50). (First half of 2019)
Insert image description here

(50) A. 9、6 B. 20、6 C. 20、9 D. 30、9

When f(2) is called, the value of parameter x passed to function f is 2, and the value of la is 3. When g(la) is called by reference, when function g is executed, the value of g The modification of parameter x is equivalent to the modification of la, so la is changed to 10 through x=xx+1, so the value of the expression lax is 102=20; when calling g(la) by value, the value of la in f does not change, and the value of expression lax is 3*2=6.

The functions foo() and hoo() are defined as follows. When calling the function hoo(), the first parameter adopts the call by value method, and the second parameter adopts the call by reference method. Suppose the function calls foo(5), then the value output after "print(x)" is executed is (48). (Second half of 2020)
Insert image description here

(48) A. 24 B. 25 C. 30 D. 36

The functions foo() and hoo() are defined as follows. The two parameters of calling function hoo(a, x) are passed by call by reference and call by value respectively, then the function calls foo(5) The output result is (50). (First half of 2021)
Insert image description here

(50) A. 2,5 B. 2,15 C. 13,5 D. 13,15

Guess you like

Origin blog.csdn.net/weixin_50843918/article/details/134905071