amanangira / blog-comments

A repository to hold my blog comments in form of GH issues.
0 stars 0 forks source link

tech/go-function-arguments-are-always-pass-by-value/ #3

Open utterances-bot opened 2 months ago

utterances-bot commented 2 months ago

Go Function Arguments Are Always Pass by Value | amanreasoned

This article demonstrates different ways in which pointers can be passed to functions as arguments and how they can affect the original variable or create new versions of the pointer. Each approach has its use cases and trade-offs depending on the specific requirements of a program.

https://www.amanreasoned.com/tech/go-function-arguments-are-always-pass-by-value/

rp21buzz commented 2 months ago

This pattern here has more to do with scope of the function argument variables than Go's 'function arguments are always pass by value', because the pattern is same in cpp too. and it's more of to do with the depth of 'pass/call by value' 'pass/call by reference'.

in incrementByPointer the statement a = &b, means the function variable is being reassigned with a new value and hence the change isn't reflected outside the function, as opposed to a = a + 1 which is modifying the value located in the address passed into function argument variable a at the time of the call.

unlike in python the implicit handling of non-primitive type function arguments as call by reference and changes to them even without the use of ref operator (*) - concept which doesn't exist in python - reflect outside the function.

amanangira commented 2 months ago

@rp21buzz thank you for stopping by and sharing your wisdom from CPP. Often analogies from our own understanding only help us draw better patterns.

rp21buzz commented 2 months ago

Agreed, everyone makes up their own analogies to understand concepts but the analogies must be correct.

nevertheless i would suggest a different title for the article "GO: call-by-value vs call-by-reference and their use cases."

amanangira commented 2 months ago

@rp21buzz I am sorry but I do not follow you. I want to take a step back and elaborate on the context of my article which is as follows.

the parameters of the call are passed by value to the function and the called function begins execution. The return parameters of the function are passed by value back to the caller when the function returns. Source

When we look at the above spec from official Go blog, I am not drawing any comparison between "pass by reference vs pass by value". I haven't included any example of "pass by value" in comparison to "pass by reference" altogether, and given the limitation of a length of a title I have also tried to highlight what the article talks about in the introduction.

To conclude, in order to draw better comparison I could include comparison with references from Java, however IMO that would exceed the scope of this article. I understand that the understanding of every individual could be subjective in regards to the title but I have only quoted what is present there in spec sheet.