"Copy by reference is generally dynamic memory while copy by value is static"
I think you're going to need to elaborate on this, since it doesn't make a great deal of sense.
In what regard is 'copy by value' static?
In what regard is 'copy by reference' generally dynamic?
"Tail recursion just returns the function itself"
I this there may be a typo here, since there's no requirement for a tail recursive function to return the function itself.
"In conclusion, recursions can be avoided by using iteration"
Iteration is recursive.
In this article you where you write 'recursion', you should generally seem to mean 'calling functions'.
If you write "calling functions can often be avoided by using iteration", then it is certainly true.
Recursion, on the other hand, does not imply function calls.
Hey @pentacular
, thanks for reading and yes you are right here by recursion I meant calling functions.
Here, copy by reference was in the context of referring to class objects, and copy by value meant struct members. Potentially what I could have written would have been pass by value and pass by reference since I'm writing about functions and their parameters.
Yeah there could be some typos, English is not my first language and writing posts is an effort to improve that. The general idea was to have an elaborated approach towards learning algorithms and why the recursive functions could be a bad idea over a simple for or while loop.
As a matter of fact, I have little to no professional experience with C, I am a javascript developer(trying to shift into C/C++) so the attempts more or less are just reflection of a little bit of self-learning.
Always open to suggestions and ways to improve. Thanks :)
Thanks for this :) I completely understand it now and how I misused the concepts in the main article however aren't class objects always pass by reference ? in the context of C++. @pentacular
^
This should print 1 but this is printing 0. My thinking is x implicit reference to a so if x changes a should also change.
If I do it like this explicitly void bar(Foo& x) then it is printing 1. I wonder what is happening behind the scenes here.
My source of the learning is from Bjarne c++ 4th edition ( First 2 section done). I also did C++ in 2015-16 during the final year in college I'm familiar with the syntax.
This is extremely helpful to me as I was looking for some guidance already :)
The explanation is that your thinking is incorrect. :)
In the example x is not a reference, so a is passed by value.
The value of a is assigned to x, which is an independent variable.
So changes to x do not affect a.
There is no 'implicit reference' in C++.
When you change x to be Foo& , a is passed by reference.
This is the difference between pass by value and pass by reference, and class instances are passed by value as usual.
Yeah, I was confused initially about why to use references with the object now it makes much more sense to me. C++ is surely deep :). Thanks for your valuable comments, I will be posting my C++ endeavors as I go on learning new stuff but this time I will be precise with my word selection and topic :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think you're going to need to elaborate on this, since it doesn't make a great deal of sense.
In what regard is 'copy by value' static?
In what regard is 'copy by reference' generally dynamic?
I this there may be a typo here, since there's no requirement for a tail recursive function to return the function itself.
Iteration is recursive.
In this article you where you write 'recursion', you should generally seem to mean 'calling functions'.
If you write "calling functions can often be avoided by using iteration", then it is certainly true.
Recursion, on the other hand, does not imply function calls.
Hey @pentacular , thanks for reading and yes you are right here by recursion I meant calling functions.
Here, copy by reference was in the context of referring to class objects, and copy by value meant struct members. Potentially what I could have written would have been pass by value and pass by reference since I'm writing about functions and their parameters.
Yeah there could be some typos, English is not my first language and writing posts is an effort to improve that. The general idea was to have an elaborated approach towards learning algorithms and why the recursive functions could be a bad idea over a simple for or while loop.
As a matter of fact, I have little to no professional experience with C, I am a javascript developer(trying to shift into C/C++) so the attempts more or less are just reflection of a little bit of self-learning.
Always open to suggestions and ways to improve. Thanks :)
You're welcome.
Class and struct instances have the same semantics with respect to copying and passing.
To understand pass-by-reference and pass-by-value we need to think about arguments vs parameters.
With pass-by-value, Y is assigned the value of X.
Changing Y will not change X -- they are different variables.
With pass-by-reference, Y is a reference to X.
Changing Y will change X -- they are effectively the same variable.
There is no difference between class and struct instances with respect to this.
Thanks for this :) I completely understand it now and how I misused the concepts in the main article however aren't class objects always pass by reference ? in the context of C++.
@pentacular ^
Consider the following code:
What does this output and why? :)
This should print
1
but this is printing 0. My thinking isx implicit reference to a
so if x changes a should also change.If I do it like this explicitly
void bar(Foo& x)
then it is printing 1. I wonder what is happening behind the scenes here.My source of the learning is from Bjarne c++ 4th edition ( First 2 section done). I also did C++ in 2015-16 during the final year in college I'm familiar with the syntax.
This is extremely helpful to me as I was looking for some guidance already :)
The explanation is that your thinking is incorrect. :)
In the example x is not a reference, so a is passed by value.
The value of a is assigned to x, which is an independent variable.
So changes to x do not affect a.
There is no 'implicit reference' in C++.
When you change x to be Foo& , a is passed by reference.
This is the difference between pass by value and pass by reference, and class instances are passed by value as usual.
Yeah, I was confused initially about why to use references with the object now it makes much more sense to me. C++ is surely deep :). Thanks for your valuable comments, I will be posting my C++ endeavors as I go on learning new stuff but this time I will be precise with my word selection and topic :)