Need Assistance?

support@tutorialshub.in

Collections and Strings

Comprehensions

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Functions create reusable behavior

A function groups a piece of logic behind a meaningful name. Good functions have a clear purpose, explicit inputs and a predictable return value. They are one of the main tools for preventing duplicated code.

Parameters and return values

Parameters describe what a function needs, while return sends a result back to the caller. A function that reaches the end without a return statement returns None.

Keep responsibilities small

A calculation function is easier to test when it does not also read input, print output, access a database and send an email. Separate those responsibilities when the application grows.

Practice: write a reusable discount calculation function and test normal, zero and invalid discount values.

ADVERTISEMENT