CH 5 PF
CH 5 PF
1. Modularizing Programs in C
3. Defining Functions
Structure:
o Prototype: Declares the function’s return type, name, and parameters (e.g., int
add(int a, int b);).
o Definition: Contains the implementation of the function.
o Call: Invokes the function with appropriate arguments.
Example:
c
Copy code
int add(int a, int b) {
return a + b;
}
Benefits:
o Organizes logic into separate units.
o Makes the code reusable.
4. Function Prototypes
Purpose: Provides the compiler with a "preview" of a function to ensure proper usage.
Syntax:
c
Copy code
return-type function-name(parameter-list);
Advantages:
o Enables type-checking of function calls.
o Allows functions to be defined after their use in the program.
Call Stack:
o A data structure used to manage function calls.
o Each function call creates a stack frame containing:
Function parameters.
Local variables.
Return address.
Mechanism:
o Control passes to the function when called.
o Stack frame is created.
o Once the function returns, its stack frame is removed.
6. Passing Arguments
By Value:
o Copies the value of arguments into function parameters.
o Changes in the function do not affect the original arguments.
By Reference:
o Passes the memory address of arguments.
o Changes in the function directly modify the original variables.
7. Random-Number Generation
Purpose: Generate pseudorandom numbers using the rand() function.
Key Points:
o Without seeding, the sequence of random numbers is the same every time the
program runs.
o Use srand(seed) to seed the random-number generator.
Example:
c
Copy code
srand(time(0)); // Seed with current time
printf("%d", rand());
Applications:
o Simulations.
o Games.
o Cryptography.
8. Recursion
Definition: A function that calls itself directly or indirectly to solve smaller instances of a
problem.
Structure:
o Base Case: Stops recursion.
o Recursive Case: Progresses towards the base case.
Example: Factorial
c
Copy code
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
Use Cases:
o Mathematical computations (e.g., Fibonacci sequence, factorials).
o Tree traversal.
o Divide-and-conquer algorithms (e.g., merge sort).
9. Storage Classes
Types:
o auto – Default for local variables.
o static – Retains variable value between function calls.
o extern – Declares global variables accessible across files.
o register – Suggests storing variables in CPU registers.
Examples:
o Static Variable:
c
Copy code
void count() {
static int counter = 0;
counter++;
printf("%d", counter);
}
Objective: Use functions and random numbers to build a simple casino-style game.
Topics Covered:
o Random number generation.
o Modular function design.
o User interaction and input validation.
This chapter emphasizes structured, reusable programming practices and introduces key concepts
essential for professional software development. Mastering these ideas will be critical for your
exam and practical programming.
QUESTIONS
Random Numbers
Recursion
Math Library
22. What does the function ceil(x) do? Provide an example.
23. Write the usage and purpose of pow(x, y) and sqrt(x).
Short Programs
27. Write a simple program using a function to calculate the square of a number.
28. Implement a function to generate random numbers between 1 and 10.