5 Replies - 1011 Views - Last Post: 07 January 2009 - 04:54 AM

#1 Welly  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 07-December 08

Pass by value

Posted 06 January 2009 - 07:32 PM

Is pass by value less efficient? It's more on the stack so it must be compared to pass by refrence like c, c++. anyone have thoughts on this?
Is This A Good Question/Topic? 0
  • +

Replies To: Pass by value

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Pass by value

Posted 06 January 2009 - 07:41 PM

Pass by value creates a copy. (slow, Depends on size of object)
Pass by reference creates a pointer to the value. (Fast,Typically 4 bytes in size)
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pass by value

Posted 06 January 2009 - 07:45 PM

It depends if you're asking about efficiency in regards to memory consumption or speed. Generally passing by reference is faster/more efficient in both instances.


edited for a typo

This post has been edited by KYA: 06 January 2009 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 Welly  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 07-December 08

Re: Pass by value

Posted 06 January 2009 - 08:12 PM

Yea knew what they were LOL. So are you saying that pass by value consumes less ram though? I never thought of the 2 efficiency sides of it. I thought ram was speed and how much memory needed to be readily availible. Never thought of it like that though.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pass by value

Posted 06 January 2009 - 09:06 PM

Since you have to create a shallow copy you are using more memory when passing by value. Passing by references uses less physical memory.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,357
  • Joined: 16-October 07

Re: Pass by value

Posted 07 January 2009 - 04:54 AM

The only time to pass by value is if that value is a small primitive. Or you actually want to work on a copy and no the original.

For example, references are usually of int size, so to only reason to pass an int by ref is to modify it. Also, most examples of recursion you see will make use of pass by value for math problems.

Yes, a copy must be made for pass by value, and if the thing you're copying is big you're taking a big hit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1