← Back to Blog

Give your function something to work with

The problem...

Your banner() function works. But every time you call it — it asks for input. You can't use it programmatically. You can't pass a title directly.

Your function is isolated. It can't receive information from the outside.

The idea!

Parameters are variables that let you pass values into a function when you call it. The function receives the value — and uses it.

The syntax

def function_name(parameter):
    # use parameter here

The parameter goes inside the parentheses. When you call the function — you pass a value. That value becomes the parameter inside the function.

Your first parameter

def greet(name):
    print(f"Hello, {name}.")
greet("Bull")     # Hello, Bull.
greet("Horn")     # Hello, Horn.
greet("Red")      # Hello, Red.

One function. Three calls. Three different names. The function adapts to what you give it.

Multiple parameters

def greet(name, role):
    print(f"Hello, {name}. You are a {role}.")
greet("Bull", "developer")
greet("Horn", "student")

# Hello, Bull. You are a developer.
# Hello, Horn. You are a student.

Separate parameters with commas. Pass values in the same order.

Banner — upgraded

def banner(title):
    border = "=" * len(title)
    print(border)
    print(title)
    print(border)

banner("RedHorn")
banner("Control Flow")
banner("Functions")
# =======
# RedHorn
# =======
# ============
# Control Flow
# ============
# =========
# Functions
# =========

No more input(). You pass the title directly. The function does the rest.

Parameters vs Arguments

def greet(name):     # name is the parameter — defined here
    print(name)

greet("Bull")        # "Bull" is the argument — passed here

Parameter — the variable in the function definition. Argument — the value you pass when calling. Same thing, different moment.

Heads up!

  • Parameters go inside def parentheses — arguments go inside call parentheses
  • Order matters — arguments are assigned to parameters by position
  • Too many or too few arguments — TypeError
  • Parameters are local — they only exist inside the function

The mindset shift

Stop thinking: "My function does one specific thing with one specific value."

Start thinking: "My function does one thing — with whatever value you give it."

What you should understand now

  • Parameters let you pass values into a function
  • The function receives the value and uses it as a variable
  • Multiple parameters — separate with commas, pass in order
  • Parameter = variable in definition. Argument = value in call.
[ login to bookmark ] // copied! 21 views · 1 min
// resources
Code Example parameters.py
← prev DEF Mini Project — Banner next → A value if you don't provide one
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.