C Language: Functions - Study Notes
1. Introduction to Functions
Functions help in organizing code, improving reusability, and maintaining clarity. Syntax: ReturnType
FunctionName(ParameterList);
2. Function Declaration / Prototype
This is a forward declaration that tells the compiler about the function name, return type, and parameters.
Example: int add(int, int);
3. Function Definition
Contains the body/logic of the function.
Example:
int add(int a, int b) {
return a + b;
4. Function Call
Calling a function executes its body.
Example:
int result = add(3, 4);
5. Return Statement
Used to return a value from the function.
Example:
return a + b;
C Language: Functions - Study Notes
6. Passing Parameters
Call by Value: Copies of variables are passed.
Call by Reference: Pointers are used to modify the original variables.
Example (Reference): void change(int *x) {
*x = 10;
7. Scope of Variables
Local: Declared inside a function/block.
Global: Declared outside all functions and accessible everywhere.
8. Storage Classes
Determine the scope, lifetime, and visibility of variables.
Types: auto, register, static, extern.
Example: static int count = 0;
9. Recursive Functions
A function that calls itself. Must include a base case.
Example:
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);