Why doesn't the makeSix function change the value of the variable?

AI Thread Summary
The discussion centers around the behavior of functions in Python, particularly regarding parameter passing and return values. It highlights that calling a function without using its returned value results in no change to the original variable, as Python uses call-by-value semantics. The example of the `makeSix` function illustrates that while the function attempts to modify the parameter, it does not affect the original variable unless explicitly returned and reassigned. Additionally, the thread clarifies that some functions can perform actions without returning values, which can still affect the program's state. Understanding these concepts is crucial for effective programming in Python.
Mogarrr
Messages
120
Reaction score
6
I'm reading the online tutorial "How to Think Like a Computer Scientist", which is a Python tutorial. There is an exercise, which I'm trying to figure out.

What happens if you call a function and you don't do anything with the result (i.e., you don't assign it to a variable or use it as part of a larger expression)?

My intuition is that nothing happens. To show this, I typed the following in the command line...

def nada(something):
nil = something

As predicted, when I call the function nada, nothing happens. Is this what the author meant by doing nothing with the result?

Experimenting with functions, I also discovered something that disturbs me about Python. I made a function...

def makeSix(number):
number = 6

Now I would think that the makeSix function will change the value of a variable to 6. For instance, I typed this:

x = 9
makeSix(x)
print x
9

Which is frustrating, since I would think that the value of the variable would change to 6.

What's going on?
 
Technology news on Phys.org
What they mean, I believe, is a function that actually returns a value but you don't do anything with the returned value.

Here's an example in C, which I'm more familiar with than Python.
Code:
#include <stdio.h>
int fun(int arg)
{
   return arg * 2;
}

int main(void)
{
   int val = 3;
   fun(val);  // Here I call fun() but don't do anything with the returned value
   printf("val is %d", val);
   return 0;
}
 
  • Like
Likes 1 person
Mogarrr said:
Experimenting with functions, I also discovered something that disturbs me about Python. I made a function...

def makeSix(number):
number = 6

Now I would think that the makeSix function will change the value of a variable to 6. For instance, I typed this:

x = 9
makeSix(x)
print x
9

Which is frustrating, since I would think that the value of the variable would change to 6.

What's going on?

This is a matter of the parameter passing strategy adopted by the language. It appears that you are expecting call-by-reference semantics and getting call-by-value semantics.

http://en.wikipedia.org/wiki/Evaluation_strategy
 
There are many functions that don't return any value, yet they are called to do something. If they do something, they apparently change the state of the system - so you can't say "nothing happens".
 
The first post in this thread consists of two questions:
1. What happens when you call a function that returns a value, but don't do anything with the returned value? (I.e., don't store the value or print it or whatever.)
2. If a function changes the value of a passed parameter, does this affect the value of the actual argument?

For the first question, the OP says
As predicted, when I call the function nada, nothing happens.
However, the OP's example function doesn't return a value, so I don't believe he/she is really answering the question.
jbriggs444's post is in reponse to question #2. In call-by-value semantics, which are the default in C and probably Python, a function is given essentially a copy of the parameter, so any changes that the function makes don't affect the original parameter. Other languages, such as Fortran, use call-by-reference semantics, so a function can change the value of the actual argument. C, C++, and some other languages derived from C are able to simulate call-by-reference by using pointers to the arguments.
 
I am not a Python guru, but read the following code:

Code:
# SNIPPET 1

x = 9


def make_six():
  x = 6

make_six()

# here x == 9

Code:
# SNIPPET 2

x = 9


def make_six(x):
  x = 6

make_six()

# here x == 9

Code:
# SNIPPET 3

x = 9


def make_six(x):
  x = 6

x = make_six(x)

# here x == None

Code:
# SNIPPET 4
x = 9


def make_six(x):
  x = 6
  return x

x = make_six(x)

# here x == 6

Code:
# SNIPPET 5
x = 9

# the only one that actually messes with your original variable and makes it == 6
def make_six():
  global x
  x = 6

make_six()

# here x == 6

Comments:
SNIPPET 1: make_six initializes a local variable x and sets it to 6. Returns None.

SNIPPET 2: make_six has a parameter x, it sets the local variable x (that stored the value passed as an argument) to 6 and returns None.

SNIPPET 3: we assign the return value from the function to x. As it returns None, x becomes None.

SNIPPET 4: now our function gets the value x, overwrite it with 6 and returns it. (So, with an assignment, x becomes 6).

SNIPPET 5: finally, what you wanted: it reaches another scope, grab the reference to x, shove an integer with the value 6 in it and dies. No need for an assignment or parameter.

PLEASE, do note that make_six is ridiculous as you could just assign 6 to whatever you wanted in the first place.
 
  • Like
Likes 1 person
Mogarrr said:
As predicted, when I call the function nada, nothing happens. Is this what the author meant by doing nothing with the result?

Didn't see this one. Well, a lot can happen without an assignment.

Code:
import sys


def program_killer():
   sys.exit(0)

program_killer()

x = 4

x will never become four. x will never be defined. x will never be more than an instruction not to be followed by the interpreter. Poor x.

SO NO! Something happened. Indeed, as I said, a LOT can happen.


To do nothing with the result (that in Python is, unless specified otherwise, None) means not to hold it in a variable, list, tuple, (any other container), or pass it as an argument to something.

Code:
def get_none():
  pass

# does something with the result
print(get_none())
# does something too
woah_this_will_store_none = get_none()
# THIS IS DOING NOTHING WITH THE RESULT
get_none()
 
Thanks for the help guys.

I'm curious, in a post how did you display code inside the white box?
 
Like this:
[NOPARSE]
Code:
... some code ...
[/NOPARSE]

Code:
... some code ...
 
  • #10
Code:
def give_thanks()
  print 'thanks'

give_thanks()

exit()
 

Similar threads

Replies
19
Views
2K
Replies
10
Views
3K
Replies
15
Views
2K
Replies
11
Views
1K
Replies
8
Views
1K
Replies
10
Views
2K
Back
Top