How to Construct a Function in C Programming

By Dan Gookin

In C programming, all functions are dubbed with a name, which must be unique; no two functions can have the same name, nor can a function have the same name as a keyword.

The name is followed by parentheses, which are then followed by a set of curly brackets. So at its simplest construction, a function looks like this:

type function() { }

In the preceding line, type defines the value returned or generated by a function. Options for type include all the standard C variable types — char, int, float, double — and also void for cheap functions that don’t return anything.

function is the function’s name. It’s followed by a pair of parentheses, which can, optionally, contain values passed to the function. These values are called arguments. Not every function features arguments. Then come the curly brackets and any statements that help the function do its thing.

Functions that return a value must use the return keyword. The return statement either ends the function directly or passes a value back to the statement that called the function. For example:

return;

This statement ends a function and does not pass on a value. Any statements in the function after return are ignored.

return(something);

This statement passes the value of the something variable back to the statement that called the function. The something must be of the same variable type as the function, an int, the float, and so on.

Functions that don’t return values are declared of the void type. Those functions end with the last statement held in the curly brackets; a return statement isn’t required.

One more important thing! Functions must be prototyped in your code. That’s so that the compiler understands the function and sees to it that you use it properly. The prototype describes the value returned and any values sent to the function. The prototype can appear as a statement at the top of your source code. Basic Function; No Return shows an example at Line 3.

BASIC FUNCTION; NO RETURN

#include <stdio.h>
void prompt(); /* function prototype */
int main()
{
 int loop;
 char input[32];
 loop=0;
 while(loop<5)
 {
 prompt();
 fgets(input,31,stdin);
 loop=loop+1;
 }
 return(0);
}
/* Display prompt */
void prompt()
{
 printf("C:\DOS> ");
}

Exercise 1: Use the source code from Basic Function; No Return to create a new project, ex1001. Build and run.

The program displays a prompt five times, allowing you to type various commands. Of course, nothing happens when you type, although you can program those actions later, if you like. Here’s how this program works in regard to creating a function:

Line 3 lists the function prototype. It’s essentially a copy of the first line of the function (from Line 22), but ending with a semicolon. It can also be written like this:

void prompt(void);

Because the function doesn’t require any arguments (the items in parentheses), you can use the void keyword in there as well.

Line 13 accesses the function. The function is called as its own statement. It doesn’t require any arguments or return any values, and it appears on a line by itself, as shown in the Listing. When the program encounters that statement, program execution jumps up to the function. The function’s statements are executed, and then control returns to the next line in the code after the function was called.

Lines 22 through 25 define the function itself. The function type is specified on Line 22, followed by the function name, and then the parentheses. As with the prototype, you can specify void in the parentheses because no argument is passed to the function.

The function’s sole statement is held between curly brackets. The prompt() function merely outputs a prompt by using the printf() function, which makes it seem like the function isn’t necessary, but many examples of one-line functions can be found in lots of programs.

Exercise 2: Modify the source code from Basic Function; No Return so that the while loop appears in its own function. (Copy Lines 7 through 16 into a new function.) Name that function busy() and have the main() function call it.

  • C has no limit on what you can do in a function. Any statements you can stuff into the main() function can go into any function. Indeed, main() is simply another function in your program, albeit the program’s chief function.

  • When declaring an int or char function type, you can also specify signed, unsigned, long, and short, as appropriate.

  • The main() function has arguments, so don’t be tempted to edit its empty parentheses and stick the word void in there. In other words, this construct is wrong:

    int main(void)

    The main() function in C has two arguments. It’s possible to avoid Listing them when you’re not going to use them, by keeping parentheses empty.

  • Other programming languages may refer to a function as a subroutine or procedure.