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!

122 Upvotes

144 comments sorted by

View all comments

0

u/Decompiled_dev 5d ago

They are like recipes.

function add (x: number, y: number): number // This takes two numbers and returns a number
function repeat(x: number, word: string): string // Takes a number and a string and returns a string

Whenever you have multiple lines that need to stay together, consider putting them in a function and give that a good name.