Function basics
You’ve used functions like print
, random.randint
, and append
in your programs already, to output values, get random numbers, and add items to lists. Under the hood, those functions are just more code -- code that someone else wrote, and you can use. As you’ll see, a function is like a recipe. Python follows the steps in the recipe when you tell it to.
We’ll see two sources of functions in this lesson. The authors of Python built many useful functions into the language. They’re called built-in functions. You can see the full list here (fair warning - it's really long!). We can also build our own functions, as you will see in the next section. Functions we write ourselves are called user-defined functions.
Both kinds of functions work the same way. They execute the code for that function. The only difference is who wrote them.
Built-in functions
Built-in functions are functions that are pre-defined in Python, and always available for us to use in our programs. We've seen many built-in functions already, like print()
, str()
, input()
, and more.
One group of built-in functions are type conversion functions. They convert values from one type to another. For example, the function int()
takes a value and tries to convert it to an integer.
>>> int('32')
32
>>> int('Hello')
ValueError: invalid literal for int(): Hello
>>> int(3.99999)
3
>>> int(-2.3)
-2
Function call syntax
As you’ve seen already, you call a function by it’s name and parentheses. For some functions, you also need to put some data in between the parentheses for the function to use.
some_function(data1, data2, data3) # a function called 'some_function' that takes in three pieces of data
If you just use the function’s name, it doesn’t run the function:
input # doesn't run the function
print # doesn't run the function
int # doesn't run the function
You need parentheses, and whatever data is needed to run the function.
input()
print("Hello, world!")
int("32")
Vocabulary
You’ve already learned the word function. Let’s cover the other words for associated concepts:
- Run a function, also called executing, invoking or calling a function. Run, execute, invoke, and call all mean the same thing: tell Python to follow the instructions — do the action for the function. For
print
, actually print something. The syntax for running (calling, executing) a function is the name, plus the parentheses, soprint()
. - The data that goes in between the parentheses are called arguments. So, for a function call
print("Hello", 12345)
, you’d say that"Hello"
and12345
are the arguments to the function. As you’ll see, functions take in arguments, and use the arguments in their operation. - Functions like
int("32")
return a value. After running the function, the code “gets back” a value that it can store in a variable, or use in some other expression. - Sometimes, you’ll see functions called methods. Method is a name for a function that’s associated with some type of data. In
"Hello".upper()
, we would say the functionupper
is a method. It’s available to use with strings like"Hello"
, and it uses the.
syntax, instead of a freestanding call likeupper("Hello")
.