Questions tagged [call-by-value]

Evaluation that bounds the resulting value to the corresponding variable in the function (frequently copying the value into a new memory region).

95 questions
27
votes
5 answers

Why is an ArrayList parameter modified, but not a String parameter?

public class StackOverFlow { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("A"); al.add("B"); markAsNull(al); System.out.println("ArrayList elements are…
Naresh
  • 313
  • 1
  • 4
  • 8
21
votes
4 answers

What is call-by-need?

I want to know what is call-by-need. Though I searched in wikipedia and found it here: http://en.wikipedia.org/wiki/Evaluation_strategy, but could not understand properly. If anyone can explain with an example and point out the difference with…
9
votes
5 answers

Does const call by reference improve performance when applied to primitive types?

Concerning objects (especially strings), call by reference is faster than call-by-value because the function call does not need to create a copy of the original object. Using const, one can also ensure that the reference is not abused. My question…
Fabian
  • 4,001
  • 4
  • 28
  • 59
6
votes
1 answer

Why is my copy constructor only called twice in this scenario?

I have the following two functions: Class foo(Class arg) { return arg; } Class bar(Class *arg) { return *arg; } Now, when I solely call foo(arg), the copy constructor is of course called twice. When I call bar(&arg) solely, it's only…
Steffen
  • 3,999
  • 1
  • 23
  • 30
5
votes
2 answers

In Scala, when would be a good time to use lazily evaluated parameter rather than to use a function as a parameter?

def getStr(): String = { println("getStr is running") "str" } def lazyHello(para: => String) = { println("lazy hello is runing") println(para) } def notLazyHello(para: String) = { println("not lazy hello is runing") …
4
votes
2 answers

Difference between call-by-value and call-by-name interpreter for the lambda calculus

In another question, Bob presented the following interpreter for the untyped lambda calculus. data Expr = Var String | Lam String Expr | App Expr Expr data Value a = V a | F (Value a -> Value a) interpret :: [(String, Value a)] -> Expr -> Value…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
4
votes
4 answers

Why do programmers say that "pass by reference" is really "passing references by value?" Why is that important?

I know the whole concept of passing by reference in C & C++, and the similar concept of only pass by value in Java. But in a point of view Everything is pass by value isnt it? In C we pass a pointer of the variable to the function. So we are just…
Shaurya Chaudhuri
  • 3,772
  • 6
  • 28
  • 58
4
votes
0 answers

What is the difference between "call by reference" and "call by value, where the value is a pointer"?

I read this article about Python's variable bindings. It was criticized for teaching errouneous hypotheses. It stated that Python is neither "call by reference" nor "by value". Someone said in the comments that Python actually follows a clear "call…
Xiphias
  • 4,468
  • 4
  • 28
  • 51
4
votes
2 answers

yet another issue with call by value|reference

The question is simple and may has been discussed before, but I could find a clear answer for my case. Assume I pass a pointer object to a function #include "foo.h" int main() { foo * aFoo = new foo; bar(aFoo); delete aFoo; aFoo = NULL; …
mahmood
  • 23,197
  • 49
  • 147
  • 242
3
votes
1 answer

c# passing class type member as parameter works as call by value

as i know class type parameter is always passed as reference. but whenever i pass a class type variable which is class member as parameter. it always works like call by value. public class MapGenerator : MonoBehaviour { [SerializeField] …
haeinsa
  • 57
  • 4
3
votes
1 answer

What happens when we pass-by-value-result in this function?

Consider this code. foo(int x, int y){ x = y + 1; y = 10; x++; } int n = 5; foo(n,n); print(n); if we assume that the language supports pass-by-value result, what would be the answer? As far as I know, pass-by-value-result copies in…
Huzo
  • 1,652
  • 1
  • 21
  • 52
2
votes
1 answer

why in this code swapping happens when the "swap" function is written after int main() but not before it?

So my doubt is, I was trying out call by value, While running the given code, Swapping happens when I write the function definition after int main() But if I cut and paste the function definition above int main(), the swap does not take place. Why…
user12110725
2
votes
2 answers

Why is the pointer's value still 20?

void func(char *p) { int q = 13; p = &q; printf("%d\n", *p); } void main(void) { int var = 20; int *p = &var; printf("%d\n", *p); func(p); printf("%d\n", *p); } How come at the function exit the pointer is still…
Theo P.
  • 21
  • 2
2
votes
2 answers

Is this call by reference or by value in C?

I'm reading a book the c programming language authored by Brian W. Kernighan and Dennis M. Ritchie. The book lists code below void strcpy(char *s, char *t){ while((*s = *t) != '\0'){ s++; t++; } } and says: Because…
Andy Lin
  • 397
  • 1
  • 2
  • 21
2
votes
1 answer

Call-by-value and by-name equivalence

I'm working in a Coursera course on functional programming and at some point they discuss the difference between call-by-value and call-by-name evaluation techniques. They're some point that confuses me, they say: Both techniques reduce to the same…
1
2 3 4 5 6 7