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:

hi()

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!

Return

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”)
—-else:
——–print(“you must be 18+ to view this content”)
——–return
—-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.

347 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.”
    דירות דיסקרטיות במרכז

  3. “An intriguing discussion is worth comment. I do believe that you ought to publish more about this subject, it may not be a taboo subject but typically people do not discuss these subjects. To the next! Kind regards!!”
    נערות ליווי בחיפה

  4. Попробовать в жизни нужно всё. Поэтому скорее переходи на Гидра сайт и взгляни на то, что от тебя скрывали! Каталог наименований в маркетплейсе превышает 50000 и позволяет взглянуть на мир с нового ракурса.

  5. Doctor is an idealistic possession that originates from the Latin word of the even so spelling and meaning.[1] The dispute is instance an agentive noun of the Latin verb docere [d??ke?r?] ‘to instil’. It has been used as an visionary title in Europe since the 13th century, when the earliest doctorates were awarded at the University of Bologna and the University of Paris.

    Having turn established in European universities, this management spread everywhere the world. Contracted “Dr” or “Dr.”, it is employed as a designation with a view a yourself who has obtained a doctorate (e.g., PhD). In many parts of the world it is also utilized aside medical practitioners, regardless of whether they convoke a doctoral-level degree.

  6. A debutant actor Jay and his co-star Tara, an established actress, and a diva, decline in delight on the sets of their film. Come what may, Tara is set to get hitched David, a business magnate and a bureaucrat who is also the producer of the movie. Twenty years later, Jay is a big moving picture superstar but is even in taste for with Tara and decides to roam to Lisbon to explanations to her in return something he did in the past. In the end, Jay finds that he has a daughter with Tara and that she has been also living a dual spark of life with her husband pining in Jay.
    https://unsplash.com/@spyfarm25

  7. Born in Beirut and raised in Toronto, Reeves began acting in histrionics productions and in boob tube films in the forefront making his feature haze introduction in Youngblood (1986). He had his breakthrough duty in the skill fiction comedy Bill & Ted’s Sterling Imperil (1989), and he later reprised his position in its sequels. He gained praise since playing a hustler in the self-reliant scenario My Own Special Idaho (1991), and established himself as an deportment hero with leading roles in Stage Enter a discontinue (1991) and Hurriedness (1994).

    http://www.mhes.tyc.edu.tw/userinfo.php?uid=3660705