PythonDefining Functions

Defining Functions in Python

A function is a reusable block of code that performs a specific task. Functions help you:

  • Organize code
  • Avoid repetition
  • Improve readability and maintainability

In Python, you define a function using the def keyword.


Basic Syntax of a Function

function_syntax.py
def function_name():
    # block of code

Explanation:

  • def – keyword used to define a function.
  • function_name – name of your function.
  • Parentheses () may include parameters.
  • The indented block is the function body — it contains the code to execute.

Example 1: Defining and Calling a Function

function_simple.py
def greet():
    print("Hello, welcome to Python!")
 
greet()
output.txt
Hello, welcome to Python!

Explanation:

  • The function greet() is defined first.
  • Calling greet() executes the function.

Example 2: Function With One Parameter

function_parameter.py
def greet(name):
    print("Hello,", name)
 
greet("Alice")
output.txt
Hello, Alice

Explanation:

  • The function accepts a parameter called name.
  • When called, the argument "Alice" is passed in.

Example 3: Function With Multiple Parameters

function_multi_params.py
def add(a, b):
    print("Sum is:", a + b)
 
add(5, 7)
output.txt
Sum is: 12

Explanation:

  • Functions can take multiple parameters.
  • You can pass any number of arguments (that match the parameter list).

Example 4: Function With Default Parameter

function_default.py
def greet(name="Guest"):
    print("Hello,", name)
 
greet()
greet("Bob")
output.txt
Hello, Guest
Hello, Bob

Explanation:

  • Default parameters provide a default value if no argument is passed.
  • Useful for optional arguments.

Example 5: Function Returning a Value

function_return.py
def square(n):
    return n * n
 
result = square(4)
print("Square is:", result)
output.txt
Square is: 16

Explanation:

  • The return statement sends a value back to the caller.
  • You can assign the returned value to a variable.

Example 6: Function Documentation (Docstring)

function_docstring.py
def greet():
    """This function prints a greeting message."""
    print("Hello!")
 
print(greet.__doc__)
greet()
output.txt
This function prints a greeting message.
Hello!

Explanation:

  • The first string inside a function is a docstring.
  • It documents what the function does.
  • You can access it with function_name.__doc__.

Why Use Functions?

BenefitDescription
Code ReuseWrite once, use many times
ModularityBreak complex problems into simpler parts
ClarityImprove code readability
MaintainabilityEasier to update, test, and debug
Avoid RepetitionNo need to duplicate code

Summary

  • Functions in Python are defined with def.
  • Functions may have parameters and may return values.
  • They improve code structure and reusability.
  • Use docstrings to document what your function does.

Next, we will explore Arguments and Parameters — how to pass data to functions in flexible and powerful ways.