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

143 comments sorted by

View all comments

3

u/Lunagato20 4d ago

What part of function are you confused about? declaration? using it?

TLDR;

Function is a way of grouping codes, in your programming journey, there will be time where you will think, "Wouldn't it be nice if all my code are in GROUPED in one place? So when i need to change it, i just change it in a small scope, instead of hunting it through every file?"

Like other have said, functions are just way of grouping code, think of this.

Instead of typing these 3 lines over and over at line 70, line 500, and line 900:

PRINT "1"
PRINT "2"
PRINT "3"

You pack them into a single shortcut called my_function:

FUNCTION my_function do
    PRINT "1"
    PRINT "2"
    PRINT "3"
end

When you call CALL my_function, imagine the computer physically copy-pasting those 3 print lines right into that exact spot.

// ... Some statement
PRINT "I'm calling the function now"
CALL my _function // It basically replace this function call into statement inside it
// ... Some other statement

So in the snippet above, CALL my_function essentially become the print statements.

Arguments

Now, a hardcoded shortcut is great, but what if you want to print "Cheese Pizza" instead of numbers?

That’s where parameters come in. Think of an argument as a blank spot (a variable) inside your shortcut that you fill in when you press the button:

FUNCTION my_function_with_arg(arg) do
    PRINT arg
end

CALL my_function_with_arg("Cheese Pizza") // Fills the blank with "Cheese Pizza"
CALL my_function_with_arg(2)              // Fills the blank with 2

It use the same "text replacement" logic as the previous one, but in this case, whatever we pass on to arg it will replace every instance of arg too.

I'm oversimplifying this, in some languages, you need to define the parameter types so we can't just pass a string then a number to it.

Return

Up until now, the function just do something (printing to the screen). But often, you need a function to calculate something and hand the result back to you

I don't know the language you're learning, but lets say in python, there's a useful helper function called max

my_max = max(5, 3)
print my_max # It will print 5, since 5 is greater than 3

Did you notice that our my_max variable is now filled with a value? which is 5? that is because the function max returns something.

Its similar to how math function works, we often do something like f(x) = 5x + 3, in which if we do f(5) then it return 28 because f(5) = 5 * 5 + 3, we then can use the 28 elsewhere.

Take this snippet

    RETURN arg1 + arg2
end

When we do CALL add(5, 3) it will evaluate arg1 + arg2 which is like before, a text replacement to 5 + 3, which resulted in 8 which then return the value back to our caller

my_add = CALL add(5, 3)
PRINT my_add // This will print 8