Functions are blocks of code that perform a specific task and return a value. They are a type of subprogram, along with procedures. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail).
Functions are used when the result of a calculation or operation needs to be obtained
Function Example
def calculateSquare(num): return num * num result = calculateSquare(5)
Which keyword is used to define a function in python?
Which of the following statements is true about functions?
Introduction to Procedures
What are Procedures?
Procedures, also known as subroutines or methods, are blocks of code that perform a specific task without returning a value. They can accept input parameters and modify the values of variables. Procedures are typically used to group related code together and make the program more organized and modular.
procedures are used when a specific task needs to be performed without a required result.
Procedure Example
def greetUser(name) : print(f"Nice to meet you {name}") greetUser("John"); // Output: Hello, John!
What is the main purpose of procedures?
What is the purpose of using functions and procedures in programming?
What is the correct syntax for declaring a function in Python?
When do you use a function instead of a procedure?
Subprogram
Local Variables
Local variables are variables that are defined within a function and are only accessible within that function. They exist only during the function's execution, and once the function finishes, the local variables are destroyed and their values are lost.
def greet(): message = "Hello, World!" # 'message' is a local variable print(message) greet() print(message)
# This will raise an error because 'message' is not accessible outside the function
The variables in a function are only accessible within that function.
Global Variables
Global variables are variables that are defined outside of any function and can be accessed and modified by any function within the same program. They persist for the duration of the program's execution and can be used to share data across different parts of the program.
If we want to alter global variables from inside a function then we need to use the global keyword inside the function.
count = 0 def increment(): global count # Declare that we are using the global variable 'count' count += 1 # Modify the global variable def show_count(): print(f"Count: {count}") increment() show_count() # Output will be: Count: 1
The scope of a extends throughout the entire program file after its declaration.
Variable Scope
Scope refers to the region of a program where a variable is accessible. In Python, there are two main types of scope:
Local Scope
A variable defined within a function has a local scope. It can only be accessed within that function.
Once the function finishes executing, the local variables are destroyed.#
Global Scope
A variable defined outside of all functions has a global scope. It can be accessed by any function within the program.
Global variables persist throughout the program's execution