A function is a named block of code that does one job. You have already used functions: main, printf, scanf. Now you will write your own.
Why functions?
- Reuse - write once, call many times.
- Readability -
calculateGST(price)explains itself. - Easier debugging - test each small piece separately.
Declaring, defining and calling#
int add(int a, int b); // 1. declaration (prototype)
int main() {
int s = add(3, 4); // 3. call
}
int add(int a, int b) { // 2. definition
return a + b;
}
12 + 30 = 42 Direct: 350
intbefore the name is the return type.aandbare parameters;xandy(or100,250) are the arguments passed in.returnsends a value back to the caller and ends the function.
If you define the function above main, you do not need a separate prototype. In bigger programs, prototypes go in header (.h) files.
void functions and functions without parameters#
======================== Welcome to Vindhyaedu! ------------------------
Call by value#
C passes copies of the arguments. The function can change its copy, but the original stays the same:
Inside function: n = 80 In main: marks = 40
C में function को variable की copy मिलती है (call by value)। Function के अंदर बदलाव करने से main वाला असली variable नहीं बदलता। असली variable बदलना हो तो उसका address (pointer) भेजना पड़ता है - यह हम Pointers वाले lesson में सीखेंगे।
Scope: local and global variables#
- A local variable is declared inside a function or block and exists only there.
- A global variable is declared outside all functions and can be used everywhere (use sparingly).
- A static local variable keeps its value between calls.
normal=1 kept=1 normal=1 kept=2 normal=1 kept=3 totalCalls=3
Recursion#
A recursive function calls itself. Every recursive solution needs:
- a base case - a simple input answered directly (it stops the recursion), and
- a recursive case - which calls the function on a smaller problem.
1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720
How factorial(4) is evaluated:
factorial(4) = 4 * factorial(3)
= 4 * (3 * factorial(2))
= 4 * (3 * (2 * factorial(1)))
= 4 * (3 * (2 * 1)) ← base case reached, now multiply back up
= 24
Each call waits on the call stack until the call below it returns.
5... 4... 3... 2... 1... Go! Sum of digits of 9075 = 21
A recursive function without a correct base case calls itself until memory for the call stack runs out ("stack overflow" / segmentation fault). Also, simple recursive Fibonacci is very slow for big n because it recomputes the same values again and again - a loop is better there.
Library functions: math.h#
C's standard library has many ready-made functions. <math.h> gives sqrt, pow, fabs, ceil, floor, round, sin, cos, log...
sqrt(144) = 12.0 pow(2, 8) = 256 ceil(4.2) = 5 floor(4.8) = 4 fabs(-7.5) = 7.5 hypotenuse = 5.00
On Linux, link the math library with -lm: gcc prog.c -o prog -lm. The online compiler already does this.
Summary#
- A function has a return type, a name, parameters and a body; declare it with a prototype if it is defined after
main. - Arguments are passed by value (copies).
- Local variables live inside their block;
staticlocals keep their value; globals are visible everywhere. - Recursion = base case + smaller recursive call.