FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses C Programming From Zero

First year

Progress0 / 6 lessons
  1. 1. What a program actually is
  2. 2. Variables, types and exact sizes
  3. 3. Input and output with printf and scanf
  4. 4. Control flow and loops
  5. 5. Functions and how the call stack works
  6. 6. Arrays and strings in C

Courses › C Programming From Zero

Functions and how the call stack works

Why functions exist, what happens in memory when you call one, and why recursion has a limit.

11 min read · Lesson 5 of 6 · Pro

Why functions exist

Not to look tidy. Two real reasons.

First, a function lets you name an idea. is_prime(n) says what it does. Fourteen lines of loop in the middle of main does not.

Second, a function is tested once and trusted everywhere. If your factorial is right, every call is right. Copy-pasted code has to be fixed in every copy, and you will miss one.

Declaration, definition, call

C
#include <stdio.h>

int add(int a, int b);          /* declaration (prototype) */

int main(void)
{
    int sum = add(3, 4);        /* call */
    printf("%d\n", sum);        /* 7 */
    return 0;
}

int add(int a, int b)           /* definition */
{
    return a + b;
}
The rest of this lesson is Pro

The free lessons of C Programming From Zero finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.