Functions
Functions are code blocks that can be called from other parts of your program. You might want to use a function if you are going to use the same code multiple times or if you want to break down a large program into smaller parts.
We have already seen some functions in the previous chapters, like the built-in print
function.
Creating Functions
The def
keyword is used to specify a function followed by the name. Any arguments that the function takes are defined inside parentheses.
When the function is called, the code block is executed much like an if
statement.
Arguments
Arguments can be passed to a function by specifying them in parentheses. The arguments are variables that can be used inside the function.
Positional Arguments
Positional arguments are arguments that are passed to the function in the same order as they are defined. Each argument is separated by a comma. You can have any number of positional arguments.
Exercise
Write a function named add
that takes two arguments and prints their sum.
Keyword Arguments
Keyword arguments are arguments that are passed to the function with a keyword. You can even specify positional arguments using keywords.
However, keywords are mostly used for optional arguments that have default values. You can also use keywords to specify arguments in a different order than they are defined.
If you want to only allow positional or keyword arguments you can use the /
and *
symbols to specify where the positional and keyword arguments start.
The print
function is also a good example of a function that takes arguments.
The first two arguments are positional arguments and the last two are keyword arguments. The print
function can take any number of positional arguments, but there are only a few keyword arguments. Here is what the help
function says about the print
function.
Keyword arguments can have default values so you don't have to specify them every time you call the function. For example, the print
function has a default value for the end
keyword argument which will add a newline character at the end, but you can override it by passing a different value.
Exercise
Modify the code below to print 0-9 on a single line.
Return Values
Functions can produce a value by using the return
keyword. This is useful for creating a reusable expression that can be used in other parts of your program.
Exercise
Write a function named add
that takes two arguments and returns their sum.
Scope
Scope refers to where a variable can be accessed. There are two types of scope: global and local.
Global scope
A variable created outside of a function has a global scope and can be accessed inside or outside of a function.
Local scope
A variable created inside a function is only available inside that function. This is called the scope of the variable. I variables with the same name can exist inside and outside of a function.
Global keyword
If you want to modify a global variable inside a function, you can use the global
keyword.
Exercise
Given a number x
, do the following:
- Create a function called
add_one
that increases the value ofx
by 1. - Create a function called
add_two
that takesx
as an argument and returns the value ofx
plus 2.