Organizing Code with Functions

Overview


By the end of the last part, you created a program that accepted multiple study sessions in one run, reacted to each one, and printed a summary at the end. The program has also started to feel somewhat long. The piece that classifies a session by length, the piece that accumulates the total, and the piece that finds the longest session are all sitting in the same script.

This is the first time the course explicitly asks: is the code organized well? The answer is no. This is fine, as the code had to be written before it could be organized, especially because you did not know what the final shape of the program would be. Now, with a real program in hand, you reorganize it into smaller, named, reusable pieces called functions.

By the end of this part, you will have a good understanding of how to define and call functions, how to use parameters and return values to make them more flexible, and how to test them. You will also have a deeper understanding of the structure of Python programs.

The structure of this part is as follows:

Background Survey

0 / 5 points

Before starting the part, please answer the short background survey below. It helps us understand the backgrounds of students taking the course, improve the course, and examine whether students with different backgrounds are supported equitably.

Your responses will not affect your grade. You may skip any question or select “Prefer not to answer”.

A useful rhythm for this part is:

  1. complete the background survey above,
  2. read the chapters in order,
  3. complete the chapter exercises as you go,
  4. then complete the project checkpoint in Restructuring Your Study Log with Functions,
  5. and complete the recap and feedback chapter.