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!
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.
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.
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:
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.
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.