Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

Functions in Python are super useful in separating your code, but they don't stop there. Any code that you think you will ever use anywhere else, you should probably put in a function. You can also add arguments to your functions to have more flexible functions, but we will get to that in a little bit. You can define a function by using the keyword def before your function name. Let's use our first Python function.

Example def someFunction():
print("boo")
someFunction()
Result boo

It is necessary to define functions before they are called. Even though we have to skip over the function while reading to see the first statement, someFunction(). This throws us back up into the def someFunction():, again with a colon following it. Then after we acknowledge that the function is being called, we create a new line with four spaces for our simple print statement.

Functions with Arguments

Simple functions like the one above are great and can be used quite often. However, there usually comes a time where we want to have the function act on data that the user inputs. We can do this with arguments inside of the () the follows the function name.

Example def someFunction(a, b):
print(a+b)
someFunction(12,451)
Result 463

Using the statement someFunction(12,451), we pass in 12, which becomes a in our function, and 451, which becomes b. Then, we just have a little print statement that adds them and prints them out.

Function Scope

Python does support global variables without you having to explicitly express that they are global variables. It's much easier just to show rather than explain:

Example def someFunction():
a = 10
someFunction()
print (a)

This will cause an error because our variable, a, is in the local scope of someFunction. So, when we try to print a, Python will bite and say a isn't defined. Technically, it is defined, but just not in the global scope. Now, let's look at an example that works.

Example a = 10
def someFunction():
print (a)
someFunction()

In this example, we defined a in the global scope. This means that we can call it or edit it from anywhere, including inside functions. However, you cannot declare a variable inside a function, local scope, to be used outside in a global scope.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    Elements_In_Space May 13, 2017, 11:52 a.m.

    I tested the claim: "... we can call [a variable] or *edit* it from anywhere, including inside functions." with the following code: " a = 10 def aFunction(a): a += 1 print(a) aFunction(a) print(a) " This returns 11 10 Which indicates we *cannot* edit a variable inside a function.

  • User

    Ark Nov. 13, 2016, 11:09 p.m.

    Thanks a lot for tutorial!!! I guess multiline function and return statement will be discussed later

  • User

    Kallam011011 Oct. 22, 2015, 8:30 a.m.

    def someFunction(): print("boo") someFunction()

  • User

    Emmanuel Adu Gyamfi Sept. 30, 2015, 10:44 p.m.

    :D It's fun !!!

  • User

    Socktooth123 July 7, 2015, 3:05 p.m.

    Man I love these things

  • User

    deadrat June 2, 2015, 6:29 a.m.

    Can't we define a function for if? for eg: def posorneg(num): if num<0: print("-ve") elif num>0: print("+ve") else: print ("neutral")

  • User

    fanty May 26, 2015, 6:28 a.m.

    the follows the function name. THAT

  • User

    VelvetCoder March 25, 2015, 6:25 p.m.

    add an extra line after the print line, then do the next line of code and your result will show

  • User

    BRIAN March 21, 2015, 12:31 p.m.

    so far so good, l am loving it :)

  • User

    sjhuskey March 2, 2015, 7:04 p.m.

    There's a typo in the first paragraph after "Functions with Arguments." In the third sentence, the second instance of "the" should be "that": "We can do this with arguments inside of the () [the] >that< follows the function name.

  • User

    Langevin Feb. 26, 2015, 9:35 p.m.

    @Calvinhobbe: As Python uses white space indentation the most likely cause is the lack of lines or spaces.

  • User

    Calvinhobbe Feb. 20, 2015, 1:37 p.m.

    def someFunction(): print("boo") someFunction() <---- has a syntax error when I put it in and hit enter. What am I doing wrong?

  • User

    noble Feb. 10, 2015, 2:33 a.m.

    For those getting an error, remember to leave the line after defining the function empty. For example: def someFunction(a, b): print(a+b) # this line should be empty to avoid syntax error somefunction(12,451)

  • User

    fatir Feb. 1, 2015, 7:19 p.m.

    if you had an experience in c or c++ it's very easy to learn.

  • User

    KingFreeFall Jan. 20, 2015, 3:53 a.m.

    a=3 def function(): print(a+2) print(a) function() print(a)

  • User

    Josh_J08 Dec. 27, 2014, 12:18 p.m.

    @10032 typo ... line 4

  • User

    Barak Dec. 8, 2014, 1:43 p.m.

    the best way to see the "Function Scope" is to try this: a=3 def function(): print(a+2) print(a) function() print(a)

  • User

    An0nymous Dec. 4, 2014, 7:55 a.m.

    @10032 This worked on mine :/ idk

  • User

    10032 Dec. 4, 2014, 6:07 a.m.

    NameError: name 'someFuntion' is not defined on line 4 I keep getting this anyone know why ? What I inputted j = 56 def someFunction(): print (j) someFuntion()

  • User

    Eastman Dec. 1, 2014, 9:21 p.m.

    so far so good

  • User

    Neo Nov. 26, 2014, 3:53 p.m.

    I did something like a converter from mb to kb Its just great to program in python

  • User

    Kazaminese Nov. 23, 2014, 5:20 a.m.

    >>> def someFunction(): print("boo") someFunction() I couldn't get this to work on IDLE

  • User

    Tesla Panda Nov. 7, 2014, 5:08 p.m.

    Random Level up names ahha

  • User

    Sderdau Oct. 18, 2014, 3:21 p.m.

    >>> def someFunction(a,b): print(a+b) >>> someFunction(12,451) Need to learn my spacing got it to work by putting in a couple of returns etc.

  • User

    Sderdau Oct. 18, 2014, 3:11 p.m.

    Python 3.2.3 (default, Feb 27 2014, 21:33:50) [GCC 4.6.3] on linux2 Type "copyright", "credits" or "license()" for more information. ==== No Subprocess ==== >>> a=10 >>> def someFunction(): print(a) someFunction() SyntaxError: unindent does not match any outer indentation level >>>

  • User

    Sderdau Oct. 18, 2014, 3:11 p.m.

    Python 3.2.3 (default, Feb 27 2014, 21:33:50) [GCC 4.6.3] on linux2 Type "copyright", "credits" or "license()" for more information. ==== No Subprocess ==== >>> a=10 >>> def someFunction(): print(a) someFunction() SyntaxError: unindent does not match any outer indentation level >>>

  • User

    Jacobzone Sept. 10, 2014, 6:47 p.m.

    This is really helpfull tnx!

  • User

    EpicYoloTime Aug. 29, 2014, 4:17 p.m.

    (&acirc;•&macr;&Acirc;&deg;&acirc;–&iexcl;&Acirc;&deg;&iuml;&frac14;‰&acirc;•&macr;&iuml;&cedil;&micro; &acirc;”&raquo;&acirc;”&acirc;”&raquo; FLIP THIS TABLE! &acirc;”&raquo;&acirc;”&acirc;”&raquo; &iuml;&cedil;&micro; &atilde;ƒ&frac12;(&Acirc;&deg;&acirc;–&iexcl;&Acirc;&deg;&atilde;ƒ&frac12;) FLIP THAT TABLE! &acirc;”&raquo;&acirc;”&acirc;”&raquo; &iuml;&cedil;&micro; &iuml;&frac14;&frac14;( &Acirc;&deg;&acirc;–&iexcl;&Acirc;&deg; )&iuml;&frac14; &iuml;&cedil;&micro; &acirc;”&raquo;&acirc;”&acirc;”&raquo; FLIP ALL THE TABLES NAO! &agrave;&sup2;&nbsp;_&agrave;&sup2;&nbsp; Put. &agrave;&sup2;&nbsp;__&agrave;&sup2;&nbsp; The&iuml;&raquo;&iquest; tables. &agrave;&sup2;&nbsp;___&agrave;&sup2;&nbsp; Back... (&acirc;•&reg;&Acirc;&deg;-&Acirc;&deg;)&acirc;•&reg;&acirc;”&sup3;&acirc;”&acirc;”&sup3; &acirc;”&sup3;&acirc;”&acirc;”&sup3; &acirc;”&sup3;&acirc;”&acirc;”&sup3;

  • User

    EpicYoloTime July 2, 2014, 7:14 p.m.

    im just here... trying to get my commenter badge...

  • User

    EpicYoloTime July 2, 2014, 7:14 p.m.

    im just here... trying to get my commenter badge...

  • User

    EpicYoloTime July 2, 2014, 7:11 p.m.

    im just here... trying to get my commenter badge...

  • User

    EpicYoloTime July 2, 2014, 7:06 p.m.

    mwa lub this. this tutorial is helping me alot :) (im a learner)

  • User

    Clau Lacatus June 28, 2014, 3:29 a.m.

    If there we can find some other complicated examples it`d be great! Thanks, since now, so far, so good! Keep up the good work!

  • User

    Jake Stephens June 23, 2014, 7:12 p.m.

    so... a = 69 def getitonwiththewife(): print (a) getitonwiththewife() ------------------------------------------ 69

  • User

    pacoalp May 4, 2014, 10:11 p.m.

    RAGhav, you must type: def er(a,b): print (a + b) er(10,12) or a = 10 b = 12 def er(): print (a + b) er() the problem are the variables, &quot;However, you cannot declare a variable inside a function, local scope, to be used outside in a global scope&quot;

  • User

    Jamie April 23, 2014, 9:37 p.m.

    &quot;However, you cannot declare a variable inside a function, local scope, to be used outside in a global scope.&quot; Why is this so?

  • User

    MaximusBo April 23, 2014, 8:01 p.m.

    Because i just got one... Lol

  • User

    MaximusBo April 23, 2014, 8:01 p.m.

    and, how the heck do you get xp to level up???

  • User

    MaximusBo April 23, 2014, 7:58 p.m.

    are you putting the two parentheses?

  • User

    Andy M April 3, 2014, 8:33 p.m.

    I really like the order that the lesson come in (at least, while perusing the Python section, since that is the only one I have tried so far). One possible error. On the &quot;Python Functions&quot; page, it says, &quot;Python does support global variables.&quot; I think that you meant that it does *not* support...

  • User

    rousbel_villar March 2, 2014, 10:04 p.m.

    this was a very good lesson ,very exited to start programming.

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 1:44 p.m.

    One another step in the syntax of the function statement and it's errors sources.

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 1:32 p.m.

    It's easy to define the function statement in Python. Many thanks for the syntax explanation

  • User

    Bryan Feb. 13, 2014, 1:40 a.m.

    def addFunction(): a = 23 # print (a) a+= 22 print (a) addFunction()

  • User

    Madinawi Feb. 10, 2014, 5:09 p.m.

    a = (23) b = (65) def hFunction(): print (a + b) hFunction()

  • User

    Aqdas Jan. 3, 2014, 5:53 a.m.

    How can I del my comment which is by mistake posted twice :( Suggetion - There should a cancel or delete button for removing comment..

  • User

    Aqdas Jan. 3, 2014, 5:50 a.m.

    a = &quot;Aqdas&quot; def someFunction(): print (a) someFunction() Love It to print my name again and again :)

  • User

    Pyn00b Dec. 24, 2013, 10:13 a.m.

    same here works on tester but not on python3.3.3....this sections need a clearer example!!



Advertisement