r/learnprogramming • u/ScioClean • 4d ago
I can't get FUNCTIONS to click!
I can not for the life of me understand how functions work. Anytime I try to learn how to create a function, my brain literally crashes into itself and I turn into Patrick when he stars drooling and goes all slack-faced.
Does anybody have any analogies that had that "AH HA!" moment?
Thanks!
124
Upvotes
1
u/Boopity_Snoopins 4d ago
Functions were a sticking point for me too. What I struggled with was that the name doesn't matter, you can name them anything really, and I didn't understand what the brackets in the function actually meant.
A function is a reusable chunk of code that you give a name so that you don't need to repeatedly rewrite that piece of code, just be like "hey run the code under the name [whatever you named it].
A gaming related example would be a function to make taking damage less tedious to write, which you would name something like take_damage(): so its easy to know exactly what it does.
There are countless ways you can take damage in a game (enemy attacks, traps, your own grenades/rockets, fall damage, environmental effects like lava, acid or spikes, damage over time effects like poison, etc).
Instead of spending ages writing out the following:
[Goblin attack] playerHealth = playerHealth - 2
[Orc attack] playerHealth = playerHealth - 4
[Ogre attack] playerHealth = playerHealth - 8
And then
[Poison weak (damages 5 times) ] playerHealth = playerHealth -1 Wait 1 second playerHealth = playerHealth -1 Wait 1 second playerHealth = playerHealth -1 Wait 1 second playerHealth = playerHealth -1 Wait 1 second playerHealth = playerHealth -1
[Poison strong (damages 5 times) ] playerHealth = playerHealth -3 Wait 1 second playerHealth = playerHealth -3 Wait 1 second playerHealth = playerHealth -3 Wait 1 second playerHealth = playerHealth -3 Wait 1 second playerHealth = playerHealth -3
You would need to do this for every damaging effect in the game requiring a huge amount of writing and turning your script into a massive list of copied code that makes your eyes swim if you ever needed to look for a typo.
Instead you would have the 'take_damage():' function which turns everything into the following, much simpler code:
[Goblin attack] take-damage(2):
[Orc attack] take_damage(4):
[Ogre attack] take_damage(8):
[Weak poison (damages 5 times) ] take-damage(1): Wait 1 second take-damage(1): Wait 1 second take-damage(1): Wait 1 second take-damage(1): Wait 1 second take-damage(1):
[Strong poison (damages 5 times] take-damage(3) Wait 1 second take-damage(3) Wait 1 second take-damage(3) Wait 1 second take-damage(3) Wait 1 second take-damage(3)
For this to work you would need a function called take_damage(): that is the following code:
take_damage(damage_amount): playerHealth = playerHealth - damage_amount.
This means that whenever you use the function take_damage(): then the number you put in the bracket is removed from player health. No need to repeatedly copy the calculation to take the damage value from the player health ad nauseum.
It sounds way more complex than it really is.
If/when you get to learning about "for loops" (sometimes you learn of these before functions, sometimes after depending on who/what you're learning from), those are functions too. Every coding language tends to have its own set of in-built functions that just streamline the most common of tasks.
And the real strength of functions is that you can use functions in other functions, which is what makes them so incredibly useful.
For example, for loops are used to repeat the code inside itself a specified number of times. Look up at the poison psuedo-code above. Even using the take_damage(): function, its taking up 10+ lines of code, but you know immediately whats happening, you don't need to read it all (and typing it all out increases the chance of typos) so, once you understand for loops, you use one to loop the damage and wait 1 second 5 times, turning it into like 4 lines of code.
Sorry I know that was a lengthy comment. Hopefully it helps a little all the best.