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
returnsends 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.
| Step | Example | Ends with | Where |
|---|---|---|---|
| Declaration (prototype) | int square(int n); | Semicolon ; | Top of the file, before main() |
| Definition | int square(int n) { return n * n; } | Body in { } | Anywhere outside other functions |
| Call | y = 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()callssquare()butsquare()is defined belowmain(), 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
int square(int n);: the prototype, ending with a semicolon. It has no body.square(a)inmain(): the call. The value ofa(6) is copied inton.- The definition below
main()runs:return n * n;sends 36 back. - 36 is stored in
resultand 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
returnin the same path is never reached. - A function can have several
returnstatements (e.g. one in an if and one in the else), but only one runs per call. - A
voidfunction returns nothing. It can usereturn;(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.
returnsends a value back and ends the function.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| No semicolon after the prototype | Syntax error | int square(int n); |
Semicolon after the definition header: int square(int n); { ... } | Error | No ; before the body |
| Prototype and definition don't match | Error: conflicting types | Same return type and parameter types |
Code written after return | That code never runs | Put return at the end of the path |
| Calling with the wrong number of arguments | Error: too few / too many arguments | Match the prototype |
Function Prototype Practice Questions
- What is a function prototype? Why is it used?
- Write the difference between a function declaration and a function definition.
- Write a prototype for a function
averagethat takes three ints and returns a float. - What two things does a return statement do?
- When is a separate prototype not required?
Show answers
- A declaration that tells the compiler a function's name, return type and parameter types before it is used, so calls can be checked.
- A declaration has no body and ends with a semicolon; a definition contains the body with the actual statements.
float average(int a, int b, int c);- It returns a value to the caller and ends the function.
- 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)
-
A function prototype ends with:
A)
{ }B);C):D) Nothing -
Which contains the body of the function?
A) Declaration B) Definition C) Call D) Prototype
-
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; -
What happens to code written after a
returnin the same block?A) Runs first B) Never runs C) Runs twice D) Error always
-
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
- B:
; - B: the definition.
- A:
int sum(int, int); - B: it never runs.
- 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 |
|---|---|
| Definition | Full function with body |
| Call | f(5); |
| Prototype needed when | Function is defined after it is called |
| return | Sends value back and ends the function |
| Values returned directly | Only 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.