Syncing account progress…

Back

Beginner · 12 min

Reusable functions

Define a function and pass a value to it.

Name a reusable action

def defines a function. Its body is indented and does not run until the function is called. Put parentheses after the function name to call it.

def greet():
    print("Welcome")

greet()
greet()

Output

Welcome
Welcome

Pass in a value

A parameter names an input inside the function. Each call supplies an argument for that parameter. Reuse the same instructions with different values.

def greet(name):
    print(f"Hello, {name}")

greet("Ada")
greet("Mina")

Output

Hello, Ada
Hello, Mina

Reusable functions

  1. Separate the function definition from its calls.
  2. Follow the argument into the parameter.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Reuse a greeting

Define greet(name) to print a greeting using its parameter. Keep both supplied calls. Display Hello, Ada and Hello, Mina on separate lines.

Practice