languages with call by value only (and maybe closures)?

Hi, are there any languages where function call is only by value. If you pass a variable to a function you can only pass a copy of it. Inside of a function body you can mutate the values as desired. My thinking is that the called function is not going to have side effects on the state of the caller.

More interesting, is it possible to have data structures in such a language? I'm sorry if this seems very vague, but I am not very sure what I'm thinking of either.

Edit: also, what would the effect of closures in this type of language? Closures allow you to 'reach into' a returned value.

var f = function() {
var i = 0;
return {
function() { i++; },
function() { return i; }
}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Matlab

Many languages only support call-by-value, but they usually come equipped with some sort of reference value.

One exception (for the most part) is Matlab: Array values are passed to functions as if they were fresh copies. Naively implemented this would be very inefficient, which is why Matlab also performs a copy-on-write optimization (see: http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/).

Implementing *mutable* data structures is impossible in this scenario, but impurity can be tamed by simply having every function return a "modified" state.

So in some strange way, data

So in some strange way, data structures would have to be implemented like in functional languages, even though you are allowed to mutate state within the function!

q

q also works this way, also using copy-on-write. There are things that are nice about it, but there are also things that are a huge pain.

C

C only has call by value, although the value passed can be an address (pointer).

ADA

For an example of procedural/OO language, ADA has some strict rules on parameters copying.
Procedure parameters can be declared as 'IN', 'OUT' or 'IN OUT'

Inside the procedure, working copies can be altered and are finally written back to the calling procedure for OUT and IN OUT types.
If an exception occurs, the parameters values are not written back, in order to avoid incoherent or incomplete results.

(My ADA is quite rusty, so it may be a bit wrong)

This is more or less standard

CL, scheme, javascript, java, C, ... are all call by value and some support closures.

Sawzall supports a deeper sense of call due to lacking pointers, and it has closures.

Closures do not allow you to "reach into a returned value" -- you are returning a value, not a variable.

What closures do is to avoid passing. In sawzall that means that you can use a closure to propagate side effects.

He means that data

He means that data structures and objects, etc. all have value semantics and not reference semantics. So calling a function with an argument gets a copy of the argument that can be mutated without affecting the parent scope.

So do I ...

So do I ...

I don't see how. None of the

I don't see how. None of the examples you provided pass arguments by value, except special value types (eg. int). The question was basically, which languages have *only* value types.

Poorly Phrased

The post was about call-by-value semantics with local updates. As I read it, 'updates' to the closure don't affect other computations using that closure.

It's an unlikely combination of semantics since it the easiest interpretation would assume a technique where closures are copied always but are (made) mutable.

Call-by-value with local mutable lets are the most akin, and I fail to see what this particular semantics would provide extra over that. You're copying values anyway, locally overriding bound values wouldn't be that different from using local mutable variables.

[ Anyway, as I said, poorly phrased. He might mean that variables in a closure are mutable, which is pretty standard in some languages. Which just means something akin call-by-name semantics with lexical scoping rules. ]

The post did not use the

The post did not use the standard meaning of "call-by-value" as used in PLT, but he made his meaning pretty clear immediately afterward:

If you pass a variable to a function you can only pass a copy of it. Inside of a function body you can mutate the values as desired. My thinking is that the called function is not going to have side effects on the state of the caller.

This is the behaviour of System.ValueType in .NET, and of lvalues in C. The poster wants to know if there are any languages where *all* values are treated as lvalues/value types.

Basically

And indeed, q is an example of a language like this. (As is Matlab, apparently? Never knew that...)

Ah, and Poor Reading too

On my part. Forget it, I substituted a closure in somewhere.

Thats interesting - I'll

Thats interesting - I'll take a look into all these Q/Pure, #s. Although I know matlab, I never wondered what type of passing was going on - probably because I used it functionally, and so there wasn't any difference.

Yeah just to emphasise what naasking said
var i = { x : 1, y: 5 };
var f = fun(p, q) { p++; give p; }
var j = f(give i.x, copy i.y);
would result in:
i = { y: 5 }
j = 2 (and probably using the same original memory location)

What I wanted was some language that doesn't need locks because variables can only be in one place at a time. I read somewhere about linear types, but this language doesn't need to be typed, because something that is not linear would not be correct syntax. However, closures ruin all the fun because it destroys the only one reference idea.

newLisp

I believe newLisp uses this approach, with the possibility of explicitly passing reference lists.

Tcl

Tcl does this. "Everything is a string" (where strings are immutable) implies no first class mutable references. Tcl also does copy on write. (Tcl has a nice handful of efficient purely functional data structures). That EiaS mantra, which is oft maligned, has at least kept Tcl "pure" in that sense. Of course, there are still global mutable variables, and things like upvar for doing call by reference, so it's not all roses.