C++ from Scratch to Advance / Functions in C++ / Function Declaration and Definition in C++
Home /C++ from Scratch to Advance /Functions in C++ /Function Declaration and Definition in C++

Function Declaration and Definition in C++

In programming, writing every single instruction inside your main program is inefficient. To build professional software, you need to understand how to delegate tasks. In this guide, we dive into the heart of C++ logic: Function Definition and Function Calling.

Definitions


Before we explore the syntax, let’s define the core concepts simply:

Term Description
Function Declaration A “heads-up” to the compiler about a function’s name, return type, and parameters before it is used.
Function Definition The set of instructions you write to tell a function exactly what to do.
Function Call The specific command you use to “activate” or execute those instructions.
Return Type The category of data (like an int or char) the function sends back after finishing its work.
Function Header The top line of a function that includes its name, return type, and parameters.
Function Body The block of code inside curly braces {} that performs the actual task.
Parameter List Variables in the header that act as “containers” to hold information sent into the function.
Void A special return type used when a function performs a task but doesn’t send a value back.

Anatomy of a Function Definition

A function is made of two main parts: the Header and the Body. Together, they form the Definition.

1. The Header: The Identification Card


The header tells the compiler three things:
    1. Return Type: What kind of value will it give back? (e.g., int, double, or void).
    2. Function Name: A unique name. Experts suggest using a verb (like displayMessage) because functions represent actions.
    3. Parameters: Variables to store the input given by the main function.

The Syntax:


<return type> <function identification> (<parameter-list>){          //Block of statements <function body> }
Example:
The function shows a message simple message “Happy Codding ” followed by the name given as input to the function.

void displayMessage (string name){ cout<<"Happy Codding: "<<name; }

2. The Body: The Action Zone


The body consists of statements wrapped in {}. This is where the magic happens. If a function is value-returning, it ends with a return statement. Once a return is executed, the function stops immediately—any code written after that return will be ignored.

The Function Call
Defining a function is like hiring a worker, but they won’t start until you tell them to. To execute the code, you must “Call” it.
    • Syntax: You write the function name followed by parentheses and a semicolon: like,displayMessage(“Ali”);.
    • Difference: Unlike the header, a Function Call does not include the return type, and the type of the values.

Execution Order & Placement

Execution Order of a C++ Program with Functions
The execution of a C++ program follows a very specific “hierarchy” or path:
    1. The Entry Point: The program always begins at the main function. The Operating System is responsible for starting (calling) the main function.
    2. Top-to-Bottom Awareness: Before main runs, the compiler must already be aware of any other functions you intend to use. This is why functions are typically defined or declared above main.
    3. The Call (The Jump): As main executes, it may hit a Function Call statement. At this exact moment, the program “jumps” from the current line in main to the Function Definition located elsewhere in the code.
    4. Executing the Body: The program then executes the statements inside the function’s body. If the function is called multiple times (such as in a loop), the program will jump to the definition, execute it, and return to main for every single call.
    5. The Return (The Finish): Once the function encounters a return statement or reaches the end of its body, it stops immediately. Any code written after a return statement inside that function is ignored.
    6. Resuming Main: After the function finishes, the control of the program returns to the main function, specifically to the line immediately following the function call.
    7. Final Exit: The program finally ends when the main function finishes its last statement, typically return 0;, which sends a success signal back to the Operating System.

Graphical representation of the execution order of a program in C++

Why Placement Matters


In C++, you should generally define your functions above the int main function. Why? The compiler reads code from top to bottom. If you call a function inside main before the compiler has seen its definition, it won’t know what you’re talking about. By putting it at the top, the compiler is already “aware” of the worker before the Contractor calls them.

Declaring function before main()


In C++ you always have to declare the function(s) before the main() function. However, you can define it (write the body of the function) the function later anywhere.

Try It Yourself!

    1. Point to Ponder: Why do you think it’s better to have 10 small functions instead of one massive int main function?
    2. Challenge 1: Write a void function named greetUser that prints “Welcome to C++!” to the console. Call it three times in your main function.
    3. Challenge 2: Look at your previous code. Try to find a repetitive task and “hire a laborer” (create a function) to do it instead.
    4. Logic Test: If you put a cout statement after a return 0; inside a function, will it ever appear on the screen? Why or why not?
    5. Pattern Challenge: Use a loop in your main function to call a displayMessage function 5 times. Notice how much cleaner the code looks!

Take the Next Step
For official technical deep-dives, check out the C++ Reference for Functions.
Happy Coding!
Video Tutorial