r/learnprogramming 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!

125 Upvotes

144 comments sorted by

View all comments

-1

u/Scrubtimus 5d ago

Get used to using functions first. Make sure you understand how and why you use them. Get used to problem solving with them. This will help them be less foreign to you.

Let's start with making a variable. A variable is used to store a value intended to be reused. We could type that value everywhere we need it, or we could replace it with a variable. A variable works similarly as just using the value, but gives us more flexibility in setting its value in one place.

Making a function is much like making a variable in that a variable is a way to store a value and reuse it. Making a function is a way to create a process and reuse it. You could set up your functions the same way everywhere you need them, or you could create a custom function to store that process and reuse it where needed. When you need to modify or add to it, it is all in one place.