Unit 8: Functions and Pointers in C Programming | B. Sc Year: III

🧭 Introduction

As programs grow larger, writing everything inside the main() function becomes confusing and difficult to manage. Also, sometimes we need programs where data is shared efficiently, and memory is handled directly.

To solve these problems, C provides two powerful concepts:

  • Functions – to divide a program into smaller, reusable parts

  • Pointers – to work directly with memory addresses

Unit 8 is one of the most important conceptual units in C programming and is frequently asked in exams, both theory and programming.


🔑 Key Concepts (Explained in Simple Language)


1️⃣ What is a Function?

A function is a block of code that is written once and can be used many times.

Why functions are used:

  • Reduce code repetition

  • Improve readability

  • Make debugging easier

  • Support modular programming

Example (real life):

  • Calculator buttons: add, subtract, multiply

  • Each button works like a function


2️⃣ Types of Functions in C

C provides two main types of functions:

✅ Library Functions

  • Predefined functions

  • Provided by header files

Examples:

printf(), scanf(), sqrt(), strlen()

✅ User-Defined Functions

  • Written by programmers

  • Perform specific tasks

Examples:

sum(), factorial(), area()

3️⃣ Components of a Function

A function has three parts:

🔹 Function Declaration (Prototype)

Tells the compiler:

  • Function name

  • Return type

  • Parameters

int sum(int, int);

🔹 Function Definition

Contains actual code.

int sum(int a, int b) {
    return a + b;
}

🔹 Function Call

Used to execute the function.

total = sum(5, 3);

4️⃣ Types of User-Defined Functions

Based on parameters and return value:

TypeArgumentsReturn
Type 1NoNo
Type 2YesNo
Type 3NoYes
Type 4YesYes

Exam tip: This classification is commonly asked.


5️⃣ Call by Value and Call by Reference

🔹 Call by Value

  • Copy of variable is passed

  • Original value remains unchanged

void change(int x) {
    x = 10;
}

🔹 Call by Reference

  • Address of variable is passed

  • Original value changes

void change(int *x) {
    *x = 10;
}

Difference between these two is a very important exam topic.


6️⃣ Recursion

A function that calls itself is called a recursive function.

Example: Factorial using Recursion

int factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}

Why recursion is used:

  • Simplifies problems like factorial, Fibonacci

  • Makes code shorter

⚠️ Must have:

  • Base condition

  • Recursive call


7️⃣ What is a Pointer?

A pointer is a variable that stores the address of another variable.

Declaration:

int *p;

Address operator (&)

p = &x;

Dereference operator (*)

printf("%d", *p);

8️⃣ Uses of Pointers

Pointers are used to:

  • Implement call by reference

  • Pass arrays to functions

  • Improve performance

  • Dynamic memory allocation

  • Work with structures and files

✅ Heavily pointed in exams.


9️⃣ Pointer Example (Simple)

#include <stdio.h>

int main() {
    int x = 10;
    int *p;
    p = &x;

    printf("Value of x = %d\n", x);
    printf("Address of x = %p\n", p);
    printf("Value using pointer = %d", *p);

    return 0;
}

🔟 Swapping Two Numbers Using Pointers

#include <stdio.h>

void swap(int *a, int *b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 10;
    swap(&x, &y);
    printf("x=%d y=%d", x, y);
    return 0;
}

✅ Classic exam-favorite program.


📝 Important Exam Questions

  1. Define function and explain its advantages

  2. What is a function prototype?

  3. Explain call by value and call by reference

  4. What is recursion? Write recursion rules

  5. Define pointer with example

  6. Write a program to swap two numbers using pointers

  7. Differentiate:

    • Call by value vs call by reference

    • Function vs recursion


🔍 Difference: Call by Value vs Call by Reference

Call by ValueCall by Reference
Copy is passedAddress is passed
Original value unchangedOriginal value changes
Uses normal variablesUses pointers
SaferMore efficient

🎯 Exam Tips / Key Points to Remember

  • Always write a function prototype

  • Recursive function must have a base case

  • Use * carefully (most common mistake)

  • Pointer stores address, not value

  • Use diagrams when explaining pointers

  • Swapping using pointers gives full marks


✅ Short Summary

In Unit 8, you learned:

  • What functions are and why they are important

  • Types and components of functions

  • Call by value and call by reference

  • Recursion and its rules

  • Pointers and their usage in C

This unit strengthens your understanding of program structure and memory handling, which is crucial for advanced topics like arrays, structures, and file handling.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers