What are Functions ?
A function in Python is a reusable block of code that performs a specific task.
Instead of writing the same code again and again, we can place it inside a function and call it whenever needed.
Features :
Reusability - A function allows you to write code once and use it multiple times.
Modularity - Functions divide a big program into smaller logical parts.
Readability - Functions give meaningful names to operations.
Abstraction - Functions hide internal logic and expose only purpose.
Creation :
Python
def function_name(): statement(s)def → keyword to define a function
function_name → name of the function
() → parentheses (parameters go here)
: → indicates block start
indented statements → function body
Function Names :
- A function name must start with a letter or underscore
- A function name can only contain letters, numbers, and underscores
- Function names are case-sensitive (myFunction and myfunction are different)
Example :
Python
def greet(): print("Welcome to Python!") greet() #output 'Welcome to Python!'