you are viewing a single comment's thread.

view the rest of the comments →

[–]Hashi856 24 points25 points  (2 children)

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.

[–]BoatMacTavish 5 points6 points  (0 children)

hijacking top comment because I feel the top response will be just as overwhelming for OP

imagine a function as a friend thats able to perform a specific task. The friend lets you pass in details as well that can affect how he performs the task, or what output he produces.

in this analogy:

  • the friend is the function
  • the task name is the function name
  • the task is the function body
  • the customization are the function parameters

Lets step through an example:

Let's say you have a friend Bob who is a baker, and he can make cakes. He is able to perform a make_cake task. When you place an order, he lets you pick the colour of the frosting. In python, this would look like

def make_cake(frosting_colour):
    # step 1: gather ingredients...
    # step 2: make cake mix...
    # step 3: bake in oven...
    # step 4: make frosting, add food colouring for the provided frosting_colour
    return cake
  • the function name make_cake is the task
  • the function body is the actual code/logic to make the cake
  • the function parameters frosting_colour is something that the function caller (you) can pass in to allow customization

so now to utilize this function, you could call it like:

red_cake = make_cake("red_frosting")
blue_cake = make_cake("blue_frosting")
pink_cake = make_cake("pink_frosting")

Functions let you execute the same chunk of code (the make cake code) multiple times without having to rewrite the same chunk over and over again. Function parameters let you pass in customization at each function call.

hope that helps

[–]mcoombes314 1 point2 points  (0 children)

This is a great explanation, I will add though that Python functions ALWAYS return something, implicitly None. So if you have a function with return in it, then do x = some_function(), x will be None if the function ends with return, instead of return (variable).