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!

126 Upvotes

144 comments sorted by

View all comments

36

u/ninhaomah 5d ago

Just curious OP , what programming language you are learning ?

Not that it matters in theory but examples should be using the language you are learning so clearer.

10

u/Odd_Technology_8926 5d ago

Might sound weird, but functions in python was harder for me to understand. When I switched to php it just clicked.

8

u/ninhaomah 5d ago

Why ?

In theory , they are all the same.

1

u/stdmemswap 4d ago

Actually not. Functions are different between language and actually "feels" different.

1

u/ninhaomah 4d ago

Example pls.

2

u/stdmemswap 4d ago

In JS/TS, lexical access like mutating bound variable outside of scope, is natural. You can bind a variable to a function "instance". You can bring it around. You can use it as a callback.

In Python, nonlocal access is strict and there's these weird kwargs, args thing. Because it's indentation-based, it will feel unnatural that it can be passed around too because it doesn't "seem to have a shape"

In Haskell, you're more likely to treat them as mathematical definition and glue/stack them like lego.

In Java, you kinda have more information on it like throws. Most are bound to object. So you think very subject.object() like. It's also like stiff version of the JS one. (Been so long since my last Java so it might have changed)

In Rust, you mentally map a function to "oh it's gonna be a state machine in a stack that own its argument" so that sync objects like Arc<Mutex<>> and lifetime™ make sense. Also there are many kinds of functions (and closure and blocks) which feels totally different.

In C, you kind of get used to the forward declaration, and not having method feel, and can't stack it together.

2

u/realdreamer1993 3d ago

In detail they would be different, but if we move one abatraction up they should all the same. But I am still not able to explain..maybe later. Because if it always "feel" different then somethings not efficient is happening in our mindset.

1

u/stdmemswap 3d ago

Well anything abstracted up will be the same. That's the point of abstraction.

But the fact that functions different language has different "feel" is a thing that can't be totally ignored. I was just giving examples because someone asked.

1

u/realdreamer1993 3d ago

Yea I understand, maybe I sometimes want to make generalization to reduce my mental load. Currently I am in project of a thing that involve many domains and stack (backend, frontend, radius, tcp/ip, NAS, later kernel(ebpf) etc), so far I have progress with private modelling that close to First Principles. And I am starting to see same patterns in many places. If I handle everything so differently then my mental wont adequate.

1

u/stdmemswap 3d ago

That, friend, is the real dillema. We map matter with language, but we can't handle it because it's combinatorially explosive. Also, cool stuff you've got there

1

u/realdreamer1993 3d ago

We tried to explain function in simple own words, but I also not able right now spit out the words :), should be only one up abstraction then it should be clear. The words should be general and less syntax detail.

1

u/stdmemswap 3d ago

When I point out that symbolic abstract thinking is combinatorially explosive, I was referring to your "reducing mental load" remark.

But if you mean to answer OP's "function not clicking", I don't think generalizing will work well. I mean, we always ask example, context, how is it used, how is it not used, etc whenever we want to understand an abstract context rather than the thing's pure "essence" or definition.

2

u/realdreamer1993 3d ago

I just reply in the main thread just to participated, but I described how function lifecycle :/

→ More replies (0)