Python #13: Recap on Custom Functions

Functions are first defined with a ‘def’, followed by a name, then with parameters (or ‘arguments’, they’re the same) wrapped in parentheses.

Whatever is in the function body is meant to be executed. In order to execute the function, you have to call it. To call the function, you type its name along with all the arguments. The only case you don’t have to call all the arguments is if the one you’re not calling has a default value.

If you want to print something from within a function, you can use the ‘print’ keyword in the body so you don’t have to use it again to execute it. All you do then is call the function.

Again, since we already have the ‘print’ inside the function’s body, we don’t need to ‘print’ again to get it to show in the output. Calling the function will execute that ‘print’ for us.

A function allows us to change a bunch of things at once. Let’s say I want that string to print multiple times, and instead of using the function, I just printed that string by itself. If I wanted to change that exclamation point into a period, I’d have to go in and change each string into that period. In the real world there can be hundreds of the same string.

The flip way of doing this would be to use a ‘return’ statement.

Ignore the ‘print’ for a minute. One overwhelming aspect of programming in general is you’re not always gonna get direct feedback on what you’re doing. So with this ‘return’ statement and its string, if I didn’t have the ‘print’ there and thus with the output, there would be nothing telling you that you did this right. There’s no real tip for this, you just gotta used to working in the dark. Understand as long as your ‘return’ is there, the function is holding whatever you put into it.

So that’s working with strings. For basic arithmetic, I pretty much got it. The use of ‘result’ is one thing that tripped me up.

For this basic multiplication problem, returning the equation by itself will still run. So ‘result’ is unnecessary in this case. To go over this again, this also works if you have multiple arguments,

just make sure you have a value for all the arguments. This code would run as an error because there is no value for ‘c’. The only way we could make this run without adding a third value to those prints is to give ‘c’ a default value. We’ll give it 23.

It’s like 2 older siblings who want to go out but they have to bring the baby along. The default value is a toy for the baby so it doesn’t feel left out. Even if I added a third value to those prints, since ‘c’ has its default value, that third value will still be ignored.

And remember, even if we remove the prints and leave the function calls, it will still compute the equations, you just won’t see it in the output.

When it comes to returning a variable within a function, there are 2 ways:

The first one sets the variable to call the function and assign a value to its argument, which is “Alice” and “Bob” to ‘name’ in this case. In the second way, we directly print the function call and those values. The output is the same for both ways. The second way looks much cleaner, but we can’t completely dismiss the first way. I don’t yet know how, but it may be the better method for a certain situation.

There’s also a third way where we could define the names in separate variables, then assign the function to those variables:

This way actually ignores the ‘name’ argument and lets the top variables take its place.

I know I’m going in circles. The point of me doing this is to show there are multiple ways of getting the same result. Again, just because one takes less typing, it doesn’t mean the other ways are useless.

We’re gonna do another 2-method problem that involves using a built-in function with a custom function.

Our goal for this is to print the length of the string “Alice” multiplied by itself. Since “Alice” is 5 letters, that’ll be 5*5 which is 25. The first code returns the argument ‘number’ multiplying it by itself. It then leaves the body and sets a variable ‘length’ to equal the length of the “Alice” string, using the ‘len’ function. Finally, it calls the function with the ‘length’ variable which computes the multiplication. With the second code, the function is defined to perform the length equation. The first code, we do it like it was given as an extra instruction. In the second code, the function is defined like the equation was the main instruction.

With these examples, it seems as if you can either perform your operation in the function’s body, and just call it, or define the function, then do all the work outside the body. I seek to understand why it would be one way and not the other, outside of readability.

So this is my little recap of Python functions. It’s essential to everything that follows with this language. My big takeaway from this was familiarizing myself with different ways of getting the same result. I know there’s a lot more we can do with functions, this is just me getting reacquainted with them.

Leave a comment