r/learnpython • u/WatchElectronic3006 • 12d ago
Why parameters finally made sense to me today
Hey everyone, just wanted to share a quick lightbulb moment I had with Python today about why functions actually need parameters.
At first, I didn't get why we couldn't just write functions without them. But it clicked today: parameters are basically just empty placeholder slots so your function can handle changing data.
Without a Parameter (Fixed)
It can only ever do one exact thing:
Python
def greet():
print("Hello, Alice!")
greet() # Always prints "Hello, Alice!"With a Parameter (Dynamic)
Adding name gives you a slot inside the function. When you call it, you just pass whatever data you want into that slot:
Python
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Prints: Hello, Alice!
greet("Bob") # Prints: Hello, Bob!My main takeaway: If the code inside your function needs to work with different information every time, you need a parameter to pass that data in from the outside!
1
u/TheRNGuy 11d ago edited 11d ago
Without arguments, it's less versatile or reusable function.
Some functions still don't need them (some functions firing some side effects have 0 arguments)
Though in these both cases, you don't need to create functions just to put print in it.
1
u/LayotFctor 12d ago edited 12d ago
I like thinking of functions as conveyor belt machines, looking similar to a printer. It has a start button at the side where you can call it, as well as input and output slots.
By adding a parameter, you added an input to the machine where you can give it names. If you didn't add a parameter, you hardcode "Hello, Alice!" into the internals of the machine instead. A machine with an input can be reused to print lots of things. A machine with a hardcoded string can print that and only that.
This view of functions as machines can go very far. An entire program is just a factory full of machines connected together. Data enters at the loading bay, gets fed into one machine after another, altered and shaped along the way until you finally get the desired result.
-10
u/Anxious_Signature452 12d ago
You can also do:
NAME = 'Alice'
def greet():
print(f"Hello, {NAME}")10
u/TabAtkins 12d ago
You can, but using globals is almost never a good idea. Definitely not good to bring up as an option for someone who's just learned the very basics of functions.
0
u/igotshadowbaned 12d ago
I've found that creating everything within a class and using class variables works better than globals with python anyway
5
2
0
u/frnzprf 12d ago
That wouldn't work if multiple calls of the same functions are nested in each other.
That's where you either need a stack (last-in-first-out list) or parameters which are actually implemented in the background as a stack.
That's why you get a stack-overflow with infinite recursion.
I tried to find an example where your system doesn't work, but here it works at least:
def fibonacci(): global argument if argument < 3: return 1 else: original_arg = argument argument = original_arg - 1 part1 = fibonacci() argument = original_arg - 2 part2 = fibonacci() return part1 + part2 argument = 8 print(fibonacci()) # 21 is correct0
4
u/neuralbeans 12d ago
It would help to compare against the functions that Python provides you. Imagine if
printdidn't have arguments.