Python Functions

If you’ve never heard of a function, this lesson may be hard to understand but the function is one of the most fundamental building blocks of any programming language. This tutorial will teach you about some of the key ideas of functions in python.

A basic function

A function has syntax similar to the if and for functions. It looks like this:

def function_name():
—-do something

This part is defining the function. Lets try a concrete example:

def hi():
—-print(“hello earthling”)

Remember to indent with the tab key. If you run this code, nothing should happen. This is because we have defined the function but not called it. When we call it, the real magic happens:


If you did everything right, this should print hello earthling. If you have a question, feel free to comment below or contact me. What we just did was define a function and call it. When a function is called, the indented code under the function is run. When calling the function we put the function name followed by brackets.

Function inputs

You may be wondering what the brackets in the function are for. They are for receiving input. Look at the following:

def hi(name):
—-print(“hello ” + name)

Now try the following:

hi(“your name here”)

It should print hello (your name). This function takes one input name. When putting the string which is your name in the brackets, you set name to your name. If we want to take multiple inputs, we can do the following:

def function(a, b):
—-do something

function(what you want a to be, what you want b to be)

Try it yourself

Try to make a function that takes two inputs name and age. If age is greater than or equal to 18 print:

Welcome (name)

Otherwise print:

You need to be 18 to use this site (name)

Where name is the variable name. Try this in python. The code should look something like this:

Try checking different ages. If your wondering, you don’t need to be 18 to use this site!


What if you want to exit a function immediately? You can use return. Try the following:

def age_checker(age):
—-if(age >= 18):
——–print(“welcome adult”)
——–print(“you must be 18+ to view this content”)
—-print(“Some content”)

Try implementing this in the age check function from earlier.

Try it yourself

Remember our first game? Try turning this into a function. When the game is done, make it so the player is asked:

Do you want to play again? y or n:

If the answer is y, run the function otherwise, return. The game should look like this.

45 thoughts on “Python Functions

  1. “The next time I read a blog, Hopefully it won’t fail me as much as this particular one. After all, Yes, it was my choice to read, but I really thought you’d have something interesting to say. All I hear is a bunch of complaining about something you could fix if you weren’t too busy seeking attention.”
    דירות דיסקרטיות בבאר שבע

  2. “The next time I read a blog, I hope that it does not fail me as much as this one. I mean, Yes, it was my choice to read, but I truly believed you would have something useful to talk about. All I hear is a bunch of crying about something you could possibly fix if you weren’t too busy looking for attention.”
    דירות דיסקרטיות במרכז

Leave a Reply

Your email address will not be published. Required fields are marked *