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%

Unit 2 — Functions: Definition and Calling

Lesson 8 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Functions in C — Basics

A function is a self-contained block of code that performs a specific task. Functions make programs modular, reusable, and easier to debug.

---

Types of Functions

  1. Built-in (Library) Functions — provided by C standard library (e.g., printf, scanf, sqrt)
  2. User-defined Functions — written by the programmer

---

Function Components

/* Function Declaration (Prototype) */
return_type function_name(parameter_list);

/* Function Definition */
return_type function_name(parameter_list) {
    /* body */
    return value;
}

/* Function Call */
function_name(arguments);

---

Example: Simple Function

#include <stdio.h>

/* Declaration */
int add(int a, int b);

int main() {
    int result = add(5, 3);   /* Call */
    printf("Sum = %d\n", result);
    return 0;
}

/* Definition */
int add(int a, int b) {
    return a + b;
}

---

Parameter Passing

1. Call by Value

A copy of the argument is passed. Changes inside the function do NOT affect the original.

void increment(int x) {
    x = x + 1;   /* only local copy changes */
}

int main() {
    int a = 5;
    increment(a);
    printf("%d\n", a);   /* still 5 */
}

2. Call by Reference (using pointers)

The address of the argument is passed. Changes inside the function DO affect the original.

void increment(int *x) {
    *x = *x + 1;   /* changes original via pointer */
}

int main() {
    int a = 5;
    increment(&a);
    printf("%d\n", a);   /* now 6 */
}

---

void Functions

Functions that do not return a value use the void return type.

void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

---

Scope of Variables

ScopeDeclaredAccessible
LocalInside a functionOnly in that function
GlobalOutside all functionsAnywhere in the file
BlockInside {}Only in that block
int globalVar = 10;   /* global */

void demo() {
    int localVar = 20;   /* local */
    printf("%d %d\n", globalVar, localVar);
}