Print , Function Parameters and 'Return'

The most used build-in fuinction for debugging and another essential thing about functions

This time we talk about 3 things because they are short and they are related to each other a lot.

Homework from Last Time (if you didn't do last lesson , ignore this)

When working with strings you can see that they can only be added and only multiplied.

When you add an int with a float , result is a float and when you make a calculate with all 5 basic operators , you will see that exponent happens first , then division , then multiplication then addition.

If you want to change the order operators execute , try to add parenthesis within the print function , like in math irl

How do we get input into it?

As I said last tutorial, print can take a max number of parameters that's equal to 255, because it uses special stuff we won't cover . When you do print(5+5) you actually put only 1 parameter , when you do print("word " + "word") it's still 1 parameter used.

When we will make functions with parameters we won't make them take up to 255 of them , we are gonna add slots as we need them

Why would I care how many parameters I use in print?

Well let's say you want to print a string and an int in the same parameter , there are ways to get around like print("word" + str(10)) [this outputs word10]

str(<variable>) will try to convert the variable to a string and it will work if some requirements are met. Like str(<variable>) there is int(), float(), bool()

NOTE : You don't have to use < > , I used it as an example

If you want the output to be "word 10" you can change the "word" to "word " , but there's a easier way than using str() and adding a space to the string. Do print("word", 10) . This outputs "word 10" and it's faster and easier to use.

Function Parameters

What are they and why would I care?

Well if you will ever make a function and that function needs to take an existing variable that changes everytime , you should know what parameters are.

Recently as I talked about print , you probably noticed that you give it parameters and the parameters are separated by a ,

How do I make use of them in MY OWN function?

We'll start by defining our function , I hope you got used to indent when making functions , I'll also screenshot to make sure you got it. So you probably know that when you define a function you start by writting def name_of_the_function(parameters): well I don't expect you to know that parameters where the ones that go in the parenthesis . Let's make a function to print it's parameter then we gonna do a little more complex.

When creating parameters , you can also do parameter=<variable> and that will be a default scenario in case the user doesn't input anything there

def nice_name(what_to_print): print(what_to_print)

Now that we defined out function , we will have to call it like this nice_name(variable_we_will_use) , of course instead of variable_we_will_use , we will use a variable we will define before our function. We'll make a variable called i_want_to_print_this = "this is a more than a word"

Now try to run the script and the output will be "this is more than a word " , if it doesnt work for you , take a look at this screenshot:

'Return' inside a function

Why do I need it?

Let's say you want to make a function that won't print the result , but give you back another value for a variable. In that case we will make use of return .

Return can also be used to stop a function and not change anything , just a way to exit a function without quittin' the entire program

How do I use it myself ?

The syntax is pretty simple , but first I'll give you an example that's been used a lot in the return examples. Let's say we need to make a function that will calculate A + B [A and B will be parameters]

We made a function called "add_2_numbers" and we made it need 2 parameters. The function will then stopped and give us the result "a+b"

We can also use this function to make a variable get the new value:

Practice makes PERFECT!

For homework this time , I'll ask you make a function with 3 parameters that will multiply first 2 , then subtract the third one with the result of first 2. The answers will be shown at the start of the next lesson in case you are stuck.

If you add more lines of the code after return , they won't run

You can define variables inside functions and they can be used ONLY inside functions that were made in.

Last updated

Was this helpful?