Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Functions Part 2: Parameter Passing

Lesson 21 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

How Data is Transferred

When you pass a variable to a function, C needs to decide how to handle that memory. There are two primary techniques, each with its own specific use cases.

1. Call by Value (The Safe Default)

A copy of the variable's value is created and handed to the function.

  • The function works on this "clone."
  • Changes made inside the function do not affect the original variable in the caller.
  • Analogy: You give someone a photocopy of your document. They can scribble on it, but your original document stays clean.
void changeValue(int n) {
    n = 100; // Only changes the clone
}

2. Call by Reference (Using Pointers)

Instead of a copy, the memory address of the variable is passed to the function.

  • The function uses a pointer to "look at" the original memory spot.
  • Changes made inside the function will affect the original variable.
  • Analogy: You give someone the key to your house. Whatever they change inside the house will be there when you get home.
void changeValue(int *n) {
    *n = 100; // Changes the actual variable in main!
}

Which one should I use?

FeatureCall by ValueCall by Reference
Data SafetyHigh (Original is safe)Low (Original can be changed)
Memory SpeedSlower for large dataFaster (Only 8 bytes sent)
Best forSmall types (int, char)Arrays and large Structures

Returning Multiple Values

In C, a function can only return one value using the return statement. If you need to "return" three things, you must pass three variables by reference and have the function update them.

Strictly speaking, C is always "Call by Value." In "Call by Reference," you are simply passing the value of the memory address! It's a subtle but important distinction in computer science.