Logo

CHiPSET

A Technical Community

Python Functions Tutorial for Beginners | AIML EP-5

Learn Python Functions with simple explanations and practical examples. Understand function syntax, parameters, return statements, lambda functions, recursion, and more in this beginner-friendly Python tutorial.

ChakradharMay 25, 202630 min
#python functions#lambda functions#recursive functions
Python Functions Tutorial for Beginners | AIML EP-5

Functions are one of the most important concepts in Python. They help you organize code into reusable blocks, making programs cleaner, shorter, and easier to manage.

What is a Function in Python?

A function is a block of code that performs a specific task. Instead of writing the same code multiple times, you can create a function once and use it whenever needed.

Image

Image

image:wht is the use of function

image source:Geeksforgeeks

Example

Image

Image

Here:

  • def is used to define a function
  • greet is the function name
  • The code inside the function is called the function body
  • Why Use Functions?

    Functions provide many advantages in programming.

    Benefits of Functions

  • Reduce code repetition
  • Improve readability
  • Make debugging easier
  • Help organize large programs
  • Allow code reusability
  • Image

    Image

    image:syntax of the function

    source: GeeksForGeeks

    Creating a Function

    In Python, functions are created using the def keyword.

    Syntax

    Image

    Image

    Calling a Function

    A function only runs when it is called.

    Example

    Image

    Image

    Function with Parameters

    Parameters allow functions to accept input values.

    Image

    Image

    Arguments are values passed to a function when it is called. They allow functions to receive input data and perform operations using those values.

    Syntax

    def function_name(arguments): # function body return value

  • def function_name(arguments): defines a function with optional arguments.
  • function body contains the statements to be executed.
  • return value returns a result from the function. If no return statement is used, it returns None by default.
  • Function with Multiple Parameters

    You can pass multiple values into a function.

    Example

    def add(a, b):

    print(a + b)

    add(5, 3)

    Output

    8

    Return Statement in Functions

    The return keyword sends a value back from the function.

    Image

    Image

    Difference Between print() and return

    Using print()

    def add(a, b):

    print(a + b)

  • Displays output directly
  • Cannot store the value easily
  • Using return

    def add(a, b):

    return a + b

  • Sends value back
  • Can store and reuse the result
  • Types of Functions in Python

    Built-in Functions

    Python already provides many ready-made functions.

    Examples

    print()

    len()

    type()

    input()

    User-defined Functions

    Functions created by programmers are called user-defined functions.

    Example

    def multiply(a, b):

    return a * b

    1. Default argument: Default argument use a predefined value when no value is passed during the function call.

    Image

    Image

    Keyword Arguments

    pass values using parameter names, so argument order does not matter.

    example shown below

    Image

    Image

    Local Variables

  • A local variableis a variable that is declared inside a function.
  • It only exists while the function is running.
  • Once the function finishes, the variable is destroyed and cannot be accessed outside.
  • Image

    Image

    Here:

  • x is created inside the function test().
  • When you call test(), Python prints 10.
  • But if you try print(x) outside the function, you’ll get an error because x only lives inside test().
  • Global Variables

  • A global variable is declared outside of all functions.
  • It can be accessed by any function in the program (unless shadowed by a local variable with the same name).
  • It exists as long as the program runs.
  • Example:

    Image

    Image

    Here:

  • x is defined outside the function, so it’s global.
  • The function show() can directly access it and prints 20.
  • Even if you call print(x) outside the function, it will still print 20.
  • Positional Arguments: values are assigned to parameters based on their order in the function call.

    example:

    Image

    Image

    Function within Functions

    A function defined inside another function is called an inner function (or nested function). It is used to organize related logic and access variables from the outer function.

    Image

    Image

    Pass by Reference and Pass by Value

    🔹 First, the big picture

  • In Python, everything is an object (numbers, strings, lists, etc.).
  • When you pass a variable into a function, Python doesn’t copy the object itself. Instead, it passes a reference to the object.
  • Whether the function can change the original object depends on whether the object is mutable or immutable.
  • 🔹 Mutable vs Immutable

  • Mutable objects: Can be changed in place. Examples → list, dict, set.
  • Immutable objects: Cannot be changed in place. Examples → int, float, str, tuple.
  • Key Concept: "Pass-by-object-reference"

  • Python doesn’t use pass-by-value (like C, where a copy is made).
  • Python doesn’t use pass-by-reference (like C++ with pointers, where you can directly change the caller’s variable).
  • Instead, Python uses pass-by-object-reference:
  • The function receives a reference to the object.
  • If the object is mutable, changes inside the function affect the original.
  • If the object is immutable, reassignment inside the function only affects the local reference.
  • 🧠 Analogy

    Think of variables as name tags pointing to boxes:

  • If the box is a mutable container (like a list), you can open it and change what’s inside → everyone sees the change.
  • If the box is an immutable sealed box (like an integer), you can’t change what’s inside. You can only stick your name tag on a different box → but that doesn’t move other people’s name tags.
  • Python Lambda Functions

    Image

    Image

    🔹 What is a Lambda Function?

    A lambda functionis just a small, anonymous function— meaning it has no name.It’s used when you need a quick, one-line function for short tasks.

    Think of it like a shortcut: instead of writing a full defblock, you write a one-liner.

    Syntax

    python

    lambda arguments: expression

  • lambda → keyword that defines the function
  • arguments → input values (like parameters)
  • expression → what the function returns (automatically)
  • Image

    Image

    Multiple arguments

    Image

    Image

    Recursive Functions

    A recursive function calls itself repeatedly until a condition is met.

    Example

    Image

    Image

    Common Errors in Functions

    Forgetting Parentheses

    greet

    Correct:

    greet()

    Missing Return Statement

    def add(a, b): a + b

    Correct:

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

    Conclusion

    Python functions help make programs modular, reusable, and easier to understand. By learning how to create, call, and use functions effectively, you can write cleaner and more professional Python code.

    previous episode: Python Loops Explained | For Loop and While Loop | AIML EP-4 | CHiPSET Tech Articles | CHIPSET SRM University Ramapuram

    Tags:#python functions#lambda functions#recursive functions

    Written by Chakradhar on May 25, 2026