Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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

I am new to python. I am trying to understand the return statement but I am failing to do it?

Can anyone help to understand the return statement?

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()

1 Answer

0 votes
by (36.8k points)

Return is used to send back the results for that particular function. Lets us consider these 2 examples with different functions.

def f():

    a=4

    return a

def f2():

    a=4

print(f(),f2())

You will get the output:

 4, None

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Related questions

Browse Categories

...