Skip to main content

Introduction

Functions are the fundamental building blocks of a program. Functions encapsulate logic into independent modules. They isolate errors and allow for code to be reused. In this chapter we will look at functions, why we use them, how to create them and how to use them. We will also look at best practices in function design.

Learning Outcomes

After completing this chapter, students will be able to:

  1. Explain the purpose of functions and describe how they promote code reuse, error isolation, and program modularity
  2. Distinguish between function prototypes, definitions, and calls and apply the concept of declaration before use
  3. Design and implement functions with appropriate parameters and return types to solve specific programming tasks
  4. Trace program execution using the runtime stack, including stack frames, function calls, and variable scope
  5. Apply cohesion and coupling principles to evaluate and improve function design
  6. Organize multi-file programs using header files and source files to separate function prototypes from definitions
  7. Perform walkthroughs of programs with functions by tracking variables, parameters, and stack frames across multiple function calls
  8. Write well-documented functions with clear descriptions of purpose, parameters, assumptions, and return values