Skip to content
Unit 4 Syllabus Note· Programming with C Theory

Programming with C

Comprehensive exam revision notes for BCA-103N Unit 4 (Functions, Parameter Passing, Recursion & Scope). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.

Programming with C

Functions, Parameter Passing, Recursion & Scope

Core Definitions & Key Concepts

What is Modular Programming

A software design technique that decomposes a large complex program into smaller, self-contained, independent, and reusable subroutines or functions, improving readability, testing, and team collaboration.

Function Declaration (Prototype) and Function Definition

Prototype: Informs compiler about function name, return type, and parameter list before main() (e.g. int add(int, int);). Definition: Contains the actual executable body code enclosed in braces.

What is the difference between Actual Parameters and Formal Parameters

Actual Parameters: Values or variables passed to the function in the calling statement (add(x, y)). Formal Parameters: Variables declared in the function definition header that receive the incoming values (int add(int a, int b)).

Call by Value with its limitation

C passes copies of actual argument values into formal parameters. Changes made to formal parameters inside the function do NOT affect caller variables in main(). Limitation: cannot modify caller arguments directly.

How does C simulate 'Call by Reference'

By passing memory addresses (pointers) of actual arguments into pointer formal parameters (&x), allowing the function to dereference the pointer (*ptr) and modify the original variable.

Recursion and state the two essential components of a recursive function

Recursion is a programming technique where a function calls itself directly or indirectly. Essential components:

  1. Base Case (Termination Anchor): A non-recursive condition that halts recursion.
  2. Recursive Step: Progressively reduces the problem toward the base case.

What happens if a recursive function lacks a base case

The function calls itself infinitely until the hardware runtime call stack runs out of memory, crashing the program with a Stack Overflow segmentation fault.

Name the four Storage Classes in C

  1. auto (default for local variables, stack-allocated)
  2. register (requests CPU register storage for speed)
  3. static (preserves value between function calls, data segment)
  4. extern (global scope across multiple source files).

What is the difference between Variable Scope and Variable Lifetime

Scope: The region of the source code where a variable is accessible/visible. Lifetime (Duration): The period of runtime execution during which the variable retains allocated memory storage.

What is the advantage of using a static local variable

A static local variable is initialized only once when execution begins and retains its updated value across multiple calls to that function.

Conceptual & Architectural Analysis

Call by Value versus Call by Reference with a Variable Swapping code example

C
1#include <stdio.h>
2
3// 1. Call by Value (FAILS to swap in caller!)
4void swap_by_value(int a, int b) {
5 int temp = a; a = b; b = temp;
6}
7
8// 2. Call by Reference (SUCCEEDS using pointers!)
9void swap_by_reference(int *a, int *b) {
10 int temp = *a;
11 *a = *b;
12 *b = temp;
13}
14
15int main(void) {
16 int x = 10, y = 20;
17 swap_by_value(x, y);
18 printf("After swap_by_value: x = %d, y = %d (Unchanged!)\n", x, y);
19
20 swap_by_reference(&x, &y); // Passing memory addresses
21 printf("After swap_by_reference: x = %d, y = %d (Swapped!)\n", x, y);
22 return 0;
23}

The Memory Call Stack mechanics during Recursion with an Activation Record diagram

Each function call allocates an Activation Record (Stack Frame) on the call stack containing:

  1. Return address in caller code.
  2. Function parameters.
  3. Local variables.
System Architecture / Concept DiagramHorizontal Scroll Enabled
CALL STACK FOR factorial(3):
+-------------------------------+
| factorial(1): n=1 -> returns 1|
+-------------------------------+
| factorial(2): n=2 * fact(1)   |
+-------------------------------+
| factorial(3): n=3 * fact(2)   |
+-------------------------------+
| main(): calls factorial(3)    |
+-------------------------------+

Frames are pushed on each recursive call and popped as base cases return values upwards.

Compare the four Storage Classes in C with a comprehensive comparison matrix

Storage ClassKeywordStorage LocationDefault Initial ValueScopeLifetime
AutomaticautoStack MemoryGarbage (indeterminate)Local block {}Exits block
RegisterregisterCPU Register (if available)GarbageLocal block {}Exits block
StaticstaticData Segment (BSS)Zero (0)Local to block or FileEntire program execution
ExternalexternData SegmentZero (0)Global across all filesEntire program execution

Write a recursive C function to compute Greatest Common Divisor (GCD) using Euclid's Algorithm

C
1#include <stdio.h>
2
3// Euclidean Algorithm: gcd(a, b) = gcd(b, a % b); base case: gcd(a, 0) = a
4int gcd(int a, int b) {
5 if (b == 0) return a; // Base condition
6 return gcd(b, a % b); // Recursive call
7}
8
9int main(void) {
10 int a = 48, b = 18;
11 printf("GCD of %d and %d = %d\n", a, b, gcd(a, b));
12 return 0;
13}

Passing Arrays to Functions in C

In C, arrays are ALWAYS passed by reference (simulated) because the array name decays into a pointer to its first element:

C
1void print_and_double(int arr[], int size) {
2 for (int i = 0; i < size; i++) {
3 arr[i] *= 2; // Directly modifies original array in main!
4 }
5}

In-Depth Analysis & Implementation

Solve the classic Tower of Hanoi problem using Recursion in C with complete algorithm explanation and call trace for N = 3 disks

1. Problem Formulation and Recursive Strategy

  • Objective: Move N disks from Source peg (A) to Destination peg (C) using Auxiliary peg (B).
  • Rules: 1) Move only one disk at a time; 2) A larger disk can never be placed on top of a smaller disk.
  • Recursive Insight:
  1. Move top (N - 1) disks from Source (A) to Auxiliary (B) using Destination (C).
  2. Move the remaining largest N-th disk directly from Source (A) to Destination (C).
  3. Move the (N - 1) disks from Auxiliary (B) to Destination (C) using Source (A).
  • Total Moves Formula: 2^N - 1. For N = 3, moves = 2^3 - 1 = 7.

2. Complete C Implementation

C
1#include <stdio.h>
2
3void tower_of_hanoi(int n, char from_rod, char to_rod, char aux_rod) {
4 if (n == 1) {
5 printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
6 return;
7 }
8 // Step 1: Move n-1 disks from source to aux
9 tower_of_hanoi(n - 1, from_rod, aux_rod, to_rod);
10
11 // Step 2: Move nth disk from source to dest
12 printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
13
14 // Step 3: Move n-1 disks from aux to dest
15 tower_of_hanoi(n - 1, aux_rod, to_rod, from_rod);
16}
17
18int main(void) {
19 int n = 3;
20 printf("Tower of Hanoi Solution for %d disks:\n", n);
21 tower_of_hanoi(n, 'A', 'C', 'B');
22 return 0;
23}

3. Step-by-Step Execution Trace (N = 3)

  1. Move disk 1 from A to C
  2. Move disk 2 from A to B
  3. Move disk 1 from C to B
  4. Move disk 3 from A to C
  5. Move disk 1 from B to A
  6. Move disk 2 from B to C
  7. Move disk 1 from A to C

Recursion vs Iteration in depth: Performance, Memory Overhead, Stack Frames, and write recursive vs iterative Factorial code

1. Comparative Analysis Matrix

FeatureRecursionIteration
Core MechanismFunction calls itself repeatedly until base conditionRepeats loop construct (for, while) until condition fails
TerminationTriggered by base case returnTriggered by loop condition becoming false
Memory ConsumptionHigh memory overhead; allocates new stack frame for each callConstant O(1) memory; reuses existing local variables
Execution SpeedSlower due to function call overhead, stack pushing/poppingSignificantly faster native hardware loop execution
Stack Overflow RiskHigh vulnerability if recursion depth exceeds stack limitsNo stack overflow risk
Code EleganceExtremely clean for tree traversals, divide-and-conquer (Towers of Hanoi, MergeSort)Can become verbose and complex for non-linear structures

2. Code Comparison

C
1// Iterative: O(N) time, O(1) space
2long long fact_iterative(int n) {
3 long long res = 1;
4 for (int i = 2; i <= n; i++) res *= i;
5 return res;
6}
7
8// Recursive: O(N) time, O(N) stack space
9long long fact_recursive(int n) {
10 if (n <= 1) return 1;
11 return n * fact_recursive(n - 1);
12}

Scope, Visibility, and Lifetime of Variables in C: Block Scope, Function Scope, File Scope, and External Linkage

1. Scope Dimensions in C

  • Block Scope (Local): Variables declared inside { } are accessible only within that block.
  • Function Scope: Labels used with goto are visible throughout the entire enclosing function.
  • File Scope (Global): Variables declared outside all functions are visible from their point of declaration through the end of the file.

2. Multi-File Compilation with extern

C
1// File 1: global_data.c
2int global_counter = 100; // Definition & storage allocation
3
4// File 2: main.c
5#include <stdio.h>
6extern int global_counter; // Declaration: informs linker variable exists elsewhere
7
8int main(void) {
9 printf("Counter = %d\n", global_counter);
10 return 0;
11}

Important Exam Points & Quick Revision

In BCA-103N Unit 4, the classic exam questions are: Call by Value vs Reference (with swap code), Tower of Hanoi recursive trace, and the 4 Storage Classes comparison table.

Your reading progress
0%

100% left on this page — finishing it closes the gap for Functions, Parameter Passing, Recursion & Scope.

Weak topic hint

Functions, Parameter Passing, Recursion & Scope still counts as a weak topic until you finish this note and answer its questions.

Practise this unit
What Should You Do Next?

Test Your Understanding

Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.