0% found this document useful (0 votes)
32 views6 pages

CH 5 PF

Uploaded by

rabiasultan24277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

CH 5 PF

Uploaded by

rabiasultan24277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CH 5 INTRO:

1. Modularizing Programs in C

 Definition: Modular programming involves breaking down a program into smaller,


manageable units called functions.
 Advantages:
o Simplifies complex problems.
o Facilitates code reuse.
o Makes debugging and maintenance easier.
 Key Idea: Use prewritten library functions along with user-defined functions for efficient
programming.

2. Math Library Functions

 Purpose: Simplifies mathematical operations using prebuilt functions from <math.h>.


 Examples:
o sqrt(x) – Computes the square root of xxx.
o pow(x, y) – Computes xyx^yxy.
o ceil(x) – Rounds up to the nearest integer.
o floor(x) – Rounds down to the nearest integer.
 Importance in Programming: Avoid reinventing basic functionalities; instead, use these
reliable and optimized functions.

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.

5. Function Call Stack and Stack Frames

 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);
}

10. Scope Rules

 Definition: Determines where a variable can be accessed in the code.


 Types:
o Block Scope: Local to a block, defined by {}.
o File Scope: Accessible throughout the file.
o Global Scope: Available across multiple files (with extern).
 Key Concept: Always minimize the scope of variables to reduce bugs and improve
readability.

11. Secure C Programming

 Focus on using secure random-number generation techniques.


 Avoid predictable patterns in critical applications (e.g., cryptography).
 Example: Use platform-specific APIs for cryptographically secure random numbers (e.g.,
arc4random on macOS).

12. Case Study: Casino Game Simulation

 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

Definitions and Concepts

1. Define modular programming.


2. What is a function prototype, and why is it necessary?
3. Explain the purpose of the function call stack.
4. Define recursion. Provide an example.
5. What is the difference between pass-by-value and pass-by-reference?
6. Explain the term base case in recursion.
7. What is the use of the <math.h> library in C? Give two examples of its functions.
8. Define scope and list its types.
9. What is the difference between automatic variables and static variables?
10. Explain the term storage class and name the storage classes in C.

Function Implementation and Usage

11. Write the general syntax of a function in C.


12. What are the advantages of using functions in a program?
13. What is the purpose of the return statement in a function?
14. What is the role of the extern keyword in C?
15. How does static affect a variable declared inside a function?

Random Numbers

16. What is the purpose of the rand() function in C?


17. How does srand() improve the randomness of rand()?
18. Explain the concept of seeding in random-number generation. Provide an example.

Recursion

19. What are the two essential parts of a recursive function?


20. Give one example each of a recursive function and an iterative function.
21. List two advantages and two disadvantages of 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).

Scope and Storage

24. Differentiate between block scope and file scope.


25. Explain the significance of the register storage class.
26. What is the difference between auto and static storage classes?

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.

You might also like