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:
| Type | Arguments | Return |
|---|---|---|
| Type 1 | No | No |
| Type 2 | Yes | No |
| Type 3 | No | Yes |
| Type 4 | Yes | Yes |
✅ 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
Define function and explain its advantages
What is a function prototype?
Explain call by value and call by reference
What is recursion? Write recursion rules
Define pointer with example
Write a program to swap two numbers using pointers
Differentiate:
Call by value vs call by reference
Function vs recursion
🔍 Difference: Call by Value vs Call by Reference
| Call by Value | Call by Reference |
|---|---|
| Copy is passed | Address is passed |
| Original value unchanged | Original value changes |
| Uses normal variables | Uses pointers |
| Safer | More 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
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.
LinkedIn ProfileRelated Posts
Loading related posts…
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...