Skip to content
StudyFame

Function Declaration, Definition and Call

Using a function in C has three steps: tell the compiler the function exists (declaration), write what it does (definition), and use it (call).

In this lesson you will learn all three, why the declaration (also called a function prototype) is needed when a function is written below main(), and how the return statement works.

What You Will Learn

  • Function declaration (prototype), definition and call.
  • Why a prototype is needed.
  • Where each part is written in a program.
  • How return sends a value back and ends a function.

Function Declaration, Definition and Call Explained

Exam definition: A function declaration (prototype) tells the compiler the function's name, return type and parameter types. A function definition contains the actual body of the function. A function call executes the function by passing arguments to it.

StepExampleEnds withWhere
Declaration (prototype)int square(int n);Semicolon ;Top of the file, before main()
Definitionint square(int n) { return n * n; }Body in { }Anywhere outside other functions
Cally = square(5);Semicolon ;Inside another function (e.g. main())

Memory trick: Think of a wedding: the invitation card (declaration) tells guests the event exists; the actual wedding (definition) is where everything happens; attending (call) is when you go to it. The invitation must reach you before you attend.

Why is a prototype needed?

  • The compiler reads your file from top to bottom.
  • If main() calls square() but square() is defined below main(), the compiler doesn't know it yet.
  • The prototype at the top tells the compiler in advance: "a function named square exists, it takes an int and returns an int". Now it can check every call for the right types.
  • If the function is defined above main(), the definition itself acts as the declaration, so a separate prototype is optional.

Syntax of Declaration, Definition and Call

return_type name(parameter_types);          /* declaration (prototype) */

return_type name(parameter_list)            /* definition */
{
    /* body */
    return value;
}

variable = name(arguments);                 /* call */

In a prototype, parameter names are optional: int square(int); is also valid.

Example 1: All Three Steps in One Program

#include <stdio.h>

int square(int n);              /* 1. declaration (prototype) */

int main(void)
{
    int a = 6;
    int result = square(a);     /* 3. call */
    printf("Square of %d = %d\n", a, result);
    return 0;
}

int square(int n)               /* 2. definition */
{
    return n * n;
}

Output

Square of 6 = 36

Code Explanation

  1. int square(int n);: the prototype, ending with a semicolon. It has no body.
  2. square(a) in main(): the call. The value of a (6) is copied into n.
  3. The definition below main() runs: return n * n; sends 36 back.
  4. 36 is stored in result and printed.

How the return Statement Works

  • return value; does two things: it sends the value back to the caller and ends the function immediately.
  • Any code after a return in the same path is never reached.
  • A function can have several return statements (e.g. one in an if and one in the else), but only one runs per call.
  • A void function returns nothing. It can use return; (with no value) to leave early.
  • A function can return only one value directly (to return more, use pointers or structures, covered later).

Example 2: Function Defined Below main with Two Returns

#include <stdio.h>

int is_even(int n);             /* prototype */
void print_line(void);          /* prototype for a void function */

int main(void)
{
    int x;
    printf("Enter a number: ");
    scanf("%d", &x);

    print_line();
    if (is_even(x))
        printf("%d is even\n", x);
    else
        printf("%d is odd\n", x);
    print_line();

    return 0;
}

int is_even(int n)
{
    if (n % 2 == 0)
        return 1;               /* ends the function here for even numbers */
    return 0;
}

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

Output

Enter a number: 7
----------
7 is odd
----------

Without the two prototypes at the top, the compiler would complain when main() calls these functions, because their definitions come later.

Important Points to Remember

  • Declaration = prototype, ends with ;, no body.
  • Definition = the full function with its body.
  • Call = using the function with arguments.
  • A prototype is needed when the function is defined after it is called.
  • return sends a value back and ends the function.

Common Mistakes Beginners Make

MistakeWhat happensFix
No semicolon after the prototypeSyntax errorint square(int n);
Semicolon after the definition header: int square(int n); { ... }ErrorNo ; before the body
Prototype and definition don't matchError: conflicting typesSame return type and parameter types
Code written after returnThat code never runsPut return at the end of the path
Calling with the wrong number of argumentsError: too few / too many argumentsMatch the prototype

Function Prototype Practice Questions

  1. What is a function prototype? Why is it used?
  2. Write the difference between a function declaration and a function definition.
  3. Write a prototype for a function average that takes three ints and returns a float.
  4. What two things does a return statement do?
  5. When is a separate prototype not required?
Show answers
  1. A declaration that tells the compiler a function's name, return type and parameter types before it is used, so calls can be checked.
  2. A declaration has no body and ends with a semicolon; a definition contains the body with the actual statements.
  3. float average(int a, int b, int c);
  4. It returns a value to the caller and ends the function.
  5. When the function is defined before (above) the place it is called.

Coding Exercise

Write a program with a prototype for int max(int a, int b), define it below main(), and use it to print the larger of 34 and 58.

Show solution
#include <stdio.h>

int max(int a, int b);

int main(void)
{
    printf("Larger = %d\n", max(34, 58));
    return 0;
}

int max(int a, int b)
{
    if (a > b)
        return a;
    return b;
}
Larger = 58

Quick Quiz: Function Prototype in C (MCQ)

  1. A function prototype ends with:

    A) { }   B) ;   C) :   D) Nothing

  2. Which contains the body of the function?

    A) Declaration   B) Definition   C) Call   D) Prototype

  3. Which is a valid prototype?

    A) int sum(int, int);   B) sum int(int a);   C) int sum(int a, int b) {};   D) int sum;

  4. What happens to code written after a return in the same block?

    A) Runs first   B) Never runs   C) Runs twice   D) Error always

  5. A prototype is needed when the function is defined:

    A) Above main()   B) Below main() and called in main()   C) Never   D) In a comment

Show answers
  1. B: ;
  2. B: the definition.
  3. A: int sum(int, int);
  4. B: it never runs.
  5. B: below main() and called in main().

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Declaration (prototype)int f(int); name, return type, parameter types
DefinitionFull function with body
Callf(5);
Prototype needed whenFunction is defined after it is called
returnSends value back and ends the function
Values returned directlyOnly one

Frequently asked questions

Why do we use a function prototype?
So the compiler knows the function before it is called and can check the argument and return types.
What is the difference between declaration and definition?
Declaration announces the function (no body, ends with ;); definition contains the body.
Can a function return more than one value?
Not directly. Use pointers, arrays or a structure.
Are parameter names required in a prototype?
No. Only the types are required: int sum(int, int);

Remember

  • Wedding: invitation (declaration) → wedding (definition) → attend (call).
  • Prototype = header + semicolon.

Next Topic

Next lesson: Types of User-Defined Functions in C. You will learn the 4 types of user-defined functions based on arguments and return values.