Computer Science Researcher • Educator • Programming Instructor
A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, you write it once inside a function and call it whenever needed. Functions help make your programs organised, cleaner, and easier to maintain.
A function has two parts: 1. Definition – where you create the function 2. Call – where you tell Python to run it
def greet():
print("Hello, welcome to Python!")
greet()
Output:
Hello, welcome to Python!
Parameters let your function receive information.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Output:
8
def welcome(name):
return "Hello " + name
print(welcome("Khalid"))
Output:
Hello Khalid
1. What is a function?
A type of variable2. What keyword is used to create a function?
func3. What does a return statement do?
Stops the program4. What will this print?
def test(x):
return x * 2
print(test(4))
25. What is the output of this code?
def hello():
print("Hi")
hello()
hello