logo

Functions

A function is reusable code that can be called anywhere in your program. Functions improve readability of your code: it’s easier for someone to understand code using functions instead of long lists of instructions.

On top of that, functions can be reused or modified which also improve testability and extensibility.
Related Course:
Complete Python Bootcamp: Go from zero to hero in Python 3

Function definition


We use this syntax to define as function:

def function(parameters):
instructions
return value

The def keyword tells Python we have a piece of reusable code (A function). A program can have many functions.

Practical Example


We can call the function using function(parameters).

#!/usr/bin/python

def f(x):
return(x*x)

print(f(3))

Output:

9

The function has one parameter, x. The return value is the value the function returns. Not all functions have to return something.

Parameters


 We can pass multiple variables:

#!/usr/bin/python

def f(x,y):
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))

f(3,2)

Output:


You called f(x,y) with the value x = 3 and y = 2
x * y = 6

Download Python Exercises

BackNext

24 thoughts on “Functions


  1. Roger's Mod
    - August 10, 2015

    Oh, didn’t noticed that it’s also explained here, really sorry.

    1. Staff
      - August 10, 2015

      Hi Roger, no problem

  2. Anuj
    - July 15, 2015

    Hey frank .. What does +str (x) …+Str(y) means ???

    1. Frank
      - July 16, 2015

      Hi, this converts integers/floats to strings. Integers or floats are simply bytes in the memory and you cannot output them directly to the screen. str() converts them to human readable text.

  3. Nub
    - July 12, 2015

    Hey Frank, thank you very much for providing this tutorial set. it is the best one i have come across. i have no programming experience besides playing around in HS, and the KISS method you use made this very easy

  4. Stuartjk
    - May 11, 2015

    Also, in this example

    #!/usr/bin/python
     def doA():
    a = 5
     def doB(a):
    print a # we pass variable as parameter, this will work
     doB(3)

    It is important to note that doB will print 3 which is the value passed to it, a=5 is contained within Doa and is still never used and therefore never visible to doB or the rest of the code.

    1. Frank
      - May 11, 2015

      Thanks stuartjk, I extended the article 🙂

  5. Stuartjk
    - May 11, 2015

    Not sure how this one got missed as Tony obviously time looking at this one, but in all examples, the end of the first print line is incomplete.

    print ‘You called f(x,y) with the value x = ‘ + str(x) + ‘ and y = ‘ + str($

    will never work as you fail to correctly wrap the last str parameter ($), which itself is invalid.

    should it not read

    print ‘You called f(x,y) with the value x = ‘ + str(x) + ‘ and y = ‘ + str(y)

    1. Frank
      - May 11, 2015

      Thanks stuartjk! It seemed to disturb nobody thus far, but you are right.
      The error came from copying source code from a terminal which capped the line. I fixed it and I’ll copy from an IDE in the future. I hope you enjoy my articles, more will come soon.

  6. Tony
    - May 3, 2015

    THe concept of scope isn’t really all that clear in your example. The first code snippet will actually work, because the function isn’t actually doing anything with z.

    Perhaps:

    #!/usr/bin/python
      def f(x,y):
    print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
    print 'x * y = ' + str(x*y)
    print str(z) # cannot reach z, so THIS WON'T WORK
     z = 3
    f(3,2)

    Perhaps other examples of code that does work as well:

    #!/usr/bin/python
     def f(x,y,a):
    print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
    print 'x * y = ' + str(x*y)
    print str(a) # can reach z, because it was passed to function f as parameter "a"
     z = 3
    f(3,2,z)

    #!/usr/bin/python
     def f(x,y):
    z = 3
    print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
    print 'x * y = ' + str(x*y)
    print str(z) # z is local to function f, so it is reachable!
     f(3,2)

    … and an example of the confusion that can arise from reusing variable names:

    #!/usr/bin/python
     z = 5
    print str(z)
     def f(x,y,z):
    print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
    print 'x * y = ' + str(x*y)
    print str(z) # can reach z, because it was passed to function f as parameter "z"
     z = 3
    f(3,2,z)

    1. Frank
      - May 3, 2015

      Hi Tony,

      I extended this part to contain more code examples. It’s rather late here so hope everything went fine. Thanks for the heads up!

    2. Sh
      - July 21, 2015

      I agree with Tony.
      Right now the article states:

      #!/usr/bin/python
       def f(x,y):
      print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
      print 'x * y = ' + str(x*y)
      print str(z) # cannot reach z, so THIS WON'T WORK
       z = 3
      f(3,2)

      but I do see 3 get printed when I run the code.

      1. Frank
        - July 21, 2015

        Hi, thanks for your comment! You can print it but not modify it. Try z = 4 inside the function and it will give you an error. I will update the article.

Leave a Reply

Login disabled