C Pass-by-Value vs Address, Recursion & 5 Modular Projects
Welcome to Phase 6 (Part 2): C Parameter Passing, Stack Frames, Recursion & 5 Modular Projects Masterclass! Understanding how arguments travel across RAM memory into function boundaries is the defining bridge between intermediate and advanced C programming. In this comprehensive guide, you will master the difference between Call by Value (safe isolated copies) and Call by Address / Reference (in-place memory mutation via pointers), analyze the internal CPU Call Stack lifecycle during recursive self-invocations, and engineer 5 complete real-world modular applications.
1. Pass by Value (Default in C)
Function ki variable copy mathrame velthundhi. Function lopala variable ni change chesina, main() lo unna original variable value change avvadhu!
2. Pass by Address / Pointer (Call by Reference)
Function ki variable เฐฏเฑเฐเฑเฐ **RAM Memory Address (&var)** pass chesthamu. Function pointer *ptr tho direct ga caller memory slot ni modify chesthundhi (e.g. Swapping two numbers)!
#include <stdio.h>
// 1. Pass by Value (Fails to swap in main!)
void wrongSwap(int a, int b) {
int temp = a; a = b; b = temp;
}
// 2. Pass by Address (Successfully swaps caller's RAM memory!)
void realSwap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int x = 10, y = 20;
wrongSwap(x, y);
printf("After wrongSwap: x = %d, y = %d (NO change!)\n", x, y);
realSwap(&x, &y); // Passing memory addresses &x and &y
printf("After realSwap: x = %d, y = %d (Swapped!)\n", x, y);
return 0;
}
Recursion ante oka function thanani thane direct ga or indirect ga call chesukovadam. Every recursive function must have 2 mandatory parts:
- 1. Base Case (Stopping Condition): Recursion infinite loop lo vellakunda terminate chese condition (e.g.
if (n <= 1) return 1;). Missing base case causes a Stack Overflow Crash! - 2. Recursive Step: Problem size ni reduce chesthu smaller input tho self-call cheyyadam (e.g.
return n * factorial(n - 1);).
[ PUSHING STACK FRAMES ] [ UNWINDING & RETURNING ]
โ factorial(1) -> returns 1 (Base Case) โ returns 1
โ factorial(2) -> 2 * factorial(1) โ returns 2 * 1 = 2
โ factorial(3) -> 3 * factorial(2) โ returns 3 * 2 = 6
โ main() โ main() receives 6!
Practical implementation of production-style modular functions across 5 distinct domains:
Projects 1 & 2: Modular Calculator & Student Grading System
#include <stdio.h>
// --- 1. Modular Calculator Library ---
double calculate(double a, double b, char op) {
if (op == '+') return a + b;
if (op == '-') return a - b;
if (op == '*') return a * b;
if (op == '/') return (b != 0) ? (a / b) : 0.0;
return 0.0;
}
// --- 2. Student Grading System ---
char calculateGrade(double avg) {
if (avg >= 90.0) return 'A';
if (avg >= 75.0) return 'B';
if (avg >= 50.0) return 'C';
return 'F';
}
int main(void) {
printf("1. Calculator: 50 * 4 = %.2f\n", calculate(50, 4, '*'));
printf("2. Student Avg 82.5%% -> Grade: %c\n", calculateGrade(82.5));
return 0;
}
Projects 3, 4 & 5: Number Utility Library, Unit Converter & Recursion
#include <stdio.h>
#include <stdbool.h>
// --- 3. Number Utility Library ---
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
long long factorialRecursive(int n) {
if (n <= 1) return 1; // Base case
return n * factorialRecursive(n - 1); // Recursive step
}
// --- 4. Unit Converter Library ---
double celsiusToFahrenheit(double c) {
return (c * 9.0 / 5.0) + 32.0;
}
double kilometersToMiles(double km) {
return km * 0.621371;
}
int main(void) {
printf("3. Number Utility: Is 31 Prime? %s\n", isPrime(31) ? "YES" : "NO");
printf("4. Recursion: Factorial of 6 = %lld\n", factorialRecursive(6));
printf("5. Unit Converter: 100 km = %.2f Miles | 100ยฐC = %.1fยฐF\n", kilometersToMiles(100), celsiusToFahrenheit(100));
return 0;
}
Run this recursive countdown and power calculation program in our online GCC compiler:
#include <stdio.h>
// Recursive power: base^exp
long long power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
int main(void) {
printf("2^8 = %lld\n", power(2, 8));
return 0;
}