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 1: Modular Programming

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

The Strategy of Modularization

A function is a self-contained block of code designed to perform one specific task. They are the primary way to organize your code into manageable, reusable modules. Without functions, a large program would be a 10,000-line "spaghetti" mess that no human could understand.

Why use Functions?

  1. Reusability: Write a calculateTax function once, and use it in 20 different places in your app.
  2. Abstraction: You use printf every day, but you don't need to know how it talks to the graphics driver. You only need to know how to call it.
  3. Maintainability: If the tax law changes, you only have to fix the code in one function, not in 20 different files.
  4. Teamwork: One developer can work on the "Input" function while another works on the "Calculation" function.

The Three Pillars of a Function

  1. Declaration (Prototype): Usually at the top of the file. It tells the compiler the function's name, its return type, and what parameters it expects.
  2. Definition: The actual code (body) of the function.
  3. Call: The line in your code that actually triggers the function to run.

Syntax Anatomy

return_type name(parameter_list) {
    // Local variables
    // Logic
    return value; // (If not void)
}

The void Keyword

  • If a function returns nothing (like just printing a message), use void as the return type.
  • If a function takes no parameters, use void in the parentheses.

Scope: Local vs. Global

  • Local Variables: Declared inside a function. They are "private"—no one outside that function can see or change them.
  • Global Variables: Declared at the top of the file. They are "public"—every function can read and modify them. Warning: Avoid globals whenever possible; they make debugging a nightmare!
Naming Rule: Use Verbs for function names (calculateTotal, printReport). Use Nouns for variables (totalPrice, userName). This makes your code read like a story!