-1

what does the "return" keyword do? I have tried to understand but I still do not get it.

def calculate_slices(people, slices_per_person):
    return people * slices_per_person
import math
def calculate_pizzas(slices, slices_per_pie):
    return math.ceil(slices / slices_per_pie)
def calculate_slices_left(slices_per_pie, pizzas, slices):
    total_slices = slices_per_pie * pizzas
    return total_slices - slices
def main():
    people = int(input("How many people?: "))
    slices_per_person = float(input("How many slices per person?: "))
    slices = calculate_slices(people, slices_per_person)
    slices_per_pie = int(input("How many slices per pie?: "))
    pizzas = calculate_pizzas(slices, slices_per_pie)
    print("You need", pizzas, "to feed", people, "people")
    slices_left = calculate_slices_left(slices_per_pie, pizzas, slices)
    print("There will be", slices_left, 'leftover slices')
main()
khelwood
  • 55,782
  • 14
  • 81
  • 108
Arav Taneja
  • 31
  • 1
  • 12

2 Answers2

2

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

Darkknight
  • 1,716
  • 10
  • 23
2

Return "sends back" a result from a function to the caller of the function.

Consider these two similar, but different functions

def f():
    a=4
    return a

def f2():
    a=4

print(f(),f2())

This yields:

 4, None
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28