5.1 - Functions (2026-09-21)
Overview
Chris Bourke introduces C functions as reusable units of code that accept typed parameters and return at most one value, showing how they reduce repetition and hide implementation details. Using a loan-payment program, he demonstrates prototypes, definitions, return statements, documentation comments, naming conventions, and how function calls transfer control; he also explains why `main` returns exit codes such as 0 for success.
Key takeaways
- A C prototype must declare a function's return type, name, and parameter types before its first use, allowing the compiler to recognize calls even when the definition appears later.
- Extracting repeated calculations such as multiplying by 100, rounding, and dividing by 100 into `roundToCents` prevents duplication and gives the operation a clear, testable unit.
- A well-designed loan-payment function can accept an annual rate, principal, and years, then handle rate conversion, years-to-months conversion, payment calculation, and cent rounding internally.
- Function documentation belongs with the prototype and should state the function's behavior rather than restating its implementation; documenting both prototype and definition needlessly repeats information.
- Compile C programs with `-Wall` and `-Wextra` to catch problems such as missing return statements in non-void functions and unused variables.
- Program exit codes communicate success or failure to shells, scripts, and graders: 0 indicates success, while nonzero values can identify an error condition.
Chapters
- Students can submit one hack and one lab for reconsideration during Fix-It Week, but must get each submission checked off.
- The course has four exams plus a final; the lowest of the four exams is dropped, while the final is not.
- Chris Bourke directs students to CampusWire and help hours for announcements, exam review, and score questions.
- A function is a reusable unit of code that may accept inputs and produce an output, making it a useful unit for testing.
- C examples already used include `sin`, `sqrt`, `pow`, `printf`, `scanf`, `atoi`, and `main`.
- A function can take multiple inputs or none, but returns at most one value; a circle fails the vertical line test and is a relation, not a function.
- Putting repeated cent-rounding logic in `roundToCents` avoids repeating multiply-by-100, `round`, and divide-by-100 calculations.
- Functions support the DRY principle: write logic once and reuse it instead of copying it into multiple places.
- Procedural abstraction hides implementation details, so callers can use `sqrt` without knowing its numerical method.
- Before solving a problem from scratch, check whether a standard-library function already handles it; library implementations are tested and optimized.
- A C prototype is a function signature followed by a semicolon, and must appear before the function is used.
- A signature specifies the function name, return type, and the number and types of its parameters.
- The loan example declares `roundToCents` with a `double` return type and a `double` amount parameter.
- Descriptive parameter names such as `amount`, `dollars`, or `currency` communicate more than a generic name like `x`.
- A definition repeats the prototype signature without the semicolon and adds a body enclosed in braces.
- The `roundToCents` body scales the amount by 100, calls the math-library `round` function, then divides by 100 and returns the result.
- Compiling with `-Wall` and `-Wextra` exposes issues such as an unused result variable or a non-void function that reaches its end without returning.
- Document each prototype with a `/** ... */` comment that explains what the function does, not how it does it; do not duplicate that documentation at the definition.
- The monthly-payment function returns a `double` and takes an annual rate, principal, and integer number of years.
- Names such as `computeMonthlyPayment` or `getMonthlyPayment` use an action verb, unlike the noun-only name `monthlyPayment`.
- The interface assigns rate conversion and years-to-months conversion to the function, keeping those responsibilities out of the caller.
- The prototype documentation describes the returned monthly loan payment and can link to an amortization reference.
- The payment implementation converts annual rate to monthly rate by dividing by 12 and converts years to months by multiplying by 12.
- It stores the calculated payment in a `double`, rounds the result to the nearest cent with `roundToCents`, and returns it.
- Replacing complex calculations in `main` with a call to `getMonthlyPayment` makes the loan program easier to read while preserving its output.
- Execution passes from `main` to the payment function and then to rounding functions, with control returning to each caller when the nested call finishes.
- Chris Bourke recommends lower camel case and verb-based function names, such as `getMonthlyPayment`; he contrasts this with uppercase camel case and underscore-separated styles.
- Calling or invoking a function interrupts sequential execution, and the function returns control to the calling function when it completes.
- A `void` function has no return value, while `main` conventionally returns 0 for success and a nonzero status such as 42 for an error.
- Shell commands expose exit statuses: `ls` can return 0 on success, while a nonexistent path can return 2; automated graders also use program exit codes to assess results.
Transcript excerpt
0:00 Hopefully it's working. Uh anyway, so thank thanks again to our speakers. Uh for those on the line that missed it, make sure that you're going to the career fair this Tuesday, Wednesday, and Thursday because there are going to be a lot more companies than just uh Fast Enterprises there. And let me get this other one up there. All right. All right. So, what we're going to be starting for uh some other administrative stuff, if you're not paying attention to the announcements on campus wire, make sure that you are because this is fix it week. uh you have the opportunity to uh submit one uh hack and one lab for reconsideration. The rules for doing that and it's not just turn it in. You have to go and get it checked off. All that information is on campus wire. So don't don't ignore campus wire. Uh second, we already posted the results of
0:47 the first exam. Not too great. Uh they didn't mention they didn't uh really track with the u uh the hack in the lab. Uh so again take this as an opportunity to reexamine how you're approaching the course. You can always come by help hours any time to go over what you might have missed or with the content of the exam. And also uh something that uh that uh people also have been ignoring on campus wire is the fact that now that we're we're past the first exam now we're going to announce what we do with them. There are four exams plus the final. Final is there period. Uh but of the four exams we drop your lowest one. So, if you didn't do too well on the first exam, uh then you're not sunk for the rest of the semester. Uh but if if
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.