Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is "pass by value" synonymous with "functional programming" ?

  1. Are there any iterative programming languages that are (largely) "pass by value"?
  2. Are there any functional programming languages that are (largely) "pass by reference"?

Thanks.

share|improve this question
2  
Java is pass by value... –  assylias Aug 18 '13 at 5:06
    
Functional programming is a software development model. It estimates and applies mathematical functions, rather than state or mutable data and imperative code threads. - More in wiki en.wikipedia.org/wiki/Functional_programming –  Yusubov Aug 18 '13 at 5:07
    
I am interested in a specific answers, (i) are there functional languages that, by default, are "pass by reference" and (ii) are there are iterative languages that, by default, are "pass by value". (the second question has been answered by assylias above) –  uday Aug 18 '13 at 5:17
1  
actual pass by reference is very rare now, most languages pass by value –  aaronman Aug 18 '13 at 5:50
1  
The semantics of Haskell are such that it makes no difference whether you pass the immutable thing in by value or by reference. In practice the compiler often passes by reference because that is more efficient. –  btilly Aug 18 '13 at 6:15

2 Answers 2

up vote 9 down vote accepted

Is "pass by value" synonymous with "functional programming" ?

No, They are not just "not synonymous", they are completely and utterly unrelated. They have absolutely nothing whatsoever to do with each other.

Are there any iterative programming languages that are (largely) "pass by value"?

I am assuming you mean imperative programming languages. Yes, there are imperative programming languages which are pass by value. In fact, there are very few which aren't: C, Java, JavaScript, Python, Ruby, pretty much all Lisps, Smalltalk, Self, Newspeak, Io, Ioke, Seph, are just some of the examples. They are not just "largely" pass by value, they are pass by value. Period. No exceptions, no ifs, no buts.

C++, C#, Visual Basic.NET, PHP are pass by value by default, you need to explicitly ask for pass by reference, e.g. using the ref keyword in C#.

Are there any functional programming languages that are (largely) "pass by reference"?

The difference between pass by value and pass by reference can only be observed when the reference is mutated. Functional programming languages don't allow mutation, ergo it is impossible to tell whether a functional programming language is pass by value or pass by reference. Language implementors make use of this by choosing either one of the two strategies depending on which one yields better performance for a particular piece of code.

For example, large objects will be passed by reference, but smaller objects where the object itself is as big as a reference or smaller (such as e.g. integers in a tagged pointer representation) will be passed by value.

Note also that both pass by value and pass by reference are eager evaluation strategies, whereas some of the more popular functional programming languages (e.g. Haskell, Frege) have non-strict semantics and thus are usually implemented using some form of lazy evaluation. Some of the more popular Haskell compilers for example use call by need with limited sharing.

share|improve this answer

C is an imperative language that is pass by value. Fortran is an imperative language that is pass by reference. C# supports both functional programming (lambdas, etc.) and pass by reference.

Therefore, the answer is no, they are not synonymous.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.