Return Values
Returning Results from Procedures
Part of: Variables, Lists & Procedures
A procedure that only prints is a dead end, the result vanishes to the screen and the program cannot reuse it. A return value sends a result back to the caller so computation can continue. return hands a value back When Python reaches return, the procedure ends immediately and the call expression becomes that value. So square(6) literally turns into 36 wherever it appears. A procedure with no return hands back the special value None. Return vs. print This distinction is the most important idea in the unit: - print(x) shows x to the user, then forgets it. - return x gives x back to the program so you can store it, add to it, or pass it on. Returning from a loop Procedures often compute over many inputs and return one answer. A procedure that finds the maximum of three numbers compares and returns: The AP pseudocode form is PROCEDURE maxOfThree(a, b, c) { ... RETURN(largest) }, identical parameter-in, value-out shape. Why it matters Return values let procedures compose: the output of one becomes the input of another. total = sum(filter(parse(data))) only works because each procedure returns something usable. Returning rather than printing is what turns isolated procedures into buildi
Challenge: Max of Three