
Welcome to C Programming Tutorial 5! This time we’re going to talk about functions in C.
Functions in C: A Bird’s Eye View
Let’s get a bird’s eye view of functions before we go into more detail.
A function is a self-contained unit of program code designed to accomplish a particular task.
Think of functions in C as building material for C programs. Just as your house is made of wood, drywall, and other materials C programs are composed of different parts. These parts are functions.
If you’ve programmed in C at all before, you’re likely familiar with the function main() which every C program needs to have. Underneath it all, main() is just another function much like all the others.
What others?
C comes pre-packaged with many different functions for handling different common tasks. You can use these functions with the #include directive at the beginning of your programs.
Another cool thing about functions in C (and other languages) is that you can write your own custom functions to do whatever you want. More on that later.
For now, let’s get the basic gist of what functions in C are.
Why Use Functions in C?
Why use functions?
First, they save you from repetitious programming. If you have to do a certain task several times in a program, you only need to write an appropriate function once. The program can then use that function wherever it needs to, or you can use the same function in different programs.
Experience has shown that the best way to develop and maintain a large C program is to construct it from smaller pieces. Each of these pieces is more manageable than the original large program.
These pieces are functions in C.
Using functions is a good idea because it makes a program more modular, hence easier to read and easier to change or fix.
Programmers can write functions to define specific tasks. Not only can these tasks find use in multiple places in the program, they can also find use in completely different programs altogether. This cuts down on repetitive work and duplicating the same code over and over again for different programs.
Let’s look at an example of a function.
In C programming, if you want to display something on the screen you may use the printf() function. In the land of Arduino, you use the Serial.print() function. They both yield the same result, but programming in Arduino is slightly different than programming in C (though they are similar).
Sure, you could write a bunch of code from scratch in C or Arduino to print some characters on your screen, but why reinvent the wheel? This has already been done and can be a huge time saver. Instead of writing a bunch of lines of code to handle the low level operations of writing a character to the screen, you can just call (or invoke) the function that does this.
Way easier.
Some C functions, like printf() or Serial.print() come with the compiler. Others are custom made.
Anatomy of a Function in C
Now let’s dissect our example function, printf() and talk about the different parts. We’ll use the following code snippet and focus on the function, which we can see in red below.
#include <stdio.h>
#define NAME "Circuit Crush"
int main(void)
{
printf("%s\n", NAME);
// More code would go here…
First, we invoke the function by typing its name. This is referred to as the function call.
What about the stuff in the parentheses?
These are the function’s arguments, which you can think of as the “stuff” the functions uses.
We defined NAME with the #define directive and assigned it to Circuit Crush.
The %s\n inside the quotes are conversion specifications which tell the computer where on the screen to print the characters and how to print them.
The %s tells the compiler that we’re printing a string of characters, not just one. The \n tells the compiler to print the text on a new line.
Earlier, we stated that all C programs need the main() function. Let’s also take a closer look at main() and dissect it just as we did with printf().
The int in front of main() indicates that the main() function returns an integer. Notice that, unlike printf(), the parentheses in main() contain the word void. This means that main() takes no arguments.
Some functions neither return anything or take any arguments. In this case you can write something like
void myFunction(void);
Writing Your Own Functions in C
C comes with a lot of built-in functions already which can save you time and work, but what if none of them do what you need?
Ever dreamed of being Iron Man, having the ability to build anything, anytime? Try Academy for Arduino!
The answer is to write your own C function.
Like variables, functions have types. Any program that uses a function should declare the type for that function before using it. This is referred to as the function prototype and needs to appear before the function’s first call.
Function Prototypes in C
Let’s pretend that for some reason we need a function that prints 25 plus signs in a row. Since our compiler does not come with such a function, we’ll need to write it.
To do that, we’ll need to start with the function prototype. Consider the C function prototype below.
void plusSign(void);
The parentheses indicate that plusSign() is a function name. The first void is the function type and indicates that the function returns no value. The second void (the one in parentheses) indicates that the function does not take arguments. The semicolon indicates that you are declaring the function, not defining it (definition comes later). That is, this line announces that the program uses a type void function with the name plusSign(). It tells the compiler to expect to find the definition of the function elsewhere.
The function prototype can either come before main() or after it. Either way will work.
The complier refers to the prototype to check that calls to the function contain the right number and types of arguments and that the arguments are in correct order. It also uses the prototype to ensure that the data type returned by the function can be used correctly in the expression that called the function.
Earlier I said that the function prototypes were necessary for writing your own functions. This is actually a half-truth. The prototype is not needed if the function definition appears before the function’s first use in the program. In this case, the function header acts as the prototype.
However, most agree that it’s good programming practice to use function prototypes in C and I suggest you do the same.
Function Definitions in C
Once we have a prototype, we’ll need to write the function itself. This is the function definition.
For our sample program that prints 25 plus signs in a row, our function definition may look like the example below.
void plusSign(void) //define the function { int count; for (count = 1; count <= 25; count++) putchar('+'); putchar('\n'); }
Let’s take a closer look at this function.
The lack of a semicolon after void plusSign(void) tells the compiler that you are defining the function instead of calling or prototyping it.
The variable count in plusSign() is a local variable. This means it is known only to that function. You could use the name count as another variable in main() or a different function definition and there will be no conflict.
PlusSign() doesn’t have any input because it doesn’t need to use any information from the calling function. It doesn’t provide (or return) any information to main(), so it doesn’t have a return value. This function doesn’t require any communication with the calling function. That’s why we see void in the beginning and inside the parentheses.
Next, we see putchar() rather than printf(). The later is a generic printing function that works with a bunch of different format specifiers and prints the result as a string. The C function putchar() simply prints a character to the screen. The two functions are similar, but when you need to print only one character and need speed using putchar() may give you a performance benefit.
Finally, the bulk of the function is enclosed in curly braces.
Just for kicks, let’s write a complete C program that creates and uses this function. Take a look at the code below.
#include <stdio.h> #define NAME "Circuit Crush” void plusSign(void); // function prototype int main(void) { plusSign(); // Function call printf("%s\n", NAME); plusSign(); // Another function call return 0; } void plusSign(void) //define the function { int count; for (count = 1; count <= 25; count++) putchar('+'); putchar('\n'); }
Can you guess what the output looks like? If you guessed the words Circuit Crush between two rows of 25 plus signs, you’re right. We can see the output below.
+++++++++++++++++++++++++ Circuit Crush +++++++++++++++++++++++++
More About Functions in C to Come
So far, we’ve covered the basics of functions — we’ve talked about why you should use them, how to call them, and how to write simple functions.
However, there is more to functions in C than just those things.
In another future article, we’ll talk about functions in C that return values, scope, recursive functions and more.
Stay tuned!
Meanwhile, comment and tell us what your latest C program is all about. If you’re new to C programming, tell us why you want to learn about it. What are your biggest questions? Maybe I or someone else can help!
P.S.
In case you missed it, here’s C Programming Tutorial 4.
Ever dreamed of being Iron Man, having the ability to build anything, anytime? Try Academy for Arduino!

Electronics Tips & Tutorials Sent Directly to Your Inbox

Submit your email & you'll get:
- Exclusive content that I don't put on the blog
- The checklist 10 mistakes all electronics enthusiasts make (& how to avoid them)
- And more!
Very useful. would expect an article on pointers.
Stay tuned…