Unit II: Functions and Program Structure

Course: Programming with C
Code: CADSM101

Table of Contents

Defining and Accessing Functions

A function is a self-contained block of code that performs a specific task. Using functions helps in modularizing code and promoting reusability.

Passing Arguments and Data Types

Functions can receive data through arguments. In C, you must specify the data type for each argument passed to a function.

Function Prototypes and Return Types

A Function Prototype tells the compiler about the function's name, return type, and parameters before the actual definition is provided.

Storage Classes

Storage classes define the scope, visibility, and lifetime of variables within a C program.

  • Lifetime
  • Storage Class Keyword Location
    Automatic auto Stack (Memory) Local to the block.
    External extern Data Segment Global; persists for the entire program.
    Static static Data Segment Value persists between function calls.
    Register register CPU Register Fast access; local to the block.

    Scope Rules and Block Structure

    The scope refers to the region of the program where a variable is accessible.

    Recursion in C

    Recursion is a technique where a function calls itself to solve smaller instances of the same problem.

    Example: Factorial of n is n * Factorial(n-1).

    The C Preprocessor and Header Files

    The preprocessor processes the source code before it is passed to the compiler.

    Exam Focus & Tips


    Frequently Asked Questions

    Q: What is a function prototype?
    A: It is a declaration of the function that informs the compiler about the return type and arguments before the function is called.

    Q: What is the difference between auto and static variables?
    A: auto variables are destroyed when the function ends, while static variables keep their value for the next call.