< eelmine | Table of Contents | next > |
4.1 WHAT ARE FUNCTIONS?
A functions are designed to perform a specific task, which can be executed whenever needed by calling the function's name. Functions take in data, process it, and return a result. They can perform calculations, manipulate data, or automate repetitive task. Functions also make the code easier to read.
CREATING A FUNCTION
The interpreter recognizes a function by the def
keyword. This is followed by the function name, the parameters the function takes, and a semicolon.
Image. Syntax of Python Function Declaration. geeksforgeeks.org
In the function body, you will define what you want the function to accomplish. Finally, you can return a value that can be passed on to the rest of the program for further operations.
Below we can see an example of a function that raises the input to the power of two. We then call the function with the input of 5 within the print statement.
PARAMETERS AND ARUGMENTS
Parameters are the variables listed inside the parentheses in the function definition. They act as placeholders for the values a function can accept when called.
Arguments are the actual values or data you pass into the function when you call it. These values are substituted into the function in place of the parameters, allowing the function to execute with concrete inputs.
# Function that calculates the square of the provided number def square_of(number): return number ** 2 five_squared = square_of(5)
Here, the number
is the sole parameter of the function square_of
. It’s a placeholder that indicates the type of value we expect to receive to then conduct operations on within the function body.
We then call the function square_of(5)
and assing it to the variable five_squared
. During the function call we pass in the value 5
which is the argument.
RETURN STATEMENT
The return
statement is used in a function to exit it and pass back a value to the caller. When a function reaches a return
statement, the function terminates immediately, and the expression specified with return
is evaluated and returned to the caller. The caller refers to the part of the program that invokes or calls the function.
In the example below, the caller is an assignment statement where square_of(5)
is invoked.
# Function that calculates the square of the provided number def square_of(number): return number ** 2 five_squared = square_of(5)
It's important to remember that the return
statement effectively allows you to take the output of a function and use it in subsequent calculations or actions. Without return
, there would be no way to conduct sequential operations based on the outcomes of previous operations.
Below you can see an example in Thonny of how we can combine multiple functions and pass one functions output as the input of the other function for further operations.
VOID FUNCTIONS
Void functions, on the other hand, do not return a value. These functions are often used for their side effects, such as printing to the console.
def print_greeting(name): print("Hello, " + name + "!") # Prints a greeting to the console
FUNCTION NAMING CONVENTIONS
Based on Python PEP 8 guidelines, function names should be lowercase, and an underscore should separate words, such as print_greeting
. This is known as the Snake case naming convention, as discussed in the Expressions section.
If the prevailing style in a codebase is in another case, for example, Camel case, then you should follow the style of that particular codebase. However, in the context of this course, feel free to use the naming convention that suits you the most.
The important aspect to remember is variable and function naming consistency within your codebase. This makes the codebase easier to read and maintain.
KEY THINGS TO REMEMBER
- Functions are defined using the
def
keyword, followed by the function name and parameters enclosed in parentheses, ending with a colon. - The body of a function contains the code that executes a specific task and mostly concludes with a return statement
- Parameters are the variables listed in the function definition that act as placeholders for the values that the function can accept
- Arguments are the actual values passed into the function when called, replacing the placeholders.
- Return statement is used to exit a function and pass back a value to the caller. This enables the function's output to be used in other parts of the program.
SELF-CONTROL EXERCISES
< eelmine | Table of Contents | next > |