Your First Function: void, Declarations, and Definitions

Your addition program works, but everything is crammed into main. Real programs are split into smaller, named pieces called functions, and you have been using them all along: printf and scanf are functions someone else wrote. In this lesson you write your own, a function that greets the user. Then you break it in two different ways to find out what the compiler needs to know about a function before you can use it.

In this lesson you will

  • Write and call a void function with no parameters
  • Follow the flow of execution into a function and back out
  • Tell the difference between a function declaration (prototype) and a definition
  • Read the “implicit declaration” and “conflicting types” errors

Start by copying your project, as you did in the scanf lesson: copy 002-UserInputCProgram to a new folder named 003-FunctionDemoCProgram, save the project under the new name, and update its title and output filename.

Try it: write a function and call it

Above main, type a new function called WelcomeMessage. Inside main, add one line, WelcomeMessage();, before everything else:

#include <stdio.h>
#include <stdlib.h>

void WelcomeMessage(void)
{
    printf("Welcome to the addition program\n");
    printf("I hope you enjoy this program\n\n");
}

int main()
{
    WelcomeMessage();

    float x = -9999;
    float y = -9999;

    printf("Please enter a value for x:\n");
    scanf("%f", &x);
    printf("Please enter a value for y:\n");
    scanf("%f", &y);

    float sum = x + y;
    printf("The sum of %.2f and %.2f is %.2f\n", x, y, sum);
    return 0;
}

Build and run, and enter 15 and 16:

Welcome to the addition program
I hope you enjoy this program

Please enter a value for x:
15
Please enter a value for y:
16
The sum of 15.00 and 16.00 is 31.00

Every C program starts running at the first line of main. When it reaches WelcomeMessage();, execution jumps to the first line inside WelcomeMessage, runs its two printf lines, and then returns to main, carrying on from the line after the call. See it for yourself with the debugger from the previous lesson. Put a breakpoint on the call, start debugging, and choose Step into: the yellow arrow moves inside your function. Choose Next line instead, and the whole function runs in one step.

What just happened

A function has a return type, a name, a list of parameters in brackets, and a body in curly braces:

  • void is the return type. void means “nothing”: this function does its job (printing) and hands no value back.
  • WelcomeMessage is the name. Choose names that say what the function does, so that main reads almost like a description of the program.
  • (void) in the brackets says the function takes no parameters. (You will also see empty brackets (), as in int main(). In the newest C standard they mean the same thing, but (void) is the clearest way to say “no parameters”.)
  • The braces hold the statements that run each time the function is called.

In this series, braces go on their own lines, and function names use upper camel case (WelcomeMessage). Many programmers put the opening brace at the end of the first line instead. Either style is fine, as long as you are consistent.

Try it: move the function below main

Many programmers like main to be the first thing in the file, so that a reader sees the big picture first. Cut the whole WelcomeMessage function and paste it below main, then build.

Break it on purpose: call a function the compiler has not seen yet

Observe: the build fails. With GCC 14 the first message is:

main.c:6:5: error: implicit declaration of function 'WelcomeMessage' [-Wimplicit-function-declaration]

Older compilers, such as the GCC included with many Code::Blocks installs, may report this as a warning instead, often followed by an error saying conflicting types for 'WelcomeMessage'. Either way, the program does not build cleanly.

Remember that the compiler reads from top to bottom. When it reaches the call in main, it has never heard of WelcomeMessage, so it does not know what kind of function this is.

The fix is to announce the function before main. Add this one line, ending in a semicolon, above main:

void WelcomeMessage(void);
#include <stdio.h>
#include <stdlib.h>

void WelcomeMessage(void);

int main()
{
    WelcomeMessage();

    float x = -9999;
    float y = -9999;

    printf("Please enter a value for x:\n");
    scanf("%f", &x);
    printf("Please enter a value for y:\n");
    scanf("%f", &y);

    float sum = x + y;
    printf("The sum of %.2f and %.2f is %.2f\n", x, y, sum);
    return 0;
}

void WelcomeMessage(void)
{
    printf("Welcome to the addition program\n");
    printf("I hope you enjoy this program\n\n");
}

Build and run: it works again, with the function below main.

Name it: declaration vs definition

  • void WelcomeMessage(void); is a function declaration, also called a prototype. It gives the function’s return type, name and parameters, and ends with a semicolon. It promises the compiler that the function exists.
  • The version with a body in braces is the function definition. It is the actual code that runs.
  • It is just like variables: float x; declares a name and a type. A function prototype does the same for a function.

Break it on purpose: break the promise

Change the definition (not the prototype) so that it takes a parameter: void WelcomeMessage(int times). Leave the prototype as void WelcomeMessage(void); and build:

main.c:23:6: error: conflicting types for 'WelcomeMessage'; have 'void(int)'
main.c:4:6: note: previous declaration of 'WelcomeMessage' with type 'void(void)'

The prototype and the definition must match exactly: same return type, same name, same parameter types. The compiler even tells you where the earlier promise was made (line 4). Undo the change and build again. Code::Blocks shows similar messages in its build log.

Key terms

  • Function: a named block of code that performs a task and can be called from elsewhere.
  • Call: using a function by writing its name followed by brackets, e.g. WelcomeMessage();
  • Return type: the type of value a function hands back. void means none.
  • Parameters: the inputs a function accepts, listed in its brackets.
  • Declaration (prototype): the function’s header followed by a semicolon. It tells the compiler the function exists.
  • Definition: the function’s header plus its body. It is the code that actually runs.
  • Flow of execution: the order in which statements run, including jumps into functions and back.

Quick check

  1. Where does every C program start running?

    Show answer

    At the first statement inside main.

  2. What does void mean as a return type?

    Show answer

    The function does not return a value. It just does its job, such as printing.

  3. Your function is defined below main and the build reports “implicit declaration of function”. What should you add?

    Show answer

    A prototype above main, for example void WelcomeMessage(void);

  4. Which line is a prototype: void Hello(void); or void Hello(void) { printf("Hi\n"); }?

    Show answer

    void Hello(void);. It has no body and ends with a semicolon. The other line is a definition.

Optional: a recorded demonstration of these steps starts around 9:10.

Part of the Introduction to C series. Next: Functions That Return Values, and Variable Scope