Programming Ch.5
Chapter 5: C Functions
5.1 Introduction
Real-world programs are often large; to maintain them, they should be constructed from smaller, manageable pieces known as functions.
The divide-and-conquer technique is emphasized to manage program complexity effectively.
5.2 Modularizing Programs in C
Functions are crucial for modularizing C programs.
C standard library includes numerous pre-packaged functions for math calculations, string manipulations, and others.
Good programming practice encourages familiarity with standard library functions instead of re-inventing them.
Standard functions (e.g.,
printf,scanf,pow) hide implementation details, promoting software engineering principles.Each function executes tasks designated by the caller and returns results.
5.3 Math Library Functions
Common mathematical calculations are achievable using math library functions (e.g.,
sqrt).Arguments can be constants, variables, or expressions, and results typically return the data type
double.Essential math functions include:
sqrt(x): square root.pow(x, y): x raised to the power y.fabs(x): absolute value.sin,cos,tan: trigonometric functions.
5.4 Functions
Functions create modular programs with local variables and parameters.
The motivation for using functions includes manageability, reusability, and avoiding code duplication.
Each function should perform one clear task, and names should reflect this to promote clarity.
5.5 Function Definitions
Functions consist of a prototype and definition.
A function prototype must match the definition; improper prototypes lead to compilation errors.
The syntax of a function definition includes the return type, function name, and parameter list.
Example definitions:
int square(int y): Returns the square of y.int maximum(int x, int y, int z): Returns the maximum of three integers.
5.6 Function Prototypes: A Deeper Look
Prototypes facilitate type checking and error prevention in function calls.
Good practice includes prototypes for better code maintenance and fewer errors.
5.7 Function Call Stack and Stack Frames
The call stack is essential for maintaining function calls, return addresses, and local variable information.
Stack frames are created upon the calling of a function and are removed when functions return.
Stack overflow errors can occur if there are too many nested function calls.
5.8 Headers
Each standard library has associated headers that contain function prototypes necessary for use.
Programmers can create custom headers to organize their functions effectively.
5.9 Passing Arguments By Value and By Reference
Arguments can be passed by value (copy) or by reference (direct access).
Pass-by-value avoids accidental changes to variables in the calling function, while pass-by-reference allows for modifications.
5.10 Random Number Generation
Randomness can be introduced in applications using
rand()from<stdlib.h>.To achieve specific ranges, scaling and shifting are used with
rand().For secure applications, use secure random number generators.
5.11 Example: A Game of Chance; Introducing enum
The
enumtype provides a means to create meaningful constant names, simplifying code readability.Used in games (e.g., craps) to indicate game status.
5.12 Storage Classes
Identifiers in C have different storage classes (auto, register, extern, static) affecting their duration and linkage.
Automatic duration applies to local variables; static variables retain values between function calls.
5.13 Scope Rules
Scope defines where identifiers are accessible; includes function scope, file scope, block scope, and function-prototype scope.
Hiding variables in inner blocks can lead to errors if not managed properly.
5.14 Recursion
Recursive functions call themselves directly or indirectly, solving problems by handling base cases and simplifying the problem.
Example: Factorial calculation using recursion demonstrates self-referential calling.
Caution with recursion as it can lead to exponential complexity if not managed properly.
5.15 Example Using Recursion: Fibonacci Series
Fibonacci sequence is defined recursively, illustrating natural occurrences and relationships.
Recursive Fibonacci functions showcase rapid growth in call count and potential performance issues.
5.16 Recursion vs. Iteration
Both techniques involve repetition; recursion does so through function calls while iteration uses loops.
Recursive solutions can be easier to understand even with added overhead costs.
5.17 Secure C Programming
The standard library lacks secure random-number generation; alternative platform-specific implementations are encouraged.
These notes provide an in-depth overview of functions in C programming, covering modularity, math libraries, recursion, and function management practices.