# 4.4 Call-by-reference and call-by-value

In the description of call statements and jump statements given in Section 3.4, the concepts of formal parameters and actual parameters were explained. When an actual parameter has been transported to a sub-program, if the sub-program ends after changing the value of the parameter, will it be reflected to the main program?

For example, let’s assume that a sub-program 0005\_pow3.job raises a value to the third power as follows:



|                |                                                                   |
| -------------- | ----------------------------------------------------------------- |
| 0001.job       | <p>var x=2</p><p>call 0005_pow3,x</p><p>print x</p><p>end</p> |
| 0005\_pow3.job | <p>param p</p><p>var t=p</p><p>p=t*t*t # (1)</p><p>end</p>    |
| Result         | 2                                                                 |

Although we expected that 8 is output because 2×2×2 is 8, the result is 2. It is because, when a numeric-type actual parameter is transported to a sub-program, the value is copied as a parameter. In other words, in (1), because the value raised to the third power was assigned to the copied version, it did not affect the value of the original parameter, x.

Therefore, the teaching program should be corrected so that the resulting value is transported by a return statement.



|                |                                                                                     |
| -------------- | ----------------------------------------------------------------------------------- |
| 0001.job       | <p>var x=2</p><p>call 0005_pow3,x</p><p>x=result()</p><p>print x</p><p>end</p> |
| 0005\_pow3.job | <p>param p</p><p>var t=p</p><p>p=t*t*t</p><p>return p</p>                       |
| Result         | 8                                                                                   |

On the other hand, in the case of arrays or objects, the reference of actual parameters, not the copied versions, will be transported. A reference refers to the position of a parameter.

In the following example, where the sub-program 0006\_pow3.job raises each element of an array to the third power, the values of the elements of the actual parameter array are changed.



|                |                                                                                                                      |
| -------------- | -------------------------------------------------------------------------------------------------------------------- |
| 0001.job       | <p>var x=[3, 2, 4]</p><p>call 0006_pow3,x</p><p>print x</p><p>end</p>                                            |
| 0006\_pow3.job | <p>param p</p><p>var t</p><p>for i=0 to len(arr)-1</p><p>  t=p[i]</p><p>p[i] = t*t*t</p><p>next</p><p>end</p> |
| Result         | \[27, 8, 64]                                                                                                         |

When a sub-program is called, if the copied version of the value of an actual parameter is transported, it is referred to as call-by-value; and if a reference is transported, it is referred to as call-by-reference. Whether it will be call-by-value or call-by-reference is determined by the type of values as follows:

|                   |                                    |
| ----------------- | ---------------------------------- |
| call-by-value     | Boolean, numeric, and string types |
| call-by-reference | Array and object types             |

![](https://3527803117-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MeFEqbwNTCpximKbwVQ%2F-MiaUJ1NdbpivakVqGvN%2F-MiaVRcOcCpKwRfyiNf3%2Fimage.png?alt=media\&token=b2d4ce08-7894-423d-a5fd-564d6410f896)

