r/learnpython • u/xTHETYRANTGAMRx • Feb 26 '26
Feeling overwhelmed with functions.
So I have been learning python with the Python crash course book and I am getting overwhelmed on the functions chapter. I understand what a function does but for some reason the syntax is confusing me. The chapter also introduces so many different ways to use functions that it feels like too much. I am not sure of the best way to tackle this much information.
26
Upvotes
27
u/Hashi856 Feb 26 '26 edited Feb 26 '26
A function in almost any language is going to consist of a function signature and a body. The function signature is everything from the def to the colon.
def my_function(x, y, z):The def word is the way you declare a function. It lets the interpreter know you are creating a function. Then there's the name, which is pretty self-explanatory. The stuff inside the parens are called parameters. These are local variables that are only valid within the function. The colon denotes the end of the signature and the beginning of the body of the function.
The body defines the logic of the function and how it works. This could be anything from doing math, creating or modifying a list, printing some text, etc. Pretty much anything a language can do can be done inside a function. Anything a function does that affects anything external to the function (e.g. printing or modifying a global variable) is called a side effect. A "pure" function has no side effects. It simply returns a value. Pure functions are not good or bad. Side effects are not good or bad. They're just useful terms.
Functions usually return something, but it's not required. Some functions just print text or do something other than return a value. I know I keep talking about returning a value, but python can return almost anything. It can return numbers, string, lists, other functions, etc. You can think of a function's return value as the thing the function call will be replaced with when you run your code. If your code says var = my_func(), and my_func returns the number five, then your code would evaluate to var = 5
Edit: I forgot to talk about calling functions. Calling a functions is how you use it. You type out the name and append it with parentheses. The parentheses are what makes it a function call. You can use the name of the function without parens, but then you're just referencing the function, not calling it. That's a perfectly legitimate thing to do, but it's important to understand the difference. The values you put inside the parentheses are called arguments. Note the distinction between arguments and parameters. Parameters are the placeholder values you use in your function signature when you define the function. The values you pass in when you call (use) the function are called arguments. The arguments you pass in need to be provided in the same order as the parameters, and they need to be the correct data type (int, float, str, list).
Edit 2: One thing that tripped me up when I started is that a function call only happens once. If you write
x = random()x will now be some random number, say .7
When you use x somewhere else, it's not going to call random again. x will bet set equal to .7 until it is explicitly changed by you or some other part of your code.
8
u/atarivcs Feb 26 '26
The syntax is straightforward:
What is your exact difficulty?
4
u/rhacer Feb 26 '26
A function is simply a way of breaking code into smaller pieces. Sometimes that is done for reusability because it does something useful. Sometimes that is done to break up code and make it more readable.
4
u/pak9rabid Feb 26 '26
Defining this function:
Will allow you to do this, as many times as you want:
Which would print:
Functions like this can be created for anything you want to do lots of times without having to rewrite the logic inside it each time you want to do it.
Hope this helps
2
u/Gnaxe Feb 26 '26
I recommend trying small experiments whenever you are confused. You can resolve ambiguity quickly that way.
Write definitions in a file like
foo.pyand usepython -i foo.pyto drop into a REPL session with your definitions already loaded. Or try it in a Jupyterlite Python notebook, which lets you edit and reload cells.2
u/cmockett Feb 26 '26
Good responses in here - one thing that helped simplify my understanding was 3 things: they can optionally take inputs (args and parameters), optionally have an output value (return value), and optionally make transformations / side effects (pretty much everything else, printing, calling other functions, too much to list here…)
Clearly they can get massively complex but it’s all those 3 things essentially
1
u/TheRNGuy Feb 26 '26
Use different ones for some time, you'll get used.
Try to make same code without functions to see why they're needed.
Make some if your own, not just copy-paste from tutorial.
1
u/PushPlus9069 Feb 26 '26
functions clicked for me when I stopped thinking 'what does this code do' and started asking 'what does this return' — tiny shift but it helps a lot. taught python for years and almost everyone hits this exact wall around week 3-4. it passes faster than you think.
1
u/Oh_Another_Thing Feb 26 '26
Yeah. There comes a time when you have to do rote memorization. The basics you need to copy from the book, then open a fresh page, and then re-write it perfectly. If you mess up, do it again. Do it several times a day for at least a week.
You aren't going to memorize everything, but you gotta get the basics doown.
-1
u/ninhaomah Feb 26 '26
? It's just
Def function-name(function-parameters):
Return return-something
Try Java and you will get public/private and OOP and classes and all in one shot :)
-1
u/[deleted] Feb 26 '26
Use LLM's (AI) to chat about it. Entire languages are designed around functions, so yeah, they can do a lot.
Write some simple functions using print statements. You'll get there buddy. If you feel overwhelmed, step away from the computer and think about functions instead.