An introduction to the use of Functions in Python
You can pass a parameter value into a function, and a function can also produce a value.
A function can produce a value with the return statement, which will exit a function and optionally pass an expression back to the caller. If you use a return statement with no arguments, the function will return None.
So far, we have used the print() statement instead of the return statement in our functions. Let’s create a program that instead of printing will return a variable.
We’ll create a program that squares the parameter x and returns the variable y. We issue a call to print the result variable, which is formed by running the square() function with 3 passed into it.
def square(x): y = x ** 2 return y result = square(3) print(result)
We can run the program and see the output:
The integer 9 is returned as output, which is what we would expect by asking Python to find the square of 3.
To further understand how the return statement works, we can comment out the return statement in the program:
def square(x): y = x ** 2 # return y result = square(3) print(result)
Now, let’s run the program again:
Without using the return statement here, the program cannot return a value so the value defaults to None.
As another example, in the add_numbers program above, we could swap out the print() statement for a return statement.
def add_numbers(x, y, z): a = x + y b = x + z c = y + z return a, b, c sums = add_numbers(1, 2, 3) print(sums)
Outside of the function, we set the variable sums equal to the result of the function taking in 1 2, and 3 as we did above. Then we called a print of the sums variable.
Let’s run the program again now that it has the return statement:
(3, 4, 5)
We receive the same numbers 3, 4 and 5 as output that we received previously by using the print() statement in the function. This time it is delivered as a tuple because the return statement’s expression list has at least one comma.
Functions exit immediately when they hit a return statement, whether or not they’re returning a value.
def loop_five(): for x in range(0, 25): print(x) if x == 5: # Stop function at x == 5 return print("This line will not execute.") loop_five()
Using the return statement within the for loop ends the function, so the line that is outside of the loop will not run. If, instead, we had used a break statement, only the loop would have exited at that time, and the last print() line would run.
The return statement exits a function, and may return a value when issued with a parameter.
Write a program that uses a function to return a single value from a maths function and prints it on screen.
Write a program that uses parameters to pass 2 integers, adds them together and returns the value to print.
Write a program that asks the user for 2 integers, passes the values as parameters into a function and then returns the sum of the values to print.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.