Logic behind executing a defined function

Hey
i just have a short question:
Following code is a loop i created to draw contours with a minimum area of 500px.

Why do i have to keep the print function after the loop active so that the loop function actually processes sth? When i delete the print function in last line its just as the program wouldnt process the loop. This makes sense to me because i only define the loop and dont execute it, but is there an other way to execute the loop than having that print function at the end?
This is just so i can simplify my code, it works as it is.

def contour_areas(contours):
    areas = []
    for cnt in contours:
        cont_area = cv.contourArea(cnt)
        if cont_area >= 500:
            areas.append(cont_area)
            if cont_area >= 500:
                cv.drawContours(imgcontours_min_area, contours, -1, (0, 255, 255), 1)
                
                #not minimal rect:
                #x,y,w,h = cv.boundingRect(cnt)
                #cv.rectangle(imgcontours_min_area,(x,y),(x+w,y+h),(0,0,255),2)
                #cv.putText(imgcontours_min_area, "w={},h={}".format(w,h), (x,y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255), 2)
                
                rect = cv.minAreaRect(cnt)
                box = cv.boxPoints(rect)
                box = np.int0(box)
                cv.drawContours(imgcontours_min_area,[box],0,(0,0,255),1)
                (x, y), (w, h), angle = rect
                cv.putText(imgcontours_min_area, "w={},h={}".format(round(w, 2), round(h, 2)), (int(x),int(y-60)), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255), 2) #x, y in integer sonst falscher data type
    return areas

print('Funktion contour_areas vor sortieren:', contour_areas(contours))

Well, you call the contour_areas function from the print(...)! Otherwise the function is simply not called!!!

Pro tip: use a debugger to see why your program is not working :wink:

Ok, so my thought was correct. Is there a “better” way to call the defined function than using the print()?

I’m quite new to programming at all so i often have problems with the basics rather than with more complex things i guess :smiley:

Yes. You call a Python function in the following way:

areas=contour_areas(contour)
# optionally: 
print(areas)

That said, there are better forums for Python beginner questions than this one.

contour_areas(contour)

if you don’t care about the return value…