lesson
State what a function promises
1 · Learn
A function contract describes inputs, expected output and conditions that must hold. A function that returns a value is different from one that only displays it. Clear contracts make components easier to test and reuse.
For a function called square(n), a suitable contract might accept a number and return n × n without changing other data. The name alone does not guarantee the implementation meets that contract.
2 · Worked example
Python def square(n): return n * n returns 25 for square(5). Printing 25 inside a function is not the same as returning 25 to its caller.
3 · Your turn
What should square(-3) return under this contract?
Check your answer
9.
4 · Apply your learning
Write tests for zero, a positive and a negative input, then compare the returned value with the expected one.
Adult guidance and safety
Use an approved sandbox with invented data. Never ask for real passwords, personal records or access to another person's system. These text examples need executable projects and assessed evidence in two languages; reading code alone is not practical mastery.
Practice this lesson
Lesson resources
Browse the available practice activities and worksheets for this lesson.