Saturday, 2 May 2026

Working With Functions

 πŸ“˜ Working with Functions 


πŸ”Ή 1. Introduction to Functions

πŸ“Œ What is a Function?

function is a block of reusable code that performs a specific task.

πŸ‘‰ Instead of writing the same code again and again, we use functions.


πŸ“Œ Advantages of Functions

  • Code reusability
  • Reduces redundancy
  • Improves readability
  • Easy debugging
  • Modular programming

πŸ“Œ Types of Functions

  1. Built-in functions → print(), len()
  2. User-defined functions
  3. Recursive functions
  4. Lambda (anonymous) functions

πŸ”Ή 2. Creating User-Defined Functions

πŸ“Œ Syntax:

def function_name(parameters):

    # body of function

    return value


πŸ“Œ Example:

def greet():

    print("Hello Students")

πŸ‘‰ Calling function:

greet()


πŸ”Ή 3. Function Arguments (VERY IMPORTANT)


1. Required (Positional) Arguments

  • Must be passed in correct order

def add(a, b):

    print(a + b)

             add(5, 3)


2. Default Arguments

  • Assign default value

def greet(name="Guest"):

    print("Hello", name)

greet()

greet("Bhav")


3. Keyword Arguments

  • Values passed using parameter names

                add(b=10, a=5)


4. Variable Length Arguments

πŸ”Έ *args (Non-keyword)

def total(*nums):

    print(sum(nums))


    total(1,2,3,4)

πŸ”Έ **kwargs (Keyword arguments)

def info(**data):

    print(data)


    info(name="Bhav", age=20)


πŸ”Ή 4. Return Statement

πŸ“Œ Purpose:

  • Returns result to caller
  • Ends function execution

    def square(x):
        return x*x


πŸ“Œ Multiple Return Values

def calc(a, b):

    return a+b, a-b


    sum1, diff = calc(10, 5)


πŸ”Ή 5. Scope of Variables


πŸ“Œ Local Variable

  • Defined inside function
  • Accessible only within function

def demo():

    x = 10


πŸ“Œ Global Variable

  • Defined outside function
  • Accessible everywhere

x = 20

def show():

    print(x)


πŸ“Œ Using global Keyword

x = 5

def change():

    global x

    x = 10


πŸ”Ή 6. Recursion (IMPORTANT)

πŸ“Œ Definition:

    Function calling itself


πŸ“Œ Example: Factorial

def factorial(n):

    if n == 1:

        return 1

    return n * factorial(n-1)


πŸ“Œ Key Points:

  • Must have base condition
  • Otherwise → infinite recursion

πŸ”Ή 7. Lambda Functions (Anonymous Functions)

πŸ“Œ Definition:

        Small one-line function without name


πŸ“Œ Syntax:

    lambda arguments: expression


πŸ“Œ Example:

square = lambda x: x*x

print(square(5))


πŸ“Œ Use Cases:

  • Short operations
  • Used with map(), filter()

πŸ”Ή 8. Built-in Functions (Common)

Function

Use

len()

Length

sum()

Total

max()

Maximum

min()

Minimum


πŸ”Ή 9. Functions with Lists

def total(lst):

    return sum(lst)


    print(total([1,2,3]))


πŸ”Ή 10. Functions with Strings

def count_vowels(s):

    count = 0

    for ch in s:

        if ch.lower() in "aeiou":

            count += 1

    return count


πŸ”Ή 11. Passing Arguments (Important Concept)

πŸ“Œ Call by Value vs Reference (Python Concept)

  • Immutable → passed by value (int, string)
  • Mutable → passed by reference (list)

def modify(lst):

    lst.append(100)

l = [1,2]

modify(l)

print(l)


πŸ”Ή 12. Docstring in Functions

πŸ“Œ Purpose:

  • Describe function

def add(a, b):

    """This function returns sum"""

    return a + b


πŸ”Ή 13. Good Programming Practices

Use meaningful function names

Keep functions short

Use comments/docstrings

Avoid global variables


🧠 Important Viva Questions

  1. What is a function?
  2. Difference between parameter and argument?
  3. What is recursion?
  4. What is lambda function?
  5. Difference between local and global variable?
  6. What is default argument?
  7. What is *args and **kwargs?

No comments:

Post a Comment

Working With Functions

  πŸ“˜ Working with Functions  πŸ”Ή 1. Introduction to Functions πŸ“Œ What is a Function? A  function  is a block of reusable code that per...