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

Click For Summary

Discussion Overview

The discussion revolves around the behavior of functions in Python, particularly focusing on the implications of calling a function without using its return value and the mechanics of parameter passing in relation to variable assignment. Participants explore concepts related to function outputs, side effects, and the differences between call-by-value and call-by-reference semantics.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that calling a function without using its return value results in "nothing happening," while others argue that functions can still have side effects that alter the state of the system.
  • One participant explains that the behavior observed with the makeSix function is due to Python's call-by-value semantics, which means that changes made to parameters within the function do not affect the original arguments.
  • Another participant provides multiple code snippets to illustrate different behaviors of function calls and variable assignments in Python, highlighting how local and global scopes interact.
  • There is a discussion about the distinction between functions that return values and those that do not, with emphasis on how the lack of assignment to a variable can lead to misconceptions about the function's impact.

Areas of Agreement / Disagreement

Participants express differing views on what it means for a function to "do nothing" with its result, and there is no consensus on the implications of parameter passing strategies in Python. The discussion remains unresolved regarding the expectations of function behavior in different programming contexts.

Contextual Notes

Limitations in understanding arise from the differences in parameter passing strategies across programming languages, which may lead to confusion about the effects of function calls on variable values.

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   Reactions: 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   Reactions: 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 ·
Replies
19
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 10 ·
Replies
10
Views
2K