Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. A Function with a Default Argument

Functions · Python example

A Function with a Default Argument

Define a reusable greeting function with a default parameter value.

Open the compilerAll examples
beginnerfunctionsparametersdefaults

Functions bundle logic behind a name. Default arguments make some parameters optional, which keeps calls short.

example.py
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Ada"))
print(greet("Grace", "Welcome"))

How the code works

  • The first call uses the default greeting 'Hello'.
  • The second call supplies 'Welcome' explicitly.
  • Each returned string is passed to print().

Key ideas

  • def starts a function definition.
  • greeting has a default value used when the argument is omitted.
  • return sends a value back to the caller.

Try changing it

  • Add a third parameter for punctuation.
  • Return a dictionary with the parts of the greeting.
  • Call the function from inside a loop over names.

Related examples

beginner

Hello World in Python

The classic first program: print a greeting and a second line of text.

intermediate

Bank Account Class

Model a bank account with a class, methods, and simple validation.

Previous exampleSet OperationsNext exampleQuadratic Equation Roots

Expected output

Hello, Ada!
Welcome, Grace!

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects