✨ Introduction
In programming, we often need to perform the same task again and again. Writing the same code repeatedly can make programs long and confusing.
This is where functions help us.
A function is one of the most important concepts in programming, especially in languages like Python.
๐น What is a Function?
A function is a block of code that performs a specific task.
๐ In simple words:
A function is like a machine. You give it input, it processes it, and gives you output.
✔ Key Points:
1.Every function has a unique name
2.It performs a specific task
3.It can be called (used) anytime
๐ฏ Why Do We Use Functions?
Functions make programming easier and more efficient.
✔ 1. Ease of Use
Write code once and use it many times.
✔ 2. Modularity
Break a large program into smaller parts.
✔ 3. Reusability
Reuse the same function in different places.
✔ 4. Maintainability
Easy to fix and update code.
✔ 5. Readability
Code becomes clean and easy to understand.
๐ Return in Functions
A function can send back a value using the return keyword.
๐ Example:
return a + b
✔ Some functions return values
✔ Some only display output (using print)
๐น Types of Functions
1. Built-in Functions
These are already created in Python.
✔ Features:
1.Pre-defined
2,Ready to use
3.Perform common tasks
๐ Example:
print()
2. User-Defined Functions
These are functions created by the programmer.
✔ Features:
1.Custom-made
2.Designed for specific tasks
๐งพ Syntax of a Function
The syntax of writing a function in Python is as following:
def function_name(parameters):
statements
return value
๐ Explanation:
1."def" → keyword to define a function
2."function_name" → name of function
3."parameters" → inputs
4."statements" → code block
5."return" → output


Comments
Post a Comment