DEV Community

Discussion on: Understanding recursions and memory

Collapse
 
pentacular profile image
pentacular
  • "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.

Collapse
 
therise3107 profile image
Lalit Yadav • Edited

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 :)

Collapse
 
pentacular profile image
pentacular

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.

int X = 1;
// X is an argument in the call to foo.
foo(X);
// Y is a parameter in the function
void foo(int Y) { ... }

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.

Thread Thread
 
therise3107 profile image
Lalit Yadav

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 ^

Thread Thread
 
pentacular profile image
pentacular

Consider the following code:

#include <iostream>

class Foo {
 public:
  Foo():v(0) {};
  int v;
};

void bar(Foo x) {
  x.v = 1;
}

int main() {
  Foo a;
  a.v = 0;
  bar(a);
  std::cout << "a.v is " << a.v;
  return 0;
}

What does this output and why? :)

Thread Thread
 
therise3107 profile image
Lalit Yadav • Edited

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 :)

Thread Thread
 
pentacular profile image
pentacular

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.

Thread Thread
 
therise3107 profile image
Lalit Yadav

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 :)