Functions
Introduction to Functions
A function is a group of statements that exist that we can use in a program for the purpose of performing a specific task. We call this modular programming. Dividing up code into sections that can be executed as needed. We can also "store" these sections of code to be reused in other programs.
Benefits of using functions:
- Simpler code
- Code reuse - write once and call it multiple times
- Better testing and debugging - can test and debug each function individually
- Faster development
- Easier facilitation of teamwork - different team members can write different functions
Void Functions and Value Return Functions
A void function simply executes the statements is contains and then terminates. A value-returning function executes the statement that it contains, then returns a value back to the statement of the original program that "called" the function. The input() function that we used in previous modules is a value-returning function. It gets the data that the user types on the keyboard and returns that data as a string to the program.
The code for a function is known as a function definition. To execute the function, you write a statement that calls it.
The input() and print() functions that we used in previous modules are pre-defined in the Python language and we call them built-in functions.
In addition to using the Python built-in functions, we can also create our own functions:
- you can't use one of Python's keywords
- a function name cannot contain spaces
- the first letter must be A-Z, a-z or underscore
- after the first character, you must use A-Z, a-z, underscore, 0-9
- uppercase and lowercase are different
Defining a Function and Calling It
To define a function, we will use the def keyword. The SYNTAX is:
def function_name():
statement
statement
etc.
The first line is the function header. The "statements" are the block of code for that function. You MUST indent the statements in the block in PYTHON.
When we want to "call" a function - have it executed from our program, we write:
function_name()
Passing Arguments to Functions
An argument is any piece of data that is passed into a function when that function is called (remember to call a function means we want to have that particular function executed). A parameter is a variable that receives an argument that is passed into the function.
The main program has the ARGUMENT (local variable name with data) and the function has the PARAMETER (local variable name) where the data will be sent (it's like an assignment statement). Once the function has the data inside of the parameter (local variable), it can use it in the function.
You CAN pass multiple arguments (more than one piece of data) to a function to be used. Both the main program (calling program) and the function (called program) MUST have the same number and data types of the arguments and parameters being used.
Storing User Defined Functions in a Module
As programs get larger and larger, it might be wise to actually store several functions together into a Module and then use the module.function to use what you need in your program. This is called modular programming. We will learn more about the modules later in the course.
Exercises
The examples below are written in "script" mode.
Example: functions.py
# ----- void function without arguments -----
def greeting():
print("-----------------------------")
print(" Hello World ")
print("-----------------------------")
# ----- calling the greeting() function -----
greeting()
# ----- void function with arguments -----
def sum_two_numbers(num1, num2):
total = num1 + num2
print("{} + {} = {}".format(num1, num2, total))
# ----- calling the sum_two_numbers() function -----
sum_two_numbers(3, 4)
# ----- function with an argument and return value -----
def num_square(num):
return num * num
my_num = 3
# ----- calling the num_square() function -----
print(num_square(2))
print(num_square(my_num))
- The def keyword is used to define functions
- Functions have to be defined before use
- A common syntax error is leaving out : at end of def statement
- Block of code for functions, control structures, etc are distinguished by indented code: 4-space indentation is recommended
- The default return value is None
Results:
-----------------------------
Hello World
-----------------------------
3 + 4 = 7
4
9