Functions are one of the most important concepts in Python programming. They help developers write reusable, organized, readable, and maintainable code.
When developing applications, we often need to perform the same operation multiple times. Instead of writing the same code repeatedly, we can create a function and call it whenever required.
Example Without Function
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
Output:
Welcome to Python
Welcome to Python
Welcome to Python
Here, we are repeating the same statement three times.
Example With Function
def welcome():
print("Welcome to Python")
welcome()
welcome()
welcome()
Output:
Welcome to Python
Welcome to Python
Welcome to Python
Here, we created a function called welcome() and reused it three times.
Syntax of a Function
def function_name():
# Statements
# Function logic
function_name()
def – A keyword used to define a function.
function_name – The name given to the function.
() – Parentheses used to pass parameters.
: – Indicates the beginning of the function body.
Indentation – Defines the statements inside the function.
Function call – Executes the function.
Top comments (0)