5.4 - Functions (2026-09-28)
Overview
Chris Bourke reviews informal unit testing in C, improving a hand-built test suite with diagnostic failures, pass/fail totals, and corner cases, then previews CMocka and its automated test reports. He explains how function calls create stack frames, why C passes arguments by value, and how pointers and memory addresses enable functions to modify caller data and support later topics such as dynamic memory.
Key takeaways
- Informal C unit tests should verify expected outputs independently, print inputs and both outputs on failure, and report passed and failed totals; otherwise, incorrect expectations can create false results.
- Floating-point test comparisons need a chosen tolerance, such as $0.01 for currency, and percentage calculations must use floating-point division to avoid integer truncation.
- CMocka automates assertion and reporting boilerplate, while a Makefile can build and run a full test suite; the demonstrated color tests convert RGB values such as steel blue's 70, 130, 180 to CMYK.
- Every C function call creates a stack frame for its arguments and local variables; returning removes that frame, so its locals do not persist.
- C passes ordinary arguments by value, meaning a swap function that modifies only its parameters changes copies, not the caller's variables.
- Pointers hold memory addresses, and passing an address with `&` enables caller-visible changes and dynamic heap allocation; uninitialized or invalid pointers can cause crashes or unsafe memory access.
Chapters
- After the opening music, Chris Bourke recaps unit tests as input and expected-output pairs checked against a function's actual output.
- Expected results should be verified independently, such as with a calculator or by hand, before they are used to judge code.
- Related test cases can be grouped into a suite that reports how many passed or failed.
- The round-to-cents tester reports the input, actual output, and expected output when a test fails, making errors easier to debug.
- Counters for passed and failed cases produce a summary; the pass percentage must use floating-point division to avoid integer truncation.
- The finance utilities library is compiled separately with GCC's `-c` flag, then linked with the tester and the math library.
- Round-to-cents cases include 123, zero, and a negative half-cent value, illustrating that expected behavior for negatives must come from the specifications.
- The monthly payment test uses a 5% annual rate, a $10,000 principal, and five years, with an expected result of $188.71.
- A tolerance of $0.01 determines whether floating-point results pass, so test inputs and expected values must be checked independently to avoid false results.
- CMocka handles repeated testing boilerplate, including comparisons, counters, and failure reporting; installation and use are covered in Lab 6 and Hack 6.
- The color-conversion example tests RGB inputs against expected CMYK values, including steel blue at RGB 70, 130, 180.
- A Makefile builds the test program and runs its collection of cases; students extend provided examples rather than writing CMocka tests from scratch.
- The framework uses function pointers, which Chris notes have not yet been covered at this point in the course.
- A program call stack follows last-in, first-out behavior: calling a function pushes a frame, and returning pops it.
- The plate-stack analogy explains `push` and `pop`: new plates go on top, and the most recently added plate is taken first.
- In the `main` to `average` to `sum` example, each function call gets a frame containing its arguments, local variables, and return information.
- In the average-and-sum example, `main`, `average`, and `sum` each have separate stack frames, even when their variables share names.
- A function can access its own locals and parameters, but same-named variables in another frame are distinct.
- Returning destroys the function's frame and its local variables, which demonstrates how function-level scope works.
- The swap example starts with `a = 10` and `b = 20`, then changes the function's local copies using a temporary variable.
- The local values become 20 and 10, but the caller's original values remain 10 and 20 after the function returns.
- C passes ordinary arguments by value, so changing parameters inside `swap` does not change the caller's variables.
- Memory consists of addresses and the contents stored at those addresses, compared to a house address and what is inside the house.
- A regular variable such as an `int` represents stored contents; a pointer variable stores an address that can refer to those contents.
- The memory illustration contrasts a one-byte character, an eight-byte `double`, and a typically four-byte integer.
- An uninitialized pointer has an unknown value; dereferencing an invalid address can cause a segmentation fault or unsafe access.
- Initializing a pointer to `NULL` gives the program a value it can check before attempting to use the pointer.
- The address-of operator `&` makes `p = &a` point to integer `a`, while Python Tutor visualizes the pointer referring to `a`'s memory location.
- Chris connects `&` to `scanf`, which needs a variable's address to store input.
- Passing addresses will let a future `swap` function modify the caller's variables instead of changing copies.
- Pointers can let functions fill multiple outputs and provide a way to report errors through output parameters.
- Dynamic memory is allocated on the heap and accessed through pointers; Chris previews using it for large data such as a billion integers.
- The lesson closes by noting that the stack is limited to a few megabytes and that pointers enable direct memory access with greater responsibility.
Transcript excerpt
0:03 [music] [music] [music] [music] >> She's not there. >> [music] >> This part of this side >> [music] >> She is new. >> [music] >> Can we show our faces now?
0:51 >> [music] [music] >> I'm not in >> [music] >> She's [music] not >> [music] >> She's for us. >> [music] >> Can we show our faces >> [music] >> in the sky? >> [music] >> I am blind. >> [music]
1:41 >> Why can't I hear? >> [music] >> Show me >> [music] >> Speak in a phrase. >> [music] [music] [music] [music] [music] [music]
2:33 [music] >> All right, welcome back. Uh just to review what we were doing last week since we did have that exam review on Friday. We were talking we we'd of course introduced functions and using functions and creating functions. Uh but then we went on to unit testing. And what unit testing is is testing your functions, treating them as units and giving it input and then it produces output and you check that against an expected output. And if if that's the input output pair. And they have to be determined to be correct before you you actually run your test. Otherwise, you're not actually testing your code. Uh what we mean by that is by pulling out a calculator, doing it on a
3:21 piece of paper or up on a whiteboard or some other means other than your code to make sure that that expected output is correct. Uh if they match, the unit passes. If they fail, then or if it does not match, then the unit fails. And then we can group multiple unit tests together in one collection and we can call that a test suite. Then we can run the entire test suite in all in one shot. Uh and
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Chris Bourke.