Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Tutorial: functions

  1. Aug 23, 2014 #1
    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?
     
  2. jcsd
  3. Aug 23, 2014 #2

    Mark44

    Staff: Mentor

    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 (Text):

    #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;
    }
     
     
  4. Aug 23, 2014 #3

    jbriggs444

    User Avatar
    Science Advisor

    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
     
  5. Aug 23, 2014 #4

    Borek

    User Avatar

    Staff: Mentor

    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".
     
  6. Aug 23, 2014 #5

    Mark44

    Staff: Mentor

    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
    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.
     
  7. Aug 23, 2014 #6
    I am not a Python guru, but read the following code:

    Code (Text):

    # SNIPPET 1

    x = 9


    def make_six():
      x = 6

    make_six()

    # here x == 9
     
    Code (Text):

    # SNIPPET 2

    x = 9


    def make_six(x):
      x = 6

    make_six()

    # here x == 9
     
    Code (Text):

    # SNIPPET 3

    x = 9


    def make_six(x):
      x = 6

    x = make_six(x)

    # here x == None
     
    Code (Text):

    # SNIPPET 4
    x = 9


    def make_six(x):
      x = 6
      return x

    x = make_six(x)

    # here x == 6
     
    Code (Text):

    # 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.
     
  8. Aug 23, 2014 #7
    Didn't see this one. Well, a lot can happen without an assignment.

    Code (Text):

    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 (Text):


    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()
     
     
  9. Aug 23, 2014 #8
    Thanks for the help guys.

    I'm curious, in a post how did you display code inside the white box?
     
  10. Aug 23, 2014 #9

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Like this:
    [NOPARSE]
    Code (Text):

    ... some code ...
     
    [/NOPARSE]

    Code (Text):

    ... some code ...
     
     
  11. Aug 23, 2014 #10
    Code (Text):
    def give_thanks()
      print 'thanks'

    give_thanks()

    exit()
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python Tutorial: functions
  1. Python Tutorial (Replies: 8)

  2. A good python tutorial (Replies: 3)

Loading...