Learn Python: Functions

Learn about code reuse with Python functions. Apply that knowledge to create functions for famous physics formulas.

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

Python Functions

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

In Python, some tasks need to be done multiple times within a program. Rather than rewrite code, a function can be defined using the keyword def with parameters, which are inputs into the function. The function can return some value as an output with the keyword return and be implemented throughout the code. In the example, a function my_function is defined with the parameter x. The function returns a value that takes the parameter and adds 1 to it. It is then implemented multiple times with different input values.

Calling Functions

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Python uses simple syntax to use or call a preexisting function. A function can be called by writing the name of it, followed by parentheses. For example, the code above would call the doHomework() method.

Defining Functions

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

A developer can create or define his or her own function in Python. To do so, the keyword def is followed by the name of the function, parentheses, and a colon. The body of the function, or the code for what the function will actually do, comes after the colon on an indented line.

Function Indentation

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Python uses indentation to identify blocks of code. Code within the same block should be indented at the same level. A Python function is one type of code block. All code under a function declaration should be indented to identify it as part of the function. There can be additional indentation within a function to handle other statements such as for and if so long as the lines are not indented less than the first line of the function code.

Function Parameters

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Sometimes functions need inputs to fill in the blanks within their code. Python functions have parameters that do just that. Parameters are variables that are first defined in the function definition and hold values which are necessary for a function's operations. For example, the code above uses a character, setting, and skill as inputs to write the first sentence of a book.

Multiple Parameters

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Python functions can have multiple parameters. Just like how you wouldn’t go to school without both a backpack and a pencil case, functions may also need more than one input to carry out their operations. To define functions with multiple parameters, the necessary inputs are placed in a comma-separated list within the parentheses of the function definition.

Function Arguments

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Parameters in python are variables— placeholders for the actual values the function needs. When the function is called, these values are passed in as arguments. For example, the arguments passed into the function .sales() are the Farmer’s Market, toothpaste, and $1 which correspond to the parameters grocery_store, item_on_sale, and cost.

Function Keyword Arguments

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Python functions can be defined with named arguments which may have default values provided. The arguments of a Python function are available inside the function as variables. They are also available when the function is called elsewhere in a program. When function arguments are passed using their names, they are referred to as keyword arguments. The use of keyword arguments when calling a function allows the arguments to be passed in any order - NOT just the order that they were defined in the function.

Returning value from Python function

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

A return keyword is used to return a value from a Python function. The value returned from a function can be assigned to a variable which can then be used in the program. In the example, the function check_leap_year returns a string which indicates if the passed parameter is a leap year or not.

Returning Multiple Values

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Python functions are able to return multiple values using one return statement. All values that should be returned are listed after the return keyword and are separated by commas.

In the example, the function square_point returns x_squared, y_squared, and z_squared by listing them after the return statement and separating them by commas.

Parameters As Local Variables

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Parameters are found inside the definition of a function and behave identically to local variables. However, they contain the values passed into the function when the function is called. Like local variables, they cannot be called outside of the scope of the function, or the program will throw an error.

In the example, the parameter c is defined as a part of the definition of my_function and therefore can only be accessed within my_function. Attempting to print the value of c outside of the function causes an error.

Global Variables

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

A variable that is defined outside of a function is called a global variable. It can usually be accessed inside a function. While a global variable can be modified on the outside of a function, it cannot be modified within a function. In the example, the variable a is a global variable because it is defined outside of the function f1. It is therefore accessible to f1, which will print the value of a.

Python Variable Scope

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

Not all the variables used by a function need to be included in the function definition. For instance, this function uses a variable defined on a previous line to create a greeting customized with a name.

The Scope of Variables

def my_function (x): #defines a function called my_function with a parameter x return x+1 #returns the value of x+1 print(my_function(2)) #inputs 2 and prints the output value 3 print (my_function(3)) #inputs 3 and prints the output value 4

In Python, a variable defined inside a function is called a local variable. It cannot be used outside of the scope of the function, and attempting to do so without defining the variable outside of the function will cause an error. In the example, the variable a is defined both inside and outside of the function. When the function f1 is implemented, a is printed as 2 because it is locally defined to be so. However, when printing a outside of the function, a is printed as 5 because it is implemented outside of the scope of the function.

Chevron Left Icon
Introduction to Functions
Lesson 1 of 2
Chevron Right Icon
  1. 1

    A function is a collection of several lines of code. By calling a function, we can call all of these lines of code at once, without having to repeat ourselves. So, a function is a tool that yo...

  2. 2

    Let's imagine that we are creating a program that greets customers as they enter a grocery store. We want a big screen at the entrance of the store to say: [...] We have learned to use print st...

  3. 3

    We have seen the value of simple functions for modularizing code. Now we need to understand how to write a function. To write a function, you must have a heading and an indented block of code. The ...

  4. 4

    Consider this function: [...] The three [...] statements are all executed together when [...] is called. This is because they have the same level of indentation. In Python, the amount of wh...

  5. 5

    Let's return to Engrossing Grocer's. The special of the day will not always be mandarin oranges, it will change every day. What if we wanted to call these three print statements again, except with ...

  6. 6

    Our grocery greeting system has gotten popular, and now other supermarkets want to use it. As such, we want to be able to modify both the special item and the name of the grocery store in a greetin...

  7. 7

    In our [...] function from the last exercise, we had two arguments: [...] Whichever value is put into [...] first is assigned to [...] , and whichever value is put in second is assigned to ...

  8. 8

    So far, we have only seen functions that print out some result to the console. Functions can also return a value to the user so that this value can be modified or used later. When there is a result...

  9. 9

    Sometimes we may want to return more than one value from a function. We can return several values by separating them with a comma: [...] This function takes in an x value and a y value, and ret...

  10. 10

    Let's say we have our function from the last exercise that creates a string about a special item: [...] What if we wanted to access the variable [...] outside of the function? Could we use it? ...

  11. 11

    Great! So far you have learned: - how to write a function - how to give a function inputs - how to return values from a function - what scope means Let's practice these concepts again so that you...

  1. 1

    This lesson will help you review Python functions by providing some challenge exercises. As a refresher, function syntax looks like this: [...] For example, a function that takes in a number a...

  2. 2

    [...]

  3. 3

    [...]

  4. 4

    [...]

  5. 5

    [...]

  6. 6

    [...]

  7. 7

    [...]

  8. 8

    [...]

  9. 9

    [...]

  10. 10

    [...]

  11. 11

    [...]

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn Python: Functions

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]