Python Concepts/Functions

Objective

 * Discuss the purpose of functions.
 * Learn how to create a simple function.
 * Understand the use of parameters.
 * Work with "default parameter values".
 * Work with positional arguments.
 * Work with keyword arguments.
 * Use the return statement within a function.
 * Understand the location and abstraction of a function.

Functions
A function is a way of reusing a block of code so that it can be used more than once. Functions give the programmer the ability to reduce code redundancy and eliminate hours of wasted work. Each function allows the code to be centralized, where one change to the function will affect everything that depends on it. Functions can be used to divide and conquer tasks that would seem otherwise impossible to accomplish!

Creating A Function
To create a function, use the  keyword, followed by the function's name, with an empty pair of parentheses, ending with a colon. Functions make up a code block, so you'll need to indent any code that's within the function. Don't forget to indent, otherwise you'll get the pesky "IndentationError".

The code above assigns and creates the function called. It acts much like the the command ; both the given examples create a new variable,   and   respectively.

Now that you've created the function nothing should visibly happen. To actually invoke or call the function, you'll need to use the function's name followed by the parentheses as shown in the script below.

The output should be:

Hello, world!

Adding the command  again will result in the words   being displayed twice. This demonstrates a small fraction of the function's uses and abilities.

Function Parameters
At this point, you might be wondering why we need to have those pointless parentheses when we call the function. Wouldn't  be a lot easier than. Believe it or not, but we can pass arguments to the function when we include them within the parentheses. This allows us to create functions that do things based on their given arguments. First, the arguments need to be declared when the function is first defined and then we need to call it with the correct number of arguments.

The following output should be: 4 9 16

now acts like a variable within the function. You should note that x will only exist within the function. Trying something like the example below will definitely cause a.

A function can take multiple arguments with any two adjacent arguments separated by a comma if more than one exists as demonstrated in the next example.

The following output should be: 7 11

Default Parameters
Having parameters is great, but it can be repetitive to continue to use them over and over, again. We can define a default value for a parameter by setting its value when you define the parameter. Take the script below for example.

The output should be: 1 0 23

Did you see the second time we called ? We didn't give an argument, yet it still printed out. If no argument is given, a default is automatically assigned.

Positional Arguments
A positional argument is an argument that is not a keyword argument. In an argument list it occupies a position that corresponds to a parameter in the function definition. In all four function calls above numbers  are positional arguments. Number  occupies the position that corresponds to arg1 in the function definition; number   occupies the position that corresponds to arg2 in the function definition; number   occupies the position that corresponds to arg3 in the function definition. All four of the above function calls produce the same output: The following syntax accepts a variable number of arguments: Python's syntax accepts required and optional arguments and a variable number of arguments: Parameters are part of the function definition. When the function is invoked, arguments are passed to the function and each argument passed must correspond to a parameter in the definition of the function.

Keyword Arguments
Keyword arguments are arguments that are supplied to a dictionary within the function. They may be supplied as the sole list of arguments or they may follow other arguments: or We saw keyword arguments used in Dictionaries:Other operations on dictionaries This lesson shows by examples how they may be passed and received. The result is:

The following example shows how (optional) positional arguments and (optional) keyword arguments may be passed to a given function upon invocation.

Returning A Value
If we wanted to use the value that the square function gives us, we would rewrite it to use the  command. Which can then be used to do such fun things as: or or even in statements such as: and If the function is to return a value, the  statement is required. Without it the function returns

Assignments



 * Previous Lesson:Directories
 * This Lesson:Functions
 * Next Lesson:Variable Scoping
 * Course Home Page