Key Concepts

Review core concepts you need to learn to master this subject

Functions

# Define a function my_function() with parameter x def my_function(x): return x + 1 # Invoke the function print(my_function(2)) # Output: 3 print(my_function(3 + 5)) # Output: 9

Some tasks need to be performed multiple times within a program. Rather than rewrite the same code in multiple places, a function may be defined using the def keyword. Function definitions may include parameters, providing data input to the function.

Functions may return a value using the return keyword followed by the value to return.

Introduction to Functions
Lesson 1 of 1
  1. 1
    In programming, as we start to write bigger and more complex programs, one thing we will start to notice is we will often have to repeat the same set of steps in many different places in our progra…
  2. 2
    Let’s come back to the trip planning application we just discussed in the previous exercise. The steps we talked about for our program were: 1. Establish an origin and destination 2. Calculate …
  3. 3
    A function consists of many parts, so let’s first get familiar with its core - a function definition. Here’s an example of a function definition: def function_name(): # functions tasks go he…
  4. 4
    Now that we’ve practiced defining a function, let’s learn about calling a function to execute the code within its body. The process of executing the code inside the body of a function is known as …
  5. 5
    Consider our welcome function for our trip planning application: def trip_welcome(): print(“Welcome to Tripcademy!”) print(“Let’s get you to your destination.”) The print statements all ru…
  6. 6
    Let’s return to our trip_welcome() function one more time! Let’s modify our function to give a welcome that is a bit more detailed. def trip_welcome(): print(“Welcome to Tripcademy!”) print(“L…
  7. 7
    Using a single parameter is useful but functions let us use as many parameters as we want! That way, we can pass in more than one input to our functions. We can write a function that takes in mor…
  8. 8
    In Python, there are 3 different types of arguments we can give a function. * Positional arguments: arguments that can be called by their position in the function definition. * Keyword arguments…
  9. 9
    There are two distinct categories for functions in the world of Python. What we have been writing so far in our exercises are called User Defined Functions - functions that are written by users …
  10. 10
    As we expand our programs with more functions, we might start to ponder, where exactly do we have access to our variables? To examine this, let’s revisit a modified version of the first function we…
  11. 11
    At this point, our functions have been using print() to help us visualize the output in our interpreter. Functions can also return a value to the program so that this value can be modified or use…
  12. 12
    Sometimes we may want to return more than one value from a function. We can return several values by separating them with a comma. Take a look at this example of a function that allows users in our…
  13. 13
    Excellent work! 👏 In this lesson, you’ve covered a major fundamental programming concept used in the majority of all programs to organize code into reusable blocks. To recap, we explored: - What …

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