Python Fundamentals: Understanding Functions

In Python programming, functions are essential building blocks that allow you to organize and reuse code effectively. Functions help break down complex problems into smaller, manageable tasks, making your code more readable and maintainable. In this guide, we will explore the fundamentals of Python functions, including their syntax, parameters, return values, and more.

1. Defining Functions

A function in Python is defined using the def keyword followed by the function name and parentheses. You can also specify parameters inside the parentheses if the function requires input values.

Syntax:

def function_name(parameters):
    # Function body
    # Statements

Example:

def greet():
    print("Hello, World!")

2. Calling Functions

To call a function in Python, you simply write the function name followed by parentheses. If the function requires input values, you can pass them inside the parentheses.

Syntax:

function_name(arguments)

Example:

greet()

3. return Statement

The return statement in Python is used to exit a function and return a value to the caller. You can use the return statement to send a result back to the calling code.

Syntax:

def function_name(parameters):
    # Function body
    return value

Demostration using all:

def add_numbers(a, b):
    return a + b

result = add_numbers(5, 3)
print(result)

4. Args , Default Args and Kwargs

4.1. Args

Using Args you can pass any number of arguments to a function. The arguments are passed as a tuple.

Syntax:

def function_name(*args):
    # Function body

Example:

def display_args(*args):
    print(args)

display_args(1, 2, 3, 4, 5)

Output:

(1, 2, 3, 4, 5)

4.2. Default Args

Default arguments are used when you do not pass a value for an argument in the function call. If a value is not provided, the default value is used.

Syntax:

def function_name(arg=value):
    # Function body

Example:

def add(a,b,c=0):
    return a+b+c

result = add(5,3)
print(result)
result = add(5,3,2)
print(result)

Output:

8
10

4.3. Kwargs

Using Kwargs you can pass any number of keyword arguments to a function. The arguments are passed as a dictionary.

Syntax:

def function_name(**kwargs):
    # Function body

Example:

def display_kwargs(**kwargs):
    print(kwargs)

display_kwargs(name="Alice", age=30, city="New York")

Output:

{'name': 'Alice', 'age': 30, 'city': 'New York'}

4.4. Combination of Args and Kwargs

Example:

def display_args_kwargs(*args, **kwargs):
    print(args)
    print(kwargs)

display_args_kwargs(1, 2, 3, name="Alice", age=30)

Output:

(1, 2, 3)
{'name': 'Alice', 'age': 30}

4.5. Unpacking Args and Kwargs

Example:

def display_args_kwargs(*args, **kwargs):
    print(args)
    print(kwargs)

args = (1, 2, 3)
kwargs = {"name": "Alice", "age": 30}
display_args_kwargs(*args, **kwargs)

Output:

(1, 2, 3)
{'name': 'Alice', 'age': 30}

5. Lambda Functions

Lambda functions, also known as anonymous functions, are small, single-expression functions that do not require a name. They are defined using the lambda keyword.

Syntax:

lambda arguments: expression

Example:

add = lambda a, b: a + b
result = add(5, 3)
print(result)

Output:

8

6.Map and filter functions

6.1. Map Function

The map() function in Python applies a given function to each item of an iterable (such as a list, tuple, or set) and returns a new iterable with the results.

Syntax:

map(function, iterable)

Example:

numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
print(list(squared))

Output:

[1, 4, 9, 16, 25]

6.2. Filter Function

The filter() function in Python filters elements from an iterable based on a given function. It returns an iterator with the elements that satisfy the condition.

Syntax:

filter(function, iterable)

Example:

numbers = [1, 2, 3, 4, 5]
even = filter(lambda x: x % 2 == 0, numbers)
print(list(even))

Output:

[2, 4]

7.Local and Global Variables

7.1 Local Variables

  • Defined inside a function.
  • Exist only while the function runs.
  • Cannot be accessed outside that function.
  • Python automatically treats any variable assigned inside a function as local, unless told otherwise. Example:
def display():
    message = "Hello, World!"  # local variable created
    print(message)

display()

print(message)  # ❌ Error: 'message' is not defined

Here, message only exists inside display(). Outside, Python doesn’t know it.

7.2 Global Variables

  • Defined outside all functions.
  • Accessible anywhere in the program (both inside and outside functions).
  • To modify a global variable inside a function, we must use the global keyword.

Example:

message = "Hello, World!"  # global variable
def display():
    print(message)  # reads global variable

display()

def change_message():
    global message   # tells Python we mean the global variable
    message = "Hello, Python!"  # modifies global, not a new local one

change_message()
print(message)  # ✅ "Hello, Python!"

Without global, Python would create a new local variable instead of changing the global one.

7.3 Local vs Global Variables

  • If a variable inside a function has the same name as a global variable, Python assumes it’s a local variable (unless you declare it global).
  • This is why local variables take precedence within the function scope.

Example:

message = "Hello, World!"  # global variable

def display():
    # new local variable created (same name as global)
    message = "Hello, Python!"
    print(message)   # local variable used here

display()
print(message)  # global variable is still unchanged

Output:

Hello, Python!
Hello, World!

Inside display(), Python ignores the global message and makes a new local one.

Key rule to remember:

  • Assignment inside a function → local variable (by default).
  • If you want to change a global variable → use global varname.