Creating and Using Functions in the C Programming Language

Functions, also known as methods in some programming languages, are an essential part of any good program. A function is simply a set of instructions. All C program have at least one function called 'main'. This is where the program starts. There are two significant benefits from making good use of functions. Good use of functions makes a program source code easy to understand to other programmers. Good use of functions also saves the programmer a lot of time. On this page we will demonstrate these two advantages, show how to create functions, and how to use them.

Functions Make Source Code Easier to Understand

The example program code given to the right contains four user defined functions within the main function. Even without any comments given for each function, you can probably figure out roughly how the program works. The first thing it does is call a functions named 'setup()' that we can assume performs some set up instructions for the program. Within the forever loop there are just three lines of code:

 

  • get_user_input();
  • perform_calculations();
  • update_output();

 

By using descriptive names for these functions, we might assume this program gets some input from the user, performs some calculations based on those inputs, and adjusts some output. This template might fit numerous applications, large and small. Even thought its only 3 lines, each of those functions called might contain many lines of code and call other functions that contain more code.

/* Using functions makes code easier to understand */
int main()
{
    setup();
    while(1){
        get_user_input();
        perform_calculations();
        update_output();
    }
    return 0;
}

Using a setup() function

Even though set up instructions might only be used once when the program first starts, it makes the code clean and tidy to stick all those instructions into a function and simply call the function in 'main()'. This function does not need to be named 'setup()'. We might decide to call in 'initialize()' instead. The point is to group all those tasks together to keep your code clean, and easy to understand.

Functions Save the Programmer Time

Functions save the programmer time by enabling the reuse of many lines of code through the use of just a single line of code; the function call being that line of code. Imagine if the 'update_output()' function in the example given above includes all the steps needed to display some data to an LCD display. There is a long list of steps needed to accomplish this, but the 'update_output()' function might only include a single function call to accomplish this such as 'write_string_to_display(valueToDisplay)'. The 'write_string_to_display(valueToDisplay)' function might include code that parses and converts 'valueToDisplay' into individual characters, then passes those character, one at a time, to another function that then sends them to the display via still other functions that control the interface between the processor and the display. If we were to have to include all of those line of code everything time we needed to use them, it would take up a lot of space and programming time. Even if copy and paste were used, the source code would become enormous. Instead, we can encapsulate discrete sets of instructions into functions that can be used in our programs though a single line of code.

Declaring Functions in our C Programs

Any function that is going to be used needs to be declared before it can be used. Declaring a function tells the program how to use the function. There are TBD elements to define when declaring a function:

 

  • Return type - The type of data that is returned by the function.
  • Function name - A unique name just like naming variables.
  • Parameter list - A list of parameters and their data type.

 

The syntax for declaring a function in C is given in the example below. In this example the return type is 'int', the function name is 'myFunction', and the parameter list is 'char, int, float'. What this parameter list means is that the function is expecting to receive three values from the calling function. Its expecting to see one char, one int, and one float. The names of these do not matter at this point.

Defining Functions in our C Programs

The function definition contains the code that will run when the function is called. The definition begins in same way as the function declaration except this time we must give names to each parameter. We then add follow this with { } brackets with the function code in between.

/* Example of a function definition */
int myFunction(char myChar, int myInt, float myFloat){
     int aValueToReturn;
     /* function does something */
     return aValueToReturn;
}
/* Example of function declaration */
int myFunction(char, int, float);

Using a Function in our C Programs

Once a function is declared and defined, it can be used in our program in the same way as shown in the first example except if the function takes parameter, we must include those parameters in the same order as they are in the declaration and the definition. The example below brings it all together. The function, 'myFunction()', is declare before the 'main()' function is entered. 'myFunction()' is defined after the 'main()' function, and its called (used) by 'main()'. When 'myFunction()' is called, three parameters are passed to it just as the declaration specifies.

/* Example of function declaration */
int myFunction(char, int, float);

int main()
{
    char someChar = "A";
    int someInt = 1;
    float someFloast = 2.0;
    myFunction(someChar, someInt, someFloat);
    return 0;
}

/* Example of a function definition */
int myFunction(char myChar, int myInt, float myFloat){
     int aValueToReturn;
     /* function does something */
     return aValueToReturn;
}
Home | Products | Electronics | Programming | SpazzOuts | Contact
© Copyright 2014 - 2017 SpazzTech LLC. All Rights Reserved
Made with Adobe Muse