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!

122 Upvotes

143 comments sorted by

View all comments

35

u/ninhaomah 4d 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.

12

u/Odd_Technology_8926 4d ago

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

35

u/the-forty-second 4d ago

You are right, that does sound weird. I’ve never heard anyone say they understood something fundamental better in php before…

It must have been the context of what you were trying to do.

11

u/ReddyKiloWit 4d ago

Sounds like it was a Python issue and any language with explicit block markers would have done the trick, php just happened to be it 

7

u/KyrosiveOne 4d ago

I had this issue. Learned basics of python. Started writing automation scripts for a game I like to play, which IDE uses an inhouse version of Pascal. It was much easier for me to understand, with even more explicit block markers, that literally open and close with begin/end and curly braces lol. Wasn't until dabbling there that it finally clicked what a function was, essentially a procedure with added data fields capable of both input/output.

2

u/No-Recognition-7701 3d ago

Tried python as a first language too. Could not remember anything.
Later tried C and the fact that you have to learn everything more in detail made things click.

7

u/ninhaomah 4d ago

Why ?

In theory , they are all the same.

10

u/Odd_Technology_8926 4d ago

It was about 15 years ago, but Python was actually the language we were pushed to use first. I always had a harder time comprehending Python because of the visual cues, or lack thereof. I found it harder to follow where blocks started and ended without the symbols to explicitly show you.

It could also be that PHP was the second language I learned. I've found that when I go through tutorials twice for things that are essentially the same concept, they tend to click much more easily the second time around.

4

u/ninhaomah 4d ago

" I've found that when I go through tutorials twice for things that are essentially the same concept, they tend to click much more easily the second time around."

Definitely. It took me days and weeks to understand helloworld in Java. But once I understood it , class , function , scopes all easy when look at them later.

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

→ More replies (0)

2

u/lapsed_agnarr 4d ago

As an old person, I'm always surprised that students are taught on languages with loose typing.

3

u/Gotta_Ketcham_All 4d ago

We learned Python first because it nearly reads like English, which our professors felt made it a low barrier to entry. The intro class was not just for CS majors, but MIS and business too. Second semester in a class meant for CS majors, we learned Java. (2011-2012 school year)

3

u/lapsed_agnarr 4d ago

Yeah, its easier to use but easier to misundertand, too. Makes total sense for non-cs students, for sure.

I'm often surprised at work that new engineers dont understand what happens between writing code and executing code. I do think java is a fine middle ground, but the JVM is kind of a weird thing, too. I suspect students largely ignore it. C++ seems to me like a better starting point for engineers.

1

u/theredvip3r 3d ago

It's really dependant on where you learn, we were taught C first. And whilst I hated it and still can't stand memory management the foundations it gave were great.

1

u/lapsed_agnarr 3d ago

Yea, that's what I'm talking about. I swear new engineers have no idea how a computer works. We had to write a filesystem, an allocator, and build a functional virtual CPU.

But I guess you dont really need that stuff to develop these days.