OnlineCBT Logo OnlineCBT
C Tutorial
C · LESSON 9

Functions and Scope Rules

By Ratan Updated: 15 Oct 2026

Write modular code using custom functions, function prototypes, parameters, and global/local scope.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Divide a program into functions

A function gives a name and an interface to one operation. Its declaration tells the compiler the parameter and return types; its definition supplies the body. Good functions have a focused purpose and make their assumptions visible.

Declaration, definition and call

#include 
int larger(int a, int b);
int main(void) {
    int result = larger(14, 9);
    printf("larger=%d\n", result);
    return 0;
}
int larger(int a, int b) {
    return a > b ? a : b;
}

Expected output: larger=14. The prototype appears before main, so the call can be checked before the later definition is seen. The arguments 14 and 9 initialize local parameter objects a and b. Returning a value transfers the result to the caller.

Scope and lifetime are different

Scope describes where a name can be used in source code. A local declared inside a block is normally visible only from its declaration to the end of that block. Lifetime describes when its storage exists. An ordinary automatic local exists during its block's execution; a static local retains its value between calls.

int next_ticket(void) {
    static int ticket = 0;
    ++ticket;
    return ticket;
}

Two calls return 1 then 2, assuming no overflow and no concurrent access. This retained state can be useful, but makes behavior depend on previous calls. For calculations such as maximum or area, passing inputs explicitly is usually easier to test.

Interface discipline

  • Use void as a return type when no result is returned.
  • In pre-C23 C declarations, function(void) explicitly states no parameters; function() has different declaration semantics.
  • Every path that produces a value in a non-void function should return a suitable result.
  • Do not return the address of an automatic local: its lifetime ends when the function returns.

Shadowing and shared state

A local name can hide an outer name. Although legal, reusing total for unrelated objects can make debugging difficult. Keep temporary values local. Avoid a global variable simply to pass one function's answer to another; a return value or parameter describes that dependency better.

Exercises

  1. Write int square(int x); for 7 it should return 49. State a safe input range to avoid signed overflow.
  2. Write int is_even(int n) using n % 2 == 0.
  3. Call larger with equal arguments. It should return that common value.
  4. Predict two calls to a function with int n = 0; return ++n;. Both return 1, because n is reinitialized on each call.

हिन्दी संकेत: scope नाम की visibility है; lifetime storage की अवधि है। दोनों को एक जैसा न समझें।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat