Functions and modular programming turn a large program into testable, reusable units. PSC questions often test parameter passing, recursion, scope, stack behavior, cohesion, coupling and why modularity improves software engineering.
Engineering Definitions
Function
Standard definition: A named block of code that performs a task and may return a value.
Exam meaning: नाम भएको reusable code block जसले काम गरेर value फर्काउन सक्छ।
Procedure
Standard definition: A named block of code that performs an action without necessarily returning a value.
Exam meaning: काम गर्ने तर return value आवश्यक नभएको routine।
Modular programming
Standard definition: A design approach that divides software into separate modules with clear responsibilities and interfaces.
Exam meaning: Software लाई clear responsibility भएको modules मा विभाजन गर्ने approach।
Recursion
Standard definition: A technique where a function solves a problem by calling itself on smaller instances.
Exam meaning: Function आफैंलाई smaller problem मा call गरेर solution बनाउने technique।
Concept Teaching
A function is not only syntax; it is an abstraction boundary. Caller should know what the function does, not every internal step. Good modular design has high cohesion inside a module and low coupling between modules.
Why Functions Matter
Functions reduce repetition and isolate logic.
- Reuse common logic.
- Improve readability by naming intention.
- Enable unit testing.
- Support top-down design.
- Hide implementation details.
- Reduce debugging scope.
Parameter Passing
Parameter passing determines whether a function receives a value copy or can modify caller data.
| Mode | Meaning | Exam point |
|---|---|---|
| Call by value | Function receives copy | Caller variable unchanged |
| Call by reference | Function receives alias/address | Caller variable can change |
| Call by pointer/address | Address is passed explicitly | Common in C-style languages |
| Immutable object passing | Object cannot be modified in place | Reassignment differs from mutation |
Scope, Lifetime and Storage
Scope is visibility; lifetime is how long storage exists.
- Local variable visible inside block/function.
- Global variable visible broadly but can increase coupling.
- Static variable may retain value across calls.
- Stack frame stores parameters, return address and local variables.
- Heap stores dynamic objects until freed/collected.
Recursion
Every recursive solution needs a base case and progress toward it.
- Base case stops recursion.
- Recursive case reduces problem size.
- Stack frame is created per call.
- Tail recursion may be optimized in some languages.
- Missing base case causes infinite recursion/stack overflow.
- Recursion is natural for trees, divide-and-conquer and backtracking.
Cohesion and Coupling
These terms often appear in design and software engineering questions.
| Quality | Good design direction | Meaning |
|---|---|---|
| Cohesion | High | Module elements belong together |
| Coupling | Low | Modules depend minimally on each other |
| Interface | Clear/stable | Defines how modules interact |
| Information hiding | Strong | Internal details are protected |
Engineering Mechanism
- Caller evaluates arguments.
- Control transfers to function/procedure.
- Stack frame stores call context.
- Function executes using parameters and local variables.
- Return value/control goes back to caller.
- Modular interface separates what is promised from how it is implemented.
Diagrams / Models To Draw
- Draw function call stack with nested calls.
- Draw parameter passing by value vs reference.
- Draw modular decomposition tree.
- Draw recursion tree for factorial or Fibonacci.
Formulas, Algorithms and Code Patterns
- Factorial recurrence: n! = n x (n-1)!, base 0! = 1.
- Recursive time often follows recurrence T(n).
- Good module design: high cohesion + low coupling.
- Stack depth equals number of active nested calls.
| Concept | Role | Exam trap |
|---|---|---|
| Function | Reusable computation | May return value |
| Procedure | Reusable action | Return value not required |
| Call by value | Copy argument | Caller not modified |
| Call by reference | Alias argument | Caller may change |
| Recursion | Self-call | Needs base case |
| Module | Design unit | Needs clear interface |
Exam Point
- Explain stack frame when discussing recursion.
- Differentiate scope and lifetime.
- Use high cohesion and low coupling phrase in modularity answers.
- Parameter passing examples are high-yield.
- Mention recursion base case and progress.
Worked Example
In call by value, `swap(a,b)` receives copies, so original a and b do not change. In call by reference, the function receives aliases/addresses and can exchange caller variables. This is why swap examples are common parameter-passing tests.
Subjective Answer Pattern
- Define function/procedure.
- Explain parameters, return value and scope.
- Describe stack frame and recursion.
- Explain modularity with cohesion/coupling.
- Conclude with testing/reuse/maintenance benefits.
Common Engineering Mistakes
- Saying recursion is just a loop without stack cost.
- Forgetting base case.
- Confusing scope with lifetime.
- Assuming call by value can modify caller primitive variable.
- Using global variables unnecessarily in modular design.
MCQ Revision
- What is high cohesion?
- What is low coupling?
- Which parameter mode can modify caller variable?
- What stores return address during function call?
- What is recursion base case?
- Where are local variables commonly stored?
Final Summary
- Functions provide abstraction and reuse.
- Parameter passing controls data modification behavior.
- Recursion needs base case and stack awareness.
- Modular programming improves testing and maintenance.
- Good modules have high cohesion and low coupling.