Scope, local functions and recursion
Scope determines where a name can be used. Local functions are helpers declared inside a containing member or block. Recursion occurs when a function calls itself, and requires a stopping condition plus progress toward it.
using System;int total = 4;if (total > 0){ int doubled = total * 2; Console.WriteLine(doubled);}Console.WriteLine(total);doubled belongs to the if block. total belongs to the surrounding scope.
Expected output (reasoned, not executed):
84Block scope and capture
A variable declared within a block is not accessible outside that block. Scope is not the same as lifetime, and a name in scope can still be unavailable before definite assignment. A non-static local function can read or modify an enclosing variable that it captures. A static local function cannot capture enclosing locals or parameters; pass needed values explicitly. Ordinary methods are declared as members of a type.
Trace a recursive call
For nonnegative n, Sum(n) can be defined as zero at n = 0 and n + Sum(n - 1) otherwise. Sum(3) waits for Sum(2), which waits for Sum(1), then Sum(0) returns zero. Results unwind as 1, 3, 6. Each invocation has its own parameters. Recursion consumes call resources; no arbitrary-depth or tail-call optimization guarantee is assumed. Use small bounded inputs or a loop when depth is untrusted.
using System;int rate = 3;Console.WriteLine(Cost(4));rate = 5;Console.WriteLine(Cost(4));Console.WriteLine(ExplicitCost(4, 2));int Cost(int n) => n * rate;static int ExplicitCost(int n, int r) => n * r;Cost reads the current captured rate on each call. ExplicitCost receives its rate from the caller.
Expected output (reasoned, not executed):
12208using System;Console.WriteLine(Sum(5));Console.WriteLine(Sum(0));static int Sum(int n){ if (n == 0) return 0; return n + Sum(n - 1);}For the stated input range 0 through 20, each call decreases n until zero, and the additions fit in int.
Expected output (reasoned, not executed):
150Common mistake and correction
Calling Sum with a negative input violates the stated contract: decrementing would move away from the base case. Validate input before recursing, or implement an iterative version. A base case alone is insufficient if some paths never reach it.
Try it yourself
Implement CountDownTotal(int n) for n from 0 through 10, returning the sum of even positive integers up to n. Use recursion with a base case, and compare with a loop for n = 0, 1, 6, 7.
Next: Classes and instances. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scope, local functions and recursion — knowledge check
Apply the lesson concepts to the following scenarios.
Can code after an if block use a local declared only inside that block?
What changes when the captured rate is reassigned before another Cost call?
What is the base case in Sum(n) for n from 0 through 20?
What does Sum(3) return for the lesson helper?
What can a static local function capture from its containing method?