-1

I am studying the Side Effect for different programming language.

For instance, in C++, if I define

x = 1;

int f(int y)
{
  x = x + y;
  return x;
}

and execute f(1) the new value of x is equal to 2.

In python, I can surely execute define the same function

x = 1

def f(y):
   x = x + y
   return x

but when I execute f(1) in that situation, I obtained the error "local variable 'x' referenced before assignment."

Why do I have a different result for the same function and execution, but in different programming languages? Could I have a side effect in Python compared to C++? Is it because C++ is compiled and Python is interpreted?

J.G
  • 167
  • 1
  • 1
  • 7
  • What is the side-effect here for Python? Integers are immutable, so I don't see how Python is relevant... – roganjosh May 07 '20 at 20:19
  • 4
    Unfortunately the best answer to "Why do I have a different result...in different programming languages" may be "Because different languages do things differently, otherwise there would only be one programming language" – G. Anderson May 07 '20 at 20:20
  • @roganjosh Could I have side effects in python compared to c++? – J.G May 07 '20 at 20:22
  • 1
    Please take a minute to first understand what that error message means. You will find that your programs are not equivalent and that this has nothing to do with side effects. As a new user, please also take the [tour] and read [ask]. – Ulrich Eckhardt May 07 '20 at 20:23
  • @David I don't know C++ but your code is broken in Python without `global`, which is an anti-pattern in itself. It's not clear to me what you're trying to reconcile. I don't know what you mean about side-effects, specifically – roganjosh May 07 '20 at 20:24
  • @UlrichEckhardt I just want to understand the difference between these two programming languages. Why do I have two different results with the same implementation and execution. Is it because C++ is compiled and Python is interpreted? – J.G May 07 '20 at 20:28
  • 1
    You don't have different results with the same implementation and execution. You have different implementations and different executions. If you just learned the languages, you would see the difference. – Ulrich Eckhardt May 07 '20 at 20:30
  • Python is _mostly_ implemented in C anyway in the form of CPython, but there are others. It's a language specification and it happens to allow this. – roganjosh May 07 '20 at 20:31
  • C++ and Python are completely different languages. They differ in much more than basic syntax. Don't expect two pieces of code in different languages to work similarly just because they look similar. – eesiraed May 07 '20 at 23:23

1 Answers1

1

In Python, you need to use the global keyword if you want to reassign a global variable. In C, you can just access them without needing an extra step. This version of your Python function should work correctly:

As @roganjosh commented, global variables are not recommended (in either language), but they would make the function behave as you had expected.

The reason for the difference is not because C++ is compiled and Python is interpreted. It is simply because Python requires the global keyword to reassign a global variable, and C++ doesn't.

def f(y):
    global x
    x = x + y
    return x
Daniel Giger
  • 2,023
  • 21
  • 20