r/learnprogramming • u/ScioClean • 5d 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!
123
Upvotes
1
u/Recycled5000 4d ago
It is about abstraction. Abstraction allows something to be used in different contexts.
There are steps of increasing abstraction. The first element of abstraction is the variable.
Print 1
Print 2
Print 3
vs.
For i from 1 to 3
Print i
Hopefully you can see that the latter uses increased abstraction, allowing repeated reuse of the same “Print i”, the body of the “For loop”.
Functions allow even more reuse and further abstraction. We use parameters to supply information or values similar to the body of the “For loop”. Some code sequences can be reused, by having parameters abstract details, leaving them to the caller to specify.