Today, we will learn how to write your own custom functions in Python. Once you master writing functions, you will be ready to build full applications using Python.
So far, we have used built-in functions, such as
Now, we will learn how to write a custom function on our own. A function allows us to perform a specific task without worrying about the implementation details.
Let's start with an illustrative example.
Under the hood, the
max function iterates over all the elements using a
for loop and compares each element with all other elements to find the maximum value. Thanks to the max function, we can do this without worrying so much about the implementation detail. This is the idea of encapsulation.
A few things to take note of:
- the rules for function names are the same as for variable names (e.g. you can't have spaces in the name but rather separate two words with an underscore).
- the argument and variables defined in the function can only be used within the function (i.e. local variables).
- a function can return no value (i.e. None). Example is the
Which are valid ways to begin function definition? Choose two correct answers.
def my_function(arg1, arg2):
def my function(arg1):
def my_function(arg1 arg2):
def my_function(arg1, arg2, arg3):
More on Return Values
- you can return multiple values from a function
- instead of returning a value, some functions may just print text to the console
You can specify default values for arguments. The default value is used for an argument if a user-defined value is not specified upon the function call.
- Calculate population density
- Convert an integer number of days to weeks
Given the number of population of a city (e.g. Medan) and its area in $km^2$, write a function to compute the corresponding population density. Hint: population density is given by
density = population/area
- name the function
- the function should take two arguments:
Write a function that takes an integer number of days and returns a string with the number of weeks and days that is. For example, 8 days are equal to 1 week and 1 day.
Hint: use integer division and the modulo operator
- name the function
- the function should take exactly one argument (number of days)
- assignment statement
- for loop
- method call
- while loop
A variable that is defined inside a function can only be used within that function. The variable cannot be accessed from outside the function
We say that
some_variable is a local variable, i.e. the scope of
some_variable is local to
my_function. On the other hand, a variable that is defined outside of a function is said to have a global scope.
When you program, you'll often find that similar ideas come up again and again. You'll use variables for things like counting, iterating and accumulating values to return. In order to write readable code, you'll find yourself wanting to use similar names for similar ideas. As soon as you put multiple piece of code together (for instance, multiple functions or function calls in a single script) you might find that you want to use the same name for two separate concepts.
Fortunately, you don't need to come up with new names endlessly. Reusing names for objects is OK as long as you keep them in separate scope.
Good practice: It is best to define variables in the smallest scope they will be needed in. While functions can refer to variables defined in a larger scope, this is very rarely a good idea since you may not know what variables you have defined if your program has a lot of variables.
- The call to this function causes an UnboundLocalErro because the variable balance has a global scope
- we can access the value of a global variable in a function but we cannot change or modify it
If you want to change the global variable
balance in a function, pass it as an argument.