Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Functions are the building blocks of any well-structured C program, and you're being tested on much more than just syntax. Exams will challenge you to demonstrate understanding of scope, memory management, modularity, and program flow—all of which revolve around how functions work. When you write a function, you're making decisions about where data lives, how it's passed, and what gets returned. These decisions directly impact your program's efficiency, readability, and correctness.
Don't just memorize how to write a function declaration—know why you'd choose pass-by-value over pass-by-reference, when recursion makes sense versus iteration, and how scope affects variable accessibility. These conceptual connections are what separate students who can read code from those who can write and debug it effectively.
Before you can use a function, the compiler needs to know it exists. This section covers the foundational syntax that tells C what your function looks like and what it does. The distinction between declaration and definition is about separating the "what" from the "how."
main() to allow the compiler to type-check function calls before seeing the full definitionfunctionName(arg1, arg2)Compare: Declaration vs. Definition—both specify the function signature, but only the definition contains executable code. If an exam asks you to "declare" a function, don't write the body; if it asks you to "define" one, include everything.
Every function communicates back to its caller in some way—either by returning a value or by explicitly returning nothing. The return type is a contract between the function and the code that calls it.
int, float, char, etc.return statement sends a value back to the caller and immediately exits the function; the value must match the declared typevoid return type indicates the function performs an action but doesn't send data back—think of it as a one-way communicationreturn; can exit a void function early, but no value follows the keywordCompare: int functions vs. void functions—both can modify program state, but only int (or other typed) functions can be used in expressions like x = getValue();. FRQs often ask you to choose the appropriate return type for a given task.
How data moves into and through functions is critical for understanding program behavior. The mechanism you choose—value or reference—determines whether the original data can be modified.
int *ptr)—the function can directly modify the original variableCompare: Pass-by-value vs. pass-by-reference—if a function needs to swap two variables' values, pass-by-value won't work because the originals remain unchanged. This is a classic exam question: "Why doesn't my swap function work?"
Where you declare a variable determines where it can be accessed and how long it exists. Scope is about visibility; lifetime is about memory allocation.
Compare: Local vs. global variables—local variables are safer because their scope is limited, but global variables persist across function calls. Exam tip: if asked about "side effects," global variables are usually the culprit.
These topics build on the basics and appear frequently in more challenging exam questions. Recursion and library functions demonstrate the power and flexibility of C's function system.
printf(), scanf(), strlen(), sqrt(), malloc()#include <stdio.h> for I/O, #include <math.h> for math functions, #include <string.h> for stringsCompare: Recursive vs. iterative solutions—both can solve the same problems, but recursion is more elegant for naturally recursive structures (trees, factorial) while iteration is more memory-efficient. If an FRQ asks for "efficiency," consider whether recursion's stack overhead matters.
| Concept | Best Examples |
|---|---|
| Declaration vs. Definition | Function prototypes, header files, forward declarations |
| Return Types | int, float, char, void, type matching |
| Parameter Passing | Pass-by-value (copies), pass-by-reference (pointers) |
| Scope | Local variables, global variables, block scope |
| Function Invocation | Function calls, argument passing, control flow |
| Recursion | Factorial, Fibonacci, base cases, stack usage |
| Standard Library | printf(), scanf(), strlen(), sqrt(), malloc() |
What's the difference between a function declaration and a function definition, and when would you use each separately?
If you write a function to swap two integer values and it doesn't work, what's the most likely cause—and how would you fix it using pass-by-reference?
Compare local and global variables: which is safer for large programs, and why might global variables cause "side effects"?
A recursive function keeps running forever. What's missing, and how does the base case prevent infinite recursion?
You need to calculate the square root of a number in your program. Would you write your own function or use a standard library function? What header file would you include?