PSUC Lab 10 — Modular Programming & Call Stack
Active Logic: Functions & Recursion
Master modular functions in C: function signatures, pass-by-value vs pass-by-reference, recursive base cases (Euclidean GCD, Fibonacci), prime generators, and custom sorting callbacks.
Function & Recursion Rules
| Concept | Rule / Formula | Key Trap / Pitfall |
|---|---|---|
| Function Signature | returnType name(params) | Return type mismatch causes implicit conversion warning |
| Base Case | if (b == 0) return a; | Missing base case causes Stack Overflow! |
| Euclidean GCD | GCD(b, a % b) | Calculates greatest common divisor recursively |
| Recursive Fibo | Fibo(n-1) + Fibo(n-2) | Base cases: n == 0 → 0, n == 1 → 1 |
🎯 Learning Philosophy: Active Logic Building
Test your function logic across 3 Interactive Learning Modes:
- 🧠Tab 1 (Logic & Hints): Function call stack & recursion trees.
- 🧩 Tab 2 (Logic Puzzle): Complete missing recursive base cases & call trees!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve function puzzles before inspecting C reference code!
Exercise 1 of 5
Factorial Function Fact(n)
Computes factorial of a number using a dedicated modular function long long Fact(int n).
Modular Function Signature
- Signature:
long long Fact(int n) - Return type
long longprevents 32-bit integer overflow whenn > 12. - Base case / Init:
f = 1; for (i = 1; i <= n; i++) f *= i;
Interactive Logic Hints
💡 Hint 1: What is Fact(0)? ▼
By mathematical definition, 0! = 1! The loop for
n = 0 runs 0 times and returns 1!🧩 Complete Factorial Function
Fact(int n) {
long long f = 1;
for (int i = 1; i <= n; i++) f i;
f;
}
long long f = 1;
for (int i = 1; i <= n; i++) f i;
f;
}
Select statements above and click Check!
🔢 Factorial Function Calculator
Factorial result...
fact_func.cReference C Code
#include <stdio.h>
long long Fact(int n) {
long long f = 1;
for (int i = 1; i <= n; i++) f *= i;
return f;
}
int main() {
int num = 5;
printf("%d! = %lld\n", num, Fact(num));
return 0;
}
Exercise 2 of 5
Recursive Greatest Common Divisor GCD(a, b)
Computes the greatest common divisor of two integers using Euclid's recursive algorithm.
Euclidean Recursion Rule
- Base Case:
if (b == 0) return a; - Recursive Step:
return GCD(b, a % b); - Example:
GCD(48, 18) → GCD(18, 12) → GCD(12, 6) → GCD(6, 0) = 6
Interactive Logic Hints
💡 Hint 1: What is the relation between GCD and LCM? ▼
LCM(a, b) = (a × b) / GCD(a, b)!
🧩 Complete Recursive GCD Function
int GCD(int a, int b) {
if (b 0) return ;
return GCD(b, a b);
}
if (b 0) return ;
return GCD(b, a b);
}
Select statements above and click Check!
🧮 Recursive GCD Visualizer
Recursive stack step trace...
recursive_gcd.cReference C Code
#include <stdio.h>
int GCD(int a, int b) {
if (b == 0) return a;
return GCD(b, a % b);
}
int main() {
int a = 48, b = 18;
printf("GCD(%d, %d) = %d\n", a, b, GCD(a, b));
return 0;
}
Exercise 3 of 5
Prime Checker Function & First N Primes
Uses a modular helper function bool isPrime(int n) to print the first N prime numbers.
Modular Helper Function
- Function:
bool isPrime(int n) - Trial division:
for (i = 2; i * i <= n; i++) if (n % i == 0) return false; - Generates N primes by calling
isPrime(num)in a generator loop.
Interactive Logic Hints
💡 Hint 1: Why isolate isPrime() into a separate function? ▼
Modularity! The main loop stays clean and easily readable:
if (isPrime(num)) print(num);🧩 Complete Prime Helper Function
bool isPrime(int n) {
if (n <= 1) return ;
for (int i = 2; i * i <= n; i++) {
if (n i == 0) return false;
}
return ;
}
if (n <= 1) return ;
for (int i = 2; i * i <= n; i++) {
if (n i == 0) return false;
}
return ;
}
Select statements above and click Check!
🔢 Prime Generator Engine
Primes output...
prime_func.cReference C Code
#include <stdio.h>
#include <stdbool.h>
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;
}
int main() {
int target = 10, count = 0, num = 2;
printf("First %d Primes: ", target);
while (count < target) {
if (isPrime(num)) { printf("%d ", num); count++; }
num++;
}
printf("\n");
return 0;
}
Exercise 4 of 5
Recursive Fibonacci Fibo(n)
Computes N-th Fibonacci term recursively: F(n) = F(n-1) + F(n-2).
Recursive Fibonacci Tree
- Base cases:
if (n == 0) return 0; if (n == 1) return 1; - Recurrence:
return Fibo(n - 1) + Fibo(n - 2);
Interactive Logic Hints
💡 Hint 1: What is the time complexity of naive recursive Fibonacci? ▼
Exponential O(2n) time! Because it recalculates duplicate subproblems multiple times!
🧩 Complete Recursive Fibonacci Base Cases
int Fibo(int n) {
if (n 0) return 0;
if (n == 1) return ;
return Fibo(n - 1) Fibo(n - 2);
}
if (n 0) return 0;
if (n == 1) return ;
return Fibo(n - 1) Fibo(n - 2);
}
Select statements above and click Check!
🌀 Recursive Fibonacci Tree Visualizer
Fibonacci tree status...
recursive_fibo.cReference C Code
#include <stdio.h>
int Fibo(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return Fibo(n - 1) + Fibo(n - 2);
}
int main() {
int n = 7;
printf("Fibonacci(%d) = %d\n", n, Fibo(n));
return 0;
}
Exercise 5 of 5
Custom Name Sorting using Sort() & Compare()
Sorts an array of strings alphabetically using custom modular functions Sort() and Compare().
Callback / Helper Logic
Compare(s1, s2)wrapsstrcmp(s1, s2). Returns> 0if s1 > s2.Sort(names, n)uses Bubble Sort withstrcpy()for string swaps.
Interactive Logic Hints
💡 Hint 1: Can we swap strings using assignment = ? ▼
No! In C, array assignment
s1 = s2 is invalid! You MUST use strcpy(temp, s1); strcpy(s1, s2); strcpy(s2, temp);!🧩 Complete Name Swap Logic
if (Compare(names[j], names[j + 1]) 0) {
char temp[50];
(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
char temp[50];
(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
Select functions above and click Check!
🔤 Alphabetical Name Sorting Engine
Sorted names output...
name_sort.cReference C Code
#include <stdio.h>
#include <string.h>
int Compare(char s1[], char s2[]) { return strcmp(s1, s2); }
void Sort(char names[][50], int n) {
char temp[50];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (Compare(names[j], names[j + 1]) > 0) {
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
}
}
}
int main() {
char names[5][50] = {"Rahul", "Abhay", "Priya", "Rohan", "Ananya"};
Sort(names, 5);
printf("Sorted Names:\n");
for (int i = 0; i < 5; i++) printf("%s\n", names[i]);
return 0;
}
Knowledge Check
Test Your Functions & Recursion Mastery
Question 1 of 5
Loading question...