C Notes
Learn how to define functions in C: syntax, return types, parameters, function body, local variables, and the complete anatomy of a function definition with practical examples.
A function definition is where you write the actual code that executes when the function is called. While the declaration tells the compiler "this function exists," the definition tells it "this is what the function does." Every function you create must have exactly one definition — it's the heart of the function containing all the logic.
Anatomy of a Function Definition
return_type function_name(parameter_list) {
// Local variable declarations
// Statements (function body)
return value; // (if non-void)
}Each component has a specific role:
| Component | Description | Example |
|---|---|---|
| Return type | Type of value the function returns | int, float, void |
| Function name | Identifier used to call the function | calculateSum |
| Parameter list | Input variables (formal parameters) | (int a, int b) |
| Function body | Code block inside { } | Statements, logic |
| Return statement | Value sent back to caller | return result; |
#include <stdio.h>
// Complete function definition with all parts labeled
int calculateSum(int num1, int num2) { // return_type name(params)
int sum; // Local variable
sum = num1 + num2; // Function body
return sum; // Return value to caller
}
int main() {
int result = calculateSum(15, 25);
printf("Sum = %d\n", result);
return 0;
}Sum = 40
Return Types
The return type specifies what kind of value the function sends back to its caller:
Integer Return Type
#include <stdio.h>
int absolute(int n) {
if (n < 0)
return -n;
return n;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
printf("|-7| = %d\n", absolute(-7));
printf("max(10, 25) = %d\n", max(10, 25));
return 0;
}|-7| = 7 max(10, 25) = 25
Float/Double Return Type
100°C = 212.0°F 2.5^4 = 39.0625
Void Return Type (No Return Value)
--------------- | Hello World | --------------- --------------- | C Functions | ---------------
Pointer Return Type
Larger value: 20 (at address 0x7ffd5e8a3c48) Found 'W' at: World
Parameter Lists
No Parameters
#include <stdio.h>
#include <time.h>
int getCurrentHour(void) {
time_t now = time(NULL);
struct tm *local = localtime(&now);
return local->tm_hour;
}
void greetUser(void) {
int hour = getCurrentHour();
if (hour < 12)
printf("Good morning!\n");
else if (hour < 17)
printf("Good afternoon!\n");
else
printf("Good evening!\n");
}
int main() {
greetUser();
return 0;
}Good afternoon!
Multiple Parameters
Area: 17.60 ******************** Average: 86.5
Local Variables and Scope
Variables declared inside a function are local — they exist only within that function and are destroyed when the function returns:
Call 1: 1 Call 2: 1 Call 3: 1 Inside function: x = 100
Function with Multiple Return Statements
A function can have multiple return statements — control leaves the function at the first return encountered:
85 marks → Grade: B 55 marks → Grade: F Found 30 at index 2
Complete Practical Example
=== Geometry Calculator === Triangle (base=6, height=4): Area = 12.00 Circle (r=5): Area = 78.54, Circumference = 31.42 Distance from (0,0) to (3,4) = 5.00 === Prime Check === 2 3 5 7 11 13 17 19
Best Practices for Function Definitions
- One function, one task — Each function should do exactly one thing
- Meaningful names —
calculateTax()is better thancalc() - Keep functions short — If it's over 30-40 lines, consider splitting
- Consistent style — Same naming convention, same brace placement
- Document complex logic — Add comments for non-obvious algorithms
- Validate inputs — Check for edge cases (null pointers, zero divisors)
Interview Questions
Q1: What happens if a non-void function doesn't have a return statement?
It's undefined behavior. The function will return a garbage value — whatever happens to be in the register used for return values. Modern compilers will warn about this. Always ensure all code paths in a non-void function return a value.
Q2: Can a function definition appear inside another function in C?
No. Unlike some languages (like Python or JavaScript), C does not support nested function definitions. All functions must be defined at the file level (global scope). Some compiler extensions (like GCC) allow it, but it's not standard C.
Q3: What is the difference between formal parameters and actual parameters?
Formal parameters are the variable names in the function definition (e.g., int add(int a, int b) — a and b are formal). Actual parameters (arguments) are the values passed during the function call (e.g., add(5, 3) — 5 and 3 are actual). Actual values are copied into formal parameters.
Q4: Can a function return multiple values in C?
Not directly. A function can return only one value. To return multiple values: (1) use pointers as parameters, (2) return a struct containing multiple fields, or (3) modify global variables (not recommended).
Q5: What's the maximum number of parameters a function can have in C?
The C standard guarantees at least 127 parameters are supported. However, if you need more than a handful of parameters, it's a code smell — consider passing a struct instead to group related data.
Summary
- A function definition contains the return type, name, parameters, and body (code)
- The body is enclosed in
{ }and contains local variables and statements - Return type can be any data type:
int,float,void, pointers, structs, etc. voidmeans no return value; usereturn;(without value) to exit early- Local variables are created on each call and destroyed on return
- A function can have multiple
returnstatements for different code paths - Functions cannot be nested inside other functions in standard C
- Follow the "one function, one task" principle for clean, maintainable code
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Function Definition in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, functions, function, definition, function definition in c programming
Related C Programming Topics