In: Computer Science
We learned that when calling subprograms or functions that we can pass data to the subprogram or function by ‘value’ or by ‘reference’. Describe what each approach is, how it works and what the potential security disadvantage is when passing parameters by reference.
When you pass parameters to a function, you can do so in two ways:
When you pass a parameter to a function by value, then a copy of the variable is made in memory. When you access or modify the variable in the function, then only the copy of the variable is modified. The original variable is left untouched.
When you pass a parameter to a function by reference, you pass the memory address of the function, so it is the same variable being passed to the function. So when you modify the variable from the function the original value of the variable is changed too.
When passing parameters by reference you are allowing the
original value of the variable to be modififed, which in some cases
might lead to a function changing the value even when the function
is not intended to do so. The programmer will need to take special
care to not accidentally modify the variable.
If you like the answer, please support by upvoting it. And
if you have any doubts regarding this, please comment in the
comment section, I am sure to help.