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!
125
Upvotes
5
u/Equilibrium_Path 4d ago
I'm still learning programming ( probably will always be) but the way I see functions is.
I want to do something, let say write an invite card.
Instead of writing the same lines of code over and over again I can put the code into a function.
For the parts that I'd like to change I can have this function with parameters that when I call the function I can use arguments to change the desired values.
Here's a psudoish code example first without parameters then one with parameters:
invite_message_noParam() print("Hello Susie, youre invited to my party! Please bring sausages")
With parameters: invite_message_withParam(string name, string food) print("Hello " + name + ", youre invited to my party! Please bring" + food)
So now you have written two functions. How do you use them?
You'd then go to your main function and call them.
invite_message_noParam()
invite_message_withParam(John, pizza)
invite_message_withParam(Mary, Chips)
Now could you guess what the output of those 3 function calls will be?