csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.
 Facebook   ………………..      Instagram   ……………..      Twitter ( X )      ……………..     YouTube


Full details of Functions in Python with suitable example

Functions in Python are blocks of reusable code that perform a specific task. They help in organizing and structuring programs by breaking them down into smaller, more manageable parts.

A function is defined using the def keyword, followed by the function name and parentheses. Any required input parameters are specified within the parentheses.
The body of the function is indented and contains the code that will be executed when the function is called.

Functions can return values using the return statement, which allows the function to pass back a result or information to the caller. If a function does not contain a return statement, it returns None by default.


Types of Functions

Built-in Functions

Built-in functions are pre-defined functions provided by Python. They are readily available for use without requiring any additional code. These functions cover a wide range of functionalities such as print(), len(), range(), input(), and type(). Built-in functions are accessible throughout the Python program.

User-defined Functions

User-defined functions are created by the programmer to perform specific tasks based on the program’s requirements. They help break a program into smaller, modular components, making the code more organized and reusable. User-defined functions are called by their names, and arguments can be passed to them if required.


Creating a Function

To create a function in Python, follow these steps:

  1. Use the def keyword followed by the function name to define the function.
    Example: def greet():

  2. Add parentheses after the function name to define parameters, if any.
    Example: def greet(name):

  3. Use a colon (:) to indicate the start of the function’s code block.

  4. Indent the code block using spaces or tabs.

  5. Write the code that you want the function to execute.

  6. Optionally, use the return statement to return a value.

  7. Call the function using its name followed by parentheses.

Example:

def greet(name):

print(“Hello, “ + name + “!”)

greet(“John”)      # Output: Hello, John!


Function Calling

Function calling in Python is the process of executing a defined function by using its name followed by parentheses. When a function is called, the code inside its code block is executed.

If the function accepts arguments, they are passed inside the parentheses.


Return Statement

The return statement in Python specifies the value that a function should return. It allows a function to send a result back to the calling code.

  • When the return statement is executed, the function stops running.

  • The value following the return keyword becomes the result of the function call.

Example:

def add_numbers(num1, num2):

return num1 + num2

result = add_numbers(5, 3)

print(result)     # Output: 8


Scope of Variables

The scope of a variable refers to the region of a program where the variable is accessible. It determines the visibility and lifetime of the variable.

Types of Variable Scope in Python

  1. Global Scope
    Variables defined outside any function or class have global scope and can be accessed throughout the program.

  2. Local Scope
    Variables defined inside a function have local scope. They are accessible only within that function and exist only during the function’s execution.

Example:

global_var = 10      # Global variable

def my_function():

local_var = 20

print(global_var)

print(local_var)

my_function()

print(global_var)

print(local_var)     # Error

 

Short Questions & Answers

  1. What is a function in Python?
    A function is a block of reusable code that performs a specific task.
  2. Why are functions used in Python?
    Functions are used to make programs modular, organized, and reusable.
  3. Which keyword is used to define a function in Python?
    The def keyword is used to define a function.
  4. What is the purpose of parentheses in a function definition?
    Parentheses are used to define parameters.
  5. What happens if a function does not return a value?
    It returns None by default.
  6. What are built-in functions?
    Built-in functions are pre-defined functions provided by Python.
  7. Give two examples of built-in functions.
    print() and len().
  8. What are user-defined functions?
    Functions created by the programmer to perform specific tasks.
  9. What is function calling?
    Function calling is the process of executing a function using its name.
  10. What symbol indicates the start of a function block?
    A colon (:).
  11. Why is indentation important in Python functions?
    It defines the function body.
  12. What is a return statement?
    A return statement sends a value back to the caller.
  13. What happens when a return statement is executed?
    The function execution stops immediately.
  14. What is variable scope?
    Scope defines where a variable can be accessed in a program.
  15. What is global scope?
    Variables defined outside any function have global scope.
  16. What is local scope?
    Variables defined inside a function have local scope.
  17. Can a global variable be accessed inside a function?
    Yes.
  18. Can a local variable be accessed outside the function?
    No.
  19. What is the lifetime of a local variable?
    It exists only during function execution.
  20. Why are functions important in Python?
    They improve code readability, reuse, and maintenance.

MCQs (20 Questions with Answers)

  1. A function in Python is used to:
    a) Store data
    b) Perform a specific task
    c) Define variables
    d) Import modules
    ✔ Answer: b
  2. Which keyword is used to define a function?
    a) func
    b) function
    c) def
    d) define
    ✔ Answer: c
  3. What does a function return by default?
    a) 0
    b) False
    c) None
    d) Error
    ✔ Answer: c
  4. Which of the following is a built-in function?
    a) greet()
    b) add()
    c) print()
    d) myFunc()
    ✔ Answer: c
  5. User-defined functions are created by:
    a) Compiler
    b) Interpreter
    c) Programmer
    d) Python
    ✔ Answer: c
  6. What symbol ends a function definition line?
    a) ;
    b) .
    c) :
    d) ,
    ✔ Answer: c
  7. What is function calling?
    a) Defining a function
    b) Executing a function
    c) Importing a function
    d) Deleting a function
    ✔ Answer: b
  8. How are arguments passed to a function?
    a) Using brackets
    b) Using quotes
    c) Inside parentheses
    d) Using commas only
    ✔ Answer: c
  9. Which statement is used to return a value?
    a) send
    b) output
    c) return
    d) break
    ✔ Answer: c
  10. When does a function stop executing?
    a) At print statement
    b) At return statement
    c) At input statement
    d) At function call
    ✔ Answer: b
  11. Variables defined outside a function are:
    a) Local
    b) Static
    c) Global
    d) Private
    ✔ Answer: c
  12. Variables defined inside a function are:
    a) Global
    b) Local
    c) Public
    d) Static
    ✔ Answer: b
  13. Local variables exist until:
    a) Program ends
    b) Function completes
    c) Variable is printed
    d) System shuts down
    ✔ Answer: b
  14. Which variable can be accessed everywhere?
    a) Local
    b) Temporary
    c) Global
    d) Static
    ✔ Answer: c
  15. What improves code reusability?
    a) Loops
    b) Conditions
    c) Functions
    d) Variables
    ✔ Answer: c
  16. What defines the body of a function?
    a) Brackets
    b) Indentation
    c) Quotes
    d) Semicolon
    ✔ Answer: b
  17. Which function gets user input?
    a) print()
    b) type()
    c) input()
    d) len()
    ✔ Answer: c
  18. What is the output of a function without return?
    a) 0
    b) False
    c) None
    d) Error
    ✔ Answer: c
  19. Which is NOT a built-in function?
    a) len()
    b) range()
    c) greet()
    d) input()
    ✔ Answer: c
  20. Functions help in making programs:
    a) Complex
    b) Longer
    c) Organized
    d) Slower
    ✔ Answer: c