Day 6 Progress
0%
Day 6  ·  1 Hour

Functions — Define & Call

Write code once, use it everywhere. Functions are the building blocks of every real C program — master them completely.

0–15 min · Function Basics
15–30 min · Parameters & Return
30–44 min · Scope
44–55 min · Prototypes
55–60 min · Recursion Intro
1

What is a Function & Why Use One?

0 – 15 min

A function is a named, reusable block of code. Instead of writing the same logic 10 times, write it once as a function and call it whenever needed.

  • Reusability — write once, use many times
  • ReadabilitycalculateArea() is clearer than raw formulas everywhere
  • Maintainability — fix a bug in one place, fixed everywhere
  • Testing — test each function independently

Function anatomy — every part explained

int
① Return type
what it gives back
 
add
② Name
what you call it
(
int a, int b
③ Parameters
data passed in
)
{
return a+b;
④ Body + return
what it does
}
functions_basic.c
C
#include <stdio.h>

// Function 1: returns sum of two ints
int add(int a, int b) {
    return a + b;
}

// Function 2: prints a greeting — returns nothing (void)
void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

// Function 3: returns larger of two numbers
int maxOf(int x, int y) {
    if (x > y) return x;
    return y;
}

int main() {
    int result = add(8, 5);         // call add, store result
    printf("8 + 5 = %d\n", result);   // 13

    greet("Ananta");                  // call greet

    printf("Max = %d\n", maxOf(14, 9)); // 14

    return 0;
}
terminal
output
8 + 5 = 13
Hello, Ananta!
Max = 14
💡 void means no return value. If a function only does something (prints, modifies) without giving back a calculated value, use void as the return type and omit the return statement (or write return; alone).
2

Parameters & Return Types — Passing Data

15 – 30 min

Parameters are the inputs a function receives. Return type is the type of value it gives back. C passes parameters by value — the function gets a copy, not the original.

Return TypeWhat it MeansExample
voidReturns nothingvoid printMenu()
intReturns a whole numberint square(int n)
floatReturns a decimalfloat average(float a, float b)
doubleReturns precise decimaldouble area(double r)
charReturns a characterchar getGrade(int marks)
params_return.c
C
#include <stdio.h>

// Returns char grade based on int marks
char getGrade(int marks) {
    if (marks >= 90) return 'A';
    if (marks >= 75) return 'B';
    if (marks >= 50) return 'C';
    return 'F';
}

// Returns area of circle using double
double circleArea(double radius) {
    return 3.14159 * radius * radius;
}

// Multiple parameters — returns average
float average(float a, float b, float c) {
    return (a + b + c) / 3.0;
}

int main() {
    printf("Grade for 85: %c\n",  getGrade(85));
    printf("Area of r=5:  %.2f\n", circleArea(5.0));
    printf("Average:      %.1f\n", average(80, 90, 70));
    return 0;
}
terminal
output
Grade for 85: B
Area of r=5:  78.54
Average:      80.0
⚠️ C passes by VALUE — not by reference! When you pass int x to a function and change it inside, the original x is NOT changed. The function works on a copy. To change the original, you must pass a pointer (covered in Day 3).
scope & prototypes
3

Scope — Where Variables Live

30 – 44 min

Scope defines where a variable can be accessed. In C there are two kinds:

  • Local variable — declared inside a function, only visible inside that function. Destroyed when the function returns.
  • Global variable — declared outside all functions, visible everywhere in the file. Lives as long as the program runs.

Scope — where each variable is visible

Global scope
int count = 0;  ← visible everywhere
main() — local scope
int x = 10;  ← only inside main()
add() — local scope
int result;  ← only inside add()
scope.c
C
#include <stdio.h>

int count = 0;   // GLOBAL — visible in all functions

void increment() {
    count++;         // can access global count
    int local = 99; // LOCAL — only inside increment()
    printf("count=%d, local=%d\n", count, local);
}

int main() {
    int x = 10;   // LOCAL to main

    increment();   // count becomes 1
    increment();   // count becomes 2
    increment();   // count becomes 3

    printf("Global count = %d\n", count);
    printf("Local  x     = %d\n", x);
    // printf("%d", local);  ERROR — local not visible here!

    return 0;
}
💡 Prefer local variables. Global variables are accessible everywhere which can lead to confusing bugs when multiple functions change them unexpectedly. Use globals only when truly needed (e.g. a counter shared by many functions).
4

Function Prototypes — Declare Before Using

44 – 52 min

C reads code top to bottom. If you call a function before it is defined, the compiler complains. A prototype is a one-line declaration at the top that tells the compiler "this function exists and here is its signature — I'll define it later."

prototype.c
C
#include <stdio.h>

// PROTOTYPES — declared at top, defined below main
int square(int n);
double power(double base, int exp);

int main() {
    printf("5 squared = %d\n",    square(5));      // 25
    printf("2^10      = %.0f\n", power(2.0, 10)); // 1024
    return 0;
}

// DEFINITIONS — below main, compiler already knows about them
int square(int n) {
    return n * n;
}

double power(double base, int exp) {
    double result = 1.0;
    for (int i = 0; i < exp; i++)
        result *= base;
    return result;
}
💡 Alternative — define functions ABOVE main. If you define all functions before main(), no prototype is needed. Prototypes are essential only when functions are defined after main() or in separate files.
5

Recursion — A Function Calling Itself

52 – 58 min

Recursion is when a function calls itself. Every recursive function needs two things: a base case (when to stop) and a recursive case (the call to itself with a smaller input). Without the base case, it recurses forever and crashes — stack overflow.

recursion.c
C
#include <stdio.h>

// Factorial: 5! = 5 × 4 × 3 × 2 × 1 = 120
int factorial(int n) {
    if (n == 0 || n == 1)    // BASE CASE — stop here
        return 1;
    return n * factorial(n - 1); // RECURSIVE CASE
}

// Sum: 1 + 2 + 3 + ... + n
int sumN(int n) {
    if (n == 0) return 0;          // BASE CASE
    return n + sumN(n - 1);        // RECURSIVE CASE
}

int main() {
    printf("5! = %d\n",       factorial(5));  // 120
    printf("Sum 1-10 = %d\n", sumN(10));      // 55
    return 0;
}
terminal — how factorial(5) works step by step
trace
factorial(5)
  → 5 × factorial(4)
       → 4 × factorial(3)
            → 3 × factorial(2)
                 → 2 × factorial(1)
                      → returns 1   ← BASE CASE reached
                 → 2 × 1 = 2
            → 3 × 2 = 6
       → 4 × 6 = 24
  → 5 × 24 = 120  ✓
⚠️ Always have a base case! Without it, the function calls itself infinitely, fills up the call stack, and the program crashes with a "stack overflow" error. Every recursive function must have a condition that stops the recursion.
practice & quiz
Q

Quick Quiz

58–60 min
Question 1 of 5

A function with return type void means:

Question 2 of 5

You pass int x = 5 to a function and change it inside. What happens to the original x?

Question 3 of 5

A local variable declared inside a function is:

Question 4 of 5

What is a function prototype?

Question 5 of 5

What is the base case in recursion?

Lesson Checklist

  • I understand the 4 parts of a function: return type, name, parameters, body
  • I can write a function that returns int, float, char, or void
  • I understand C passes parameters by value (copies)
  • I know the difference between local and global variables
  • I know why to prefer local variables over global
  • I understand function prototypes and when they are needed
  • I can write a recursive function with a base case and recursive case
  • I understand what stack overflow means in recursion
  • I completed the quiz

Day 7 Preview

Coming up next
  • 🧵 Strings deep dive — fgets, string.h, custom functions Day 7
  • 📐 2D Arrays — matrices, rows & columns Day 7
  • 🔗 Arrays & Functions — passing arrays to functions Day 7
  • 🏗️ Structures (struct) — custom data types Day 7