100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Functions in C

Learn C functions: declaration vs definition, parameters, pass-by-value, return statements, and prototypes with examples.

Functions & RecursionBeginner14 min readJul 7, 2026
Analogies

1. Introduction

A function in C is a self-contained block of code that performs a specific task and can be called (invoked) from other parts of a program. Functions let you break a large program into smaller, reusable, and testable pieces. C programs are built from functions — every C program has at least one, main(), which is where execution begins. Beyond main(), you can write your own functions to avoid repeating code, to organize logic into named units, and to make programs easier to read, debug, and maintain.

🏏

Cricket analogy: A bowler's specific delivery, like Bumrah's yorker, is a reusable skill drilled separately from the match itself; main() is the innings where every skill gets called into action to complete the game.

Functions in C fall into two categories: library functions (predefined functions such as printf(), scanf(), strlen(), and sqrt() that come with the C Standard Library and are declared in header files like stdio.h, string.h, and math.h) and user-defined functions (functions that you write yourself to perform tasks specific to your program). This lesson focuses on user-defined functions — how to declare, define, call, and pass data to and from them.

🏏

Cricket analogy: printf() is like using a standard-issue Kookaburra bat everyone trusts, already manufactured and ready; a user-defined function is like a custom bat Steve Smith has specially crafted to his own grip and stroke.

2. Syntax

c
/* Function prototype (declaration) */
returnType functionName(parameterType1 param1, parameterType2 param2, ...);

/* Function definition */
returnType functionName(parameterType1 param1, parameterType2 param2, ...)
{
    /* function body */
    /* ... statements ... */
    return value;   /* omitted if returnType is void */
}

/* Function call */
functionName(argument1, argument2);

A function's signature consists of its return type, its name, and the types (and optionally names) of its parameters. The return type specifies what kind of value the function sends back to the caller; use void if the function returns nothing.

🏏

Cricket analogy: A bowler's action signature — pace, arm angle, and delivery type — identifies them, like Rashid Khan's leg-spin; a function's signature identifies its return type and parameter types the same way, with void meaning no scorecard entry.

3. Explanation

3.1 Declaration vs. Definition

A function declaration (also called a prototype) tells the compiler the function's name, return type, and parameter types WITHOUT providing the body. It ends with a semicolon: int add(int a, int b);. A function definition provides the actual body of the function — the code that executes when the function is called: int add(int a, int b) { return a + b; }. In small single-file programs you can define a function before main() and skip a separate prototype, but in larger programs, prototypes are placed near the top of the file (or in a header file) so that functions can be called before their full definition appears later in the file, or from other source files entirely.

🏏

Cricket analogy: A team sheet announcing Rohit Sharma will open the batting is like a prototype — it names the player and role without showing the actual innings; the definition is Rohit walking out and playing the full innings.

3.2 Why Prototypes Matter

Without a prototype, if you call a function before the compiler has seen its definition, older compilers assumed an implicit int return type — a dangerous, non-standard behavior that C99 and later standards disallow, producing a compile error instead. Always declare a prototype (or define the function) before it is first called. Prototypes also let the compiler perform type checking on arguments, catching mismatched-type bugs at compile time rather than causing undefined behavior at runtime.

🏏

Cricket analogy: Fielding a batter without checking the team sheet first, like assuming any padded-up player is the designated opener, risks fielding the wrong strategy; modern rules require confirming the lineup before play, just as C99 requires a prototype before calling.

3.3 Parameters vs. Arguments

A parameter is the variable listed in the function's definition/declaration (a placeholder). An argument is the actual value supplied when the function is called. In int add(int a, int b), 'a' and 'b' are parameters; in add(5, 3), 5 and 3 are arguments. C requires the number and (compatible) types of arguments to match the parameters, since C does not support default argument values or function overloading the way C++ does — every function call must supply exactly the arguments the prototype demands.

🏏

Cricket analogy: In a coaching drill, "batter faces bowler_type" is a parameter — a placeholder role; when Kohli actually faces Starc in a real match, that specific pairing is the argument supplied at call time.

3.4 Pass-by-Value Semantics

C passes all arguments by value: when you call a function, the function receives a COPY of each argument's value, not the original variable. Any changes the function makes to its parameters have no effect on the caller's original variables. This is why a function like void increment(int x) { x = x + 1; } does not change the variable passed to it — 'x' inside the function is a separate copy in its own stack frame.

🏏

Cricket analogy: Handing a scorer a photocopy of the scorecard to annotate during review doesn't change the official scorebook; similarly, a function receiving a copy of a value can scribble on it without altering the caller's original variable.

C has NO pass-by-reference mechanism (unlike C++, which offers reference parameters with &). To let a function modify a caller's variable, you must explicitly pass a pointer to that variable (its address, using &) and have the function dereference the pointer (using *) to read or write the original value. Forgetting this is one of the most common beginner bugs — writing void swap(int a, int b) and expecting it to swap the caller's variables will silently fail because 'a' and 'b' are only local copies.

c
#include <stdio.h>

/* WRONG: pass-by-value cannot swap the caller's variables */
void swapWrong(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

/* CORRECT: pass addresses, modify via pointers */
void swapCorrect(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void) {
    int x = 10, y = 20;

    swapWrong(x, y);
    printf("After swapWrong: x=%d, y=%d\n", x, y);   /* unchanged */

    swapCorrect(&x, &y);
    printf("After swapCorrect: x=%d, y=%d\n", x, y);  /* swapped */

    return 0;
}

3.5 The return Statement

The return statement ends a function's execution and optionally sends a value back to the caller. Its type must match (or be convertible to) the function's declared return type. A function declared void must not return a value (a bare return; is allowed to exit early). A function can have multiple return statements along different code paths, but execution stops at whichever one runs first. If a non-void function reaches its closing brace without hitting a return, the returned value is undefined — always ensure every path returns a value.

🏏

Cricket analogy: A batter's dismissal ends their innings and reports a final score back to the scoreboard, like Kohli's 82; a non-out declaration is like void, ending without a number, but forgetting to record any score at all leaves the scoreboard undefined.

Tip: Keep functions short and focused on a single task (the 'do one thing well' principle). Small functions with a clear name, a well-defined return type, and few parameters are easier to test, debug, and reuse than one giant function that does everything.

4. Example

c
#include <stdio.h>

/* Function prototypes */
int square(int n);
int add(int a, int b);
void printLine(void);

int main(void) {
    int num = 6;
    int result = square(num);

    printf("Square of %d is %d\n", num, result);
    printf("Sum of 4 and 9 is %d\n", add(4, 9));
    printLine();

    return 0;
}

/* Function definitions */
int square(int n) {
    return n * n;
}

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

void printLine(void) {
    printf("------------------\n");
}

5. Output

text
Square of 6 is 36
Sum of 4 and 9 is 13
------------------

6. Key Takeaways

  • A function declaration (prototype) tells the compiler the name, return type, and parameter types; a definition provides the actual body.
  • Parameters are placeholders in the function signature; arguments are the real values passed during a call.
  • C uses pass-by-value only — functions receive copies of arguments, so changes inside the function do not affect the caller's originals.
  • To modify a caller's variable, pass a pointer to it and dereference the pointer inside the function.
  • The return statement sends a value back to the caller and must match the declared return type; void functions return nothing.
  • Always declare or define a function before it is called so the compiler can type-check the call.
  • C has no function overloading or default arguments (unlike C++) — each function name maps to exactly one signature.

Practice what you learned

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#FunctionsInC#Functions#Syntax#Explanation#Declaration#StudyNotes#SkillVeris#ExamPrep