Example on Unit 4

Hello again!

Could someone explain to me the below example?

def add(a=2,b=2):
    res = a + b
    return res
    
r = add(3,4)
print (r)

I can’t understand why the writer uses the return statement?

Also, I can’t understand the below exercise

from robot_control_class import RobotControl

robotcontrol = RobotControl(robot_name="summit")

def get_laser_values(a,b,c):
    r1 = robotcontrol.get_laser_summit(a)
    r2 = robotcontrol.get_laser_summit(b)
    r3 = robotcontrol.get_laser_summit(c)

    return [r1, r2, r3]

l = get_laser_values(0, 500, 1000)

print ("Reading 1: ", l[0])
print ("Reading 2: ", l[1])
print ("Reading 3: ", l[2])

Hi @vasileios.tzimas,

for the first question:

Why the writer uses the return statement?

In most of the cases when we call a function, we want something as a response. If we ask a calculator to add two numbers, we want to know the result. The value returned by the function is printed to the user.

And for the second question, the get_laser_values(0, 500, 1000) call is basically returning the laser readings in the positions 0, 500 and 1000 of the Summit robot.

Since a list containing the three laser readings is returned, they are printed in order, on indexes 0, 1 and 2 of the value returned.

Oh! thank you very much!