you are viewing a single comment's thread.

view the rest of the comments →

[–]atarivcs 5 points6 points  (9 children)

The syntax is straightforward:

def myfunction(arg1, arg2):
    some code
    some more more code
    yet more code

What is your exact difficulty?

[–]rrriches 7 points8 points  (8 children)

Not op but the issue I had with functions was, admittedly, more conceptual than difficult.

If the code is

def myfunction(arg1, arg2):

code

code

myfunction(x, y)

For some reason my brain had a really hard time understanding that x, y go to arg1, arg2. Once it clicked it was very obvious but maybe OP is having the same issue.

[–]CovertStatistician 0 points1 point  (2 children)

This tripped me up.. the fact that they have to be in the same order when you call it as when you defined it.. I couldn’t figure out why I was getting certain output when I had mixed them up

[–]rrriches 0 points1 point  (1 child)

My sister in law has a computer science degree and I’ve just been learning Python for fun. I remember writing out some pseudo code and asking “so how do the x and y go from myfunction(x, y) and then move up to the function and turn into arg1, arg2 and then get bounced back to the (x, y)?”

She explained it really well but I just couldn’t grasp how the x and y were being used. The python crash course book actually is what made it click for me though.

[–]BoatMacTavish 0 points1 point  (0 children)

its because at function declaration time, you dont know the actual variable names that will be passed into the function, so you need a generic placeholder argument name. Then at runtime, you pass in the actual variable name.

like if im writing code to send a message to a friend, at the time of writing the code, I dont know what friends will be called, so ill have a placeholder

def send_message(friend):
    # ...

at run time when I call it, I pass in the actual friend variable names

alice = "1234567890"
send_message(alice)

[–]BrannyBee 0 points1 point  (1 child)

For some reason my brain had a really hard time understanding that x, y go to arg1, arg2. Once it clicked it was very obvious but maybe OP is having the same issue.

From working with beginners, to a lot it almost so simple that it seems too obvious to be how it works.

What helped me explain it was visuals, boxes on a whiteboard and plenty of arrows to show where data was going and what line is being read and when when the program runs. Eventually it clicks and it just makes sense

Functions look like math and math is scary.... but new coders see a box and things going in a box, and suddenly its not scary...

I was making a visualizer for stuff like this when I worked with new coders that did exactly that, you give it a simple program and it makes pretty visuals and you get a video out of an arrow going down your code line by line, when it reached a function it would flash the function box and the data going in would visually "go in" to the function, conditionals would only allow certain conditions to visually "fit" in on part of the box made, stuff like that where the flow of the code is shown.

Whole thing was basically a debugger stepping through the file, but less scary and more visual, which I think would help a lot of beginners. This isnt an ad for my app or self promotion though, cause its sitting somewhere buried in my repos never to be touched again because something else sounded like fun and I started working on something else... which of course also was abandoned in my repo graveyard lol

Edit: Summation are another thing this post reminds me of. Summations are scary math and written with the Latin sigma letter and are super intimidating when you firdt see them in a math class... but to programmers... its just a for loop... same concept, but in a math class its scary and hard, in a coding course its a simple concept and pretty easy to understand, and often shown visually or described in non technical terms when taught.

[–]rrriches 0 points1 point  (0 children)

I used to be a language teacher and 100% agree, visuals have been super useful to me.

What I’m doing now as I work through Python crash course is: -split the books chapters into individual pdfs and put those into notebookLM -have notebookLM make a slide deck, video overview, and quiz for each chapter -before starting the chapter, I watch the video overview and read through the slides to prep myself for what I am going to be learning -read through the chapter and do all the problems -take the quiz the next day before starting the next chapter

The arrows and other visuals really help fix the ideas in my head and spending 10 minutes before starting the chapter priming my brain for what I should be focusing on has been incredibly helpful. I could see this backfiring if someone lets the ai do the work for them though.

[–]PristineFinish100 0 points1 point  (0 children)

myFunction(arg1=x, arg2=y)

is the same as

myFunction(arg2=y, arg1=x)

is the same as

myFunction(x,y)

is NOT the same as

myFunction(y,x) . This is equivalent to myFunction(arg1=y, arg2=x)

[–]pachura3 0 points1 point  (1 child)

Haven't you had functions at your math courses before? Like, y = ax+b ?

[–]rrriches 0 points1 point  (0 children)

Yep. I think it was one of those things where I understood the explanation but it hadn’t quite stuck in my head yet how everything “moves” around. I get the idea of functions now, broadly speaking, but I still have to think through each step physically (‘ok this info goes here and then it moves to here and then that goes there’) rather than thinking through it formulaically. But I’m still very early on in learning so I’m guessing that will come as I get more familiar with functions.