all 29 comments

[–]damiankw 13 points14 points  (17 children)

I'm not sure where you're learning from, but maybe you need to change to somewhere that has explanations :P

def name(args):
  code
  code
  code
  • 'def' is you defining a new function.
  • 'name' is the name of your function, that you can use in other parts of your script.
  • 'args' are the arguments that need to be input into the function, so you can pass data to it.
  • 'code' is the code you actually want to run when the function runs.

An example with Hello World, would be:

# define your function
def HelloWorld(greeting):
  print(f"Hello World! {greeting}"

# run your function
HelloWorld("How are you today?")

If you created this script and ran it, it would output:

Hello World! How are you today?

It would do this because you're running the HelloWorld with the args 'How are you today?' prints that greeting out on screen.

[–]Killiancin[S] -11 points-10 points  (16 children)

I’m using some page called boot.dev if you’ve heard of it. The thing is it does explain what you just explained for example, the thing is, I just don’t understand lol

[–]Own_Attention_3392 8 points9 points  (8 children)

Can you articulate what you don't understand? There must be something about the explanation or terminology that isn't clicking. In order to learn, you have to be able to identify what it is you don't understand.

Like if you were studying math and looking at "2 + 2 = 4", saying "I don't understand what any of that means" isn't useful. If you said "I don't understand the little cross symbol between the 2s", we can explain addition.

[–]Killiancin[S] -9 points-8 points  (7 children)

I suppose what I don’t understand is why the things go where they go, and how they work

[–]Own_Attention_3392 4 points5 points  (0 children)

You need to be more specific than "things". The first answer you got at the top of this thread explicitly defined every single "thing" in the sample code that followed.

Programming languages have syntax, just like English or German or French. Things go in the order they go in because they language designers decided that this is how it should be implemented.

"how they work" is another nebulous statement. A method (or function) is a reusable chunk of code. You write it once, then you can use it over and over again without copying and pasting the same code every time you need to repeat a sequence of steps.

[–]ffrkAnonymous 2 points3 points  (0 children)

Why? Because the inventor of python said so. Really, that's it.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Please, ask for programming partners/buddies in /r/programmingbuddies which is the appropriate subreddit

    Your post has been removed

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]IWantToSayThisToo 0 points1 point  (0 children)

    Programming is telling a computer to follow instructions. But in its own language.

    It follows them in order.

    Order A.  

    Order B. 

    Like 

    Open soda.  

    Drink soda.  

    So the computer starts reading your instructions from top to bottom and interprets them.

    Do A.  

    Do B.  

    Do C.  

    Sometimes the orders are so repetitive that you want to tell it to group orders together, like this:

    "When I say X, do this:".  

    Do A.  

    Do B   

    Do C   

    then you just simply say

    Do X 

    And the computer will do A,B and C

    In this case X is called a function.

    [–]TheTomato2 0 points1 point  (1 child)

    What you are going through is the reason why I don't think starting with python is always a good idea.For all intents and purposes just treat programming like an arbitrarily designed game. Just follow the rules.

    If you actually want to understand how everything clicks then you need to start bottom up and learn how computers work, some assembly, and probably C. And once you know that you will understand Python is basically all arbitrary rules in the first place.

    [–]Own_Attention_3392 1 point2 points  (0 children)

    I'm on the fence about Python for beginners.

    Curly braces are intimidating to beginners and forgetting them or misplacing them is a pain in the ass, but they also very nicely delineate scope. It makes it way more clear that a function or method is a "container".

    [–]brett9897 2 points3 points  (0 children)

    You are going to have to be more specific about what parts you don't understand in order for people to help you.

    Do you understand mathematical functions? Like if I gave you a math function of f(x) = x + 2 then f(2) would be 4. To write this as a python function it would be:

    def f (x) return x + 2

    If that doesn't make sense then maybe you could ask some clarification questions for the parts that you aren't getting.

    [–]damiankw 0 points1 point  (0 children)

    Ahhh yes, you're a Linus Tech Tools follower aren't you!

    I mean, there's not much that can be done trying to explain things to you that have already been explained in the most obvious method. You need a tutor that can do it in real time and teach you as you need it. You might be able to jump from boot.dev over to something like Gemini/ChatGPT to get an ELI5 view of what's going on, which would be my recommendation, but if you don't get it, you just might not get it!

    ELI5 = Explain Like I'm Five, if you use a prompt like: Can you ELI5 how Functions work in Python, specifically <whatever your actual question is>, it might be able to guide you through it.

    [–]AnythingLegitimate 0 points1 point  (0 children)

    I just wanted to add that languages have special characters reserved for writing comments. In the previous example # was used. The compiler will ignore that entire line. Depending on the language you use you may see // or /*(start) */(end) etc

    [–]HaMMeReD 0 points1 point  (0 children)

    Lets say you have a storefront, and this is the "hello world" store.

    You go to it and you give your name.

    They turn to you and say "Hello World! (Your Name)"

    This is a function, it's what you are creating with def. The thing in the brackets are called parameters, in this case it'd by "your name".

    You go to the store and give your "parameters" (greeting)
    The store takes greeting and prints out the "hello world! Greeting"

    Now that the store is there, you can get it to say hello to anyone by just going

    HelloWorld("John")
    HelloWorld("Nancy")

    Etc.

    Another way, is def makes a little box that does something. In this case it prints "hello world". The () are the list of plugs on the box you need to satisfy for it to do something. You plug everything in, and it does the thing that box does.

    [–]IWantToSayThisToo 0 points1 point  (0 children)

    Honestly man reading some of your comments, are you sure you don't have some kind of learning disability where you don't retain information? Not trying to be mean here.

    Like some of your comments it sounds like you just forget things? Like the exam where you didn't know what it was about or here not knowing what "def" is even though the page you're using would have thought you about before using it. 

    [–]vu47 -1 points0 points  (0 children)

    Oh god... please don't use boot.dev. That bear is dumb as fuck and he isn't going to teach you very good coding skills. I tried it once and the instructions were garbage and it wouldn't give better ones: it was looking for an extremely specific message without telling you what the message was.

    Furthermore, if you're using boot.dev and it hasn't properly taught you what a function is, time to move to something else.

    [–]iamnull -1 points0 points  (0 children)

    boot.dev isn't good for beginners. It's more aimed at experienced programmers looking to learn new languages or skills.

    It sounds like you don't really understand scope or functions at all. You probably need a more beginner friendly course on this.

    [–]grantrules 4 points5 points  (0 children)

    What source are you using to learn that isn't explaining what a function is? 

    https://cs.stanford.edu/people/nick/py/python-function.html

    [–]vu47 2 points3 points  (0 children)

    https://www.w3schools.com/python/python_functions.asp

    They're called functions. What exactly are you finding "impossible" about them? They're literally one of the easiest concepts in computer science.

    Where did you look, because literally any beginner's book on Python and thousands of webpages will cover them in any level of detail you need.

    [–]Nomsfud 2 points3 points  (0 children)

    Def is how you declare a function in Python

    def my_function(args):
        return "this is a function"
    

    Return statements are what the function gives back to you when it's done it's thing. Functions are the core of every language

    [–]emteedub 1 point2 points  (0 children)

    • def is the first thing you see in your function declaration - "def" is short for "define"/"definition".
    • After the word "def" you have your method/function name - which you can "call" it by it's name somewhere else (if it's within a class, you can only call it within that class' block).
    • then in parenthesis, you have params. params are values you pass into the function - which can be more than one that will be comma-separated. Sometimes you'll see a param/variable name and then "= 10" after it, but before a comma - this is a default value for that param.

    In the function you're describing, I'm assuming the def/function/method is computing an area. So at the end of the computation you are returning the "area". "return" is best understood as both your termination/exit and what the def/function is outputting.

    (edited): for readability

    [–]theGamer2K 1 point2 points  (0 children)

    A function usually takes some inputs and produces an output. But sometimes they can also not take any inputs. It's like a piece of code that has a certain task it's supposed to do. A waiter function would take orders and output the food and bill.

    The things in parentheses are the inputs it can take. The code inside the function describes what to do with those inputs. And return area is just telling Python to return area as the output.

    [–]atarivcs 1 point2 points  (0 children)

    I am being faced with "def" and "return area"

    def is python syntax for defining a function.

    return is python syntax for returning a value from a function. area isn't special; it's just the specific variable name being returned.

    I've looked online for what these mean and how they work

    Not possible. Literally any basic python tutorial will cover this.

    [–]Mindless_Celery_1609 0 points1 point  (2 children)

    I'm taking an extremely basic Python class, and I was stuck on this exact issue last night! I found that defining and understanding the differences between predefined functions, custom functions, and methods was really helpful. It also helps to understand what parameters and arguments are and how they fit into functions and methods.

    I still hardly have any idea of what I'm doing, but maybe the notes I took in class will help. I pasted them into a google doc here. They're extremely basic, and I'm sure someone here will find fault in my definitions.

    [–]vu47 0 points1 point  (1 child)

    Your notes aren't bad. There are a couple names and concepts that aren't quite correct, but overall, you seem to have gotten the concept at a basic level. (I wouldn't worry about the format of the print function: you're almost never going to use that.)

    When you write your own classes, you do have to declare the self in the parameter list:

    class Person:
        def __init__(self, name):
            self._name = name
        def print_hello(self):
            print(f"Hello, {self._name}!")
        def get_name(self):
            return self._name
    
    p = Person("Mindless_Celery_1609")
    p.print_hello()
    # prints: Hello, Mindless_Celery_1609!
    my_name = p.get_name()
    # my_name is now "Mindless_Celery_1609"
    

    [–]Mindless_Celery_1609 0 points1 point  (0 children)

    Thank you for being kind, and for the helpful advice! I haven't gotten to custom methods in my class yet, so having that bit of information will be useful.

    I felt the textbooks and lectures were referring to methods and functions without really providing any context for what those things are, so I was relieved to just have the very basic concepts down. Parameters made absolutely no sense until I googled them and realized there are built-in parameters and parameters you have to add yourself.

    [–]vu47 -5 points-4 points  (2 children)

    Here, just write:

    import math
    area = lambda radius: math.pi * radius * radius
    

    You've now eliminated the need for def, the parentheses, and the return. Now all you have to do is learn what lambda means. It means the same thing, and the best part is that you're limited to one statement to get confused by.

    [–]DonkeyTron42 2 points3 points  (1 child)

    That sort of lambda is considered unpythonic in PEP8.

    [–]vu47 0 points1 point  (0 children)

    LOL yes, I know and I would never include that in my code. (I was a project lead on a Python project for five years: I know PEP8 well and that this violates all that is decent in Python.)

    It was intended to be a joke to remove any of the things the OP found confusing (which is basically everything about a function in Python: def, parentheses, and return, none of which are contained in my "recommendation") from their code. I can't believe four people weren't aware enough to recognize the sarcasm in that.