A function in Python is a block of code packaged together to perform a single related action.
The name ‘function’ comes from the fact that a block of code is trying to achieve a single functionality in the program. The main advantage of writing and using functions in Python is that it allows programmers to break their tasks into a group of reusable code.
There are two kinds of functions in Python: built-in functions and user-defined functions.
Built-in functions in Python
Built-in functions in Python are functions that come pre-written when you install Python and are readily available to use when coding.
You’ve already come across some built-in functions like
type() earlier on in this course. Here is a list of some commonly used built-in functions in Python.
|Prints the specified message
|Returns the class type
|Calculates the length of a string
|Returns the absolute value of a number
|Converts a value to a formatted representation
User-defined functions in Python
User-defined functions in Python are functions that are defined by the user to perform a custom task. The general syntax for writing a user-defined function in Python is as follows:
def function_name(parameters): """ docstring """ statement(s) return [expression]
Let us go over all of the lines mentioned in the above syntax and understand clearly about what they mean:
- Functions in Python are created using the
defkeyword alongside a
function_namewhich marks the name of the function.
function_nameis followed by a set of parenthesis inside which we can pass various parameters. These parameters are the input to the function and are optional while creating a function that doesn’t need any input.
- A colon (:) marks the end of the function header and any line of code that is part of the function is written with an indentation.
docstringis a multi-line comment that describes what the function does. It is optional but writing docstrings helps in setting proper documentation for user-defined functions.
- The actual lines of code are denoted by
statement(s)and they build up the functionality of the function.
returnkeyword is used to return back any output when the function is used.
Once a function is written, it is very easy to use it. The syntax for using a function or ‘calling’ the function is as follows:
As you can see, we can simply write the function name followed by a set of parenthesis to call a function. Any input to be supplied to the function is passed in the form of parameters which is written in between the set of parenthesis.
Example of a user-defined function in Python
Now, that we know the general syntax of defining and calling a function, let us learn more about it using a concrete example.
The function below calculates the sum of two numbers and returns back the output.
def sum_func(a, b): """ This function takes in two parameters and returns their sum. """ total = a + b return total
Now, to call this function, we can simply call the function and pass two values we would like to add through it.
output = sum_func(4, 6) print(output)
The following things are happening when the
sum_func is called:
- The value of
avariable is set as 4 and the value of
bvariable is set as 6.
- The total variable is calculated as 4 + 6 = 10.
- The total variable is returned back from the function and the output variable gets assigned with the value of the total variable, i.e. 10.
Local and Global Variables
Whenever we create a variable inside of a function, the scope of the variable gets limited to that function. This means that the variable can only be used within the function it is defined at. Such kinds of variables are known as local variables.
In the above
sum_func function, the variable
b can only be used within the function itself. If we try to use these variables outside of the function, we will receive an error stating that the variables are not defined. This is because the scope of the two variables is limited to the
On the other hand, if we declare a variable outside of a function, the scope of the variable is the entire program. This means that we can use it inside of user-defined functions as well. However, we cannot change the value of such variable from inside of a function.
To change a variable declared outside of a function from within a function, we must use the
global keyword when declaring the variable. Such kinds of variables are known as global variables. Here is an example of a global variable:
global a = 4 def sum_func(b): """ This function takes in two parameters and returns their sum. """ total = a + b return total output = sum_func(6) print(output)
That is it for this lesson on Python functions.