Functions

Functions are like special shortcuts that help people who make computer games or programs. They have a name and when that name is used, it does a certain job or action. Some functions come with the computer language called Python, but you can also create your own shortcuts for your games and programs. This way, you don't have to write the same thing over and over again - you just use the shortcut's name!


How to Use Functions

When you want to use a function, you “call” it by typing the name of the function, followed by parentheses, which may contain a parameter. A parameter is data for a function to use.


When you want to use one of Python’s built-in functions, all you need to do is call it by typing out its name followed by a pair of empty parentheses. This tells Python to run the code saved in that function.


Built-in Functions

Think of Python as a special helper who can do different tasks for you quickly, just like when you use shortcuts to get somewhere faster. Python has many built-in functions, or helpers, that can do a bunch of cool things for you. These functions make it super easy to do these tasks without having to figure everything out on your own!

In the below example, Python helps us display a message through the print() function. In this case, print is the name of the function and the String "Hi there!" is the parameter.

Create a Repl called functions, write the above code, and run it.

Python can help us do other things like let the user enter information. This happens when we call the input() function. You can use input() to make the game ask the player what their name is. Their answer becomes a return value, which the function will then assign to the name variable. The code below does exactly that. Write the below code and run it.

Member Functions

Imagine the words in a sentence. Verbs are the words that show action, like "run" or "eat". Now, think of pieces of data like numbers and strings as the "nouns" in our coding sentence. These nouns sometimes have their very own action words, or "functions," that can help them do things or be changed. We call these special functions "member" functions.



When we want to use these member functions, we write the noun (the piece of data), then a little dot (like a period in a sentence), and finally the action word (the function), along with a pair of empty parentheses. In the above example, we have a list (our noun) called "todo" that represents a list of chores to complete. And there's a member function called clear that empties the list. To use this member function, we would write todo.clear(). Write the above code and run it.

Below are other member functions:

Creating your own functions

There isn’t a built-in function for everything, so you need to know how to write, or “define,” your own. A function should have one clear purpose and a name that describes what it does. Follow these steps to create a function that calculates a player’s score.

Define The Function

Create a function called fruit_score write the below code and then run it.

Add Some Parameters

The function works well so far, but what if you want to have different scores for different fruits you collect? For the function to know which score to print, it needs to know which fruit you have collected. Imagine you get ten points for an apple, but five points for an orange. You can do this by adding a parameter to the function. Change your code to look like below. Notice any tools we've used before?

Return A Value

Rather than printing out the score, you might want to use it elsewhere in your code. You can ask to get a value out of a function to be used later. This is called “returning” a value. Type in the keyword return before the value you want it to return in each case. Try switching your print statements to return statements. Change your code to look like below.

Using The Return Value

You can use the return value of a function elsewhere in your code. In this case, we make two calls to the function—one for each fruit. We then add these results together to get a total score. Add the below code underneath what you've written. What's the output?

Activity

Write a function to add two numbers. Imagine your teacher asked you to solve a simple addition problem, and you decide to write a Python function to do the work! Create a function called add_numbers that takes two numbers as input and returns their sum.

Copyright © KX Technology Group, LLC. All Rights Reserved.