Coding Pirates

Lesson 6

Functions

A function is a command you make yourself. You give it a name, tell it what it needs, and say what it should give back. Then you can use it as many times as you like.

def greet(name: str) -> str:
    return f"Ahoy, {name}!"

message = greet("Ada")
print(message)
  • def starts a function. greet is its name.
  • name: str is a parameter: something the function needs. The : str says it should be text.
  • -> str says the function returns text.
  • return sends the answer back to whoever called the function.
  • Calling greet("Ada") runs the function with name set to "Ada".
1 / 4