What the hell are functions in Python | TechAmbitionX | An easy guide by Mr. BILRED
What the hell is a Function in Python?
Okay relax, you might've not opened the book, and exam is around the corner, but still, there's hope. HOPE!
What is a Function?
A function is a reusable block of code that performs a specific task. Functions help you organize code, avoid repetition, and make programs easier to understand. Got it?
Creating Functions with def
def greet():
print("Hello, TechAmbitionX!")
This defines a function named greet. To use it, call it like this:
greet() # Output: Hello, TechAmbitionX!
Returning Values with return
Functions can return values instead of printing them. This makes your code more flexible.
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
print() vs return
print()displays output immediately.returnsends data back to where the function was called, so you can store or use it later.
Okay, maybe upar se gai, but wait;
Use print() to see the result. Use return to use the result.
Positional vs Keyword Arguments
Functions accept inputs called arguments. You can pass them by position or by naming the parameters explicitly.
def info(name, age):
print(f"{name} is {age} years old.")
info("Bilal", 22) # Positional arguments
info(age=22, name="Bilal") # Keyword arguments
Default Parameter Values
You can set default values for parameters. If an argument is not provided, the default is used.
def greet(name="TechAmbitionX"):
print(f"Hello, {name}!")
greet() # Output: Hello, TechAmbitionX!
greet("Bilal") # Output: Hello, Bilal!
Functions Taking Input & Returning Output
Functions often accept inputs and return outputs to be used elsewhere in your program.
def square(num):
return num * num
print(square(4)) # Output: 16
🔄 Nested and Recursive Functions
Nested functions are functions inside functions:
def outer():
def inner():
print("Hello from inner function!")
inner()
outer()
Recursive functions call themselves to solve problems step-by-step:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
The above example may cause a small headache to understand, but I'll leave it that way so you may work on that(InshaALLAH that understanding would help ya). Hint: Recursive Function keeps calling itself with a smaller or simpler value until it reaches a point where it stops called the base case.
Lambda Functions (Anonymous Functions)
Lambda functions are short, unnamed functions useful for simple tasks:
square = lambda x: x * x
print(square(6)) # Output: 36
Quick Recap
| Concept | Example |
|---|---|
| Define Function | def func(): ... |
| Return Value | return value |
| Print vs Return | print() shows output, return sends data back |
| Arguments | Positional & Keyword |
| Default Params | def func(x=5): ... |
| Nested Functions | Functions inside functions |
| Recursive Functions | Functions calling themselves |
| Lambda | Anonymous short functions |
QW5kIGxpc3RlbiwgSWYgeW91IHdhbnQgbW9yZSBERVRBSUxFRCBTVFVGRiwgb3IgaGVscCwgY29udGFjdCBtZSBhdCBNci5CSUxSRURAcHJvdG9uLm1l
OKAY, NOW DONT RUN AWAY NOW, TRY YOUR CODES HERE OR AT YOUR CODE EDITOR... PROGRAMMING SIRAF DEKHNEY SE HI NAHI AAJATI(I Can't Much about this something, but that's what they say), DO PRACTICE
This post is for educational purposes only.
What I Think:
KNOWLEDGE SHOULD BE SHARED ONLY WITH THE ONE, WHO KNOWS ITS TRUE WORTH. Not everyone deserves it. - Bilal Ahmad Khan AKA Mr. BILRED

Comments
Post a Comment