Pass-by-value and pass-by-address issues

Pass-by-value and pass-by-address examples

Real questions for the soft exam in the first half of 2023:

22. Suppose the definitions of functions foo and hoo are as shown in the figure below. When calling function hoo in function foo, the first parameter of hoo is called by reference, and the second parameter is called by value. , then print(a, b) in function foo will output ()

 

A.8,5
B.39,5
C.8,40
D.39,40

Reference answer: B

Answers and analysis

Question Difficulty: Easy

  • Knowledge points: Fundamentals of programming languages ​​> Passing by value and passing by address
  • Analysis of test questions:

According to the calling process described in the question stem, the first parameter of hoo() is a reference call , and the second parameter is a value call . Therefore, the modification of m in hoo() will eventually affect the transfer in the original foo() function The parameter a is the final value of a printed. According to the hoo() function process, the initial parameter of b is the original m = 5, at this time m = x *m = 8*5=40 (note that m here is a local variable, which is only used in hoo()), x is initially passed The parameter is the original a = 8. At this time, x=m-1 = 40-1=39 , and the final global variable a value is 39 . (Note that the original a here is a global variable, which can be understood as the alias x in the hoo() parameter . Now m is a local variable, which is what was obtained before.
5)。

Inference: 2020 Question 39

The functions foo() and hoo0 are defined as follows. When calling the function hoo(), the first parameter adopts the method of passing value (call by value), and the second parameter adopts the method of passing reference (call by reference). Assuming function call (function foo(5)), then the value output after execution of "print(x)" is ( ).

 

  • (A) 24
  • (B) 25
  • (C) 30
  • (D) 36

Answers and analysis

  • Question Difficulty: Easy
  • Knowledge points: Fundamentals of programming languages ​​> Passing by value and passing by address
  • Answer to test question: [['A']]
  • Analysis of test questions:

According to the calling process described in the question stem, the first parameter of hoo() is call by value, and the second parameter is call by reference. Therefore, the modification of a in hoo() will eventually affect the transfer in the original foo() function The parameter x, which is the final printed value of x.
According to the hoo() function process, the initial parameter of x is the original args=5, at this time x=x-1=4 (note that x here is a local variable, only used in hoo()), and the initial parameter of a is the original x =6, at this time a=a*x=6*4=24, the final global variable x value is 24. (Note that the original x here is a global variable, which can be understood as the alias a in the hoo() parameter. Now x is a local variable, which is the 4 obtained previously).

Guess you like

Origin blog.csdn.net/ck3345143/article/details/132236312