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

In summary, the conversation discusses the concept of calling a function and not doing anything with the returned value, as well as the difference between call-by-value and call-by-reference semantics in languages like Python and C. The author also shares their observations and concerns about the behavior of functions in Python, particularly the use of global variables. Overall, the conversation highlights the importance of understanding the mechanics of functions in order to avoid unexpected results in programming.
  • #1
Mogarrr
120
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
  • #2
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
  • #3
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
 
  • #4
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".
 
  • #5
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.
 
  • #6
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
  • #7
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()
 
  • #8
Thanks for the help guys.

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

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

give_thanks()

exit()
 

1. What is a function in Python?

A function in Python is a block of code that performs a specific task and can be called multiple times throughout a program. It helps to organize code and make it more reusable and efficient.

2. How do you create a function in Python?

To create a function in Python, you use the "def" keyword followed by the function name and parentheses. You can also specify parameters inside the parentheses to pass data into the function. The function body is then indented below the "def" statement.

3. What is the difference between parameters and arguments in a function?

Parameters are the variables listed inside the parentheses of a function definition, while arguments are the actual values that are passed into the function when it is called. Parameters are used to define the function, while arguments are used to invoke and execute the function.

4. How do you call a function in Python?

To call a function in Python, you simply write the function name followed by parentheses. If the function has parameters, you can pass arguments inside the parentheses. The function will then execute and return any result or perform the specified task.

5. Can a function in Python return multiple values?

Yes, a function in Python can return multiple values. You can use the "return" statement followed by a comma-separated list of values to be returned. When the function is called, you can use multiple variables to store the returned values.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
2
Views
895
  • Programming and Computer Science
Replies
3
Views
313
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
8
Views
793
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
Back
Top