Creating Simple Functions

Writing your own functions requires following the proper syntax. First, you must create a function prototype—a declaration of a function's name, arguments, and what type of value it returns. This prototype helps the compiler check that the function is properly used in an application. The syntax for the prototype is

					type_returned function_name (arguments);

The type_returned section refers to what sort of value the function will return. For example, the main function returns an integer (0 in all of the book's examples so far). If the function returns no value, its type_returned will be void.

Naming functions follows the same rules as naming variables: you use alphanumeric characters plus the underscore. Also, function names are case-sensitive in C, so you should stick to a consistent capitalization strategy. More important, though, is that you cannot use an existing function name for your functions (or any keyword, like int or break).

The arguments a function takes is a more complicated subject, which will be covered later in this chapter. For now, keep in mind that if a function takes no arguments, again void is used.

The function prototype is normally placed after any include lines but before the main function definition. So the bare-bones syntax is

#include <stdio.h>
void my_func (void); /* prototype */
int main (void) {

You can call your own functions as you would any standard library function in C:

my_func();

The final step in defining and using your own functions is the function definition itself. Normally this is placed after the main function in your code. The beginning of the function definition looks like the function prototype. Then you place the function's content (what code is executed when the function is called) between opening and closing curly braces. Let's finish the function definition we began earlier:

void my_func (void) {
   printf ("This is a silly function.
");
}

In our first example, you will take the code for discarding extraneous input and turn that into a function.

To define and use your own functions

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 7.1):

/* discard_function.c - Script 7.1 */
#include <stdio.h>

Script 7.1. In this application, a user-defined function is created and called.


3.
Add the function prototype:

void discard_input (void);

The function being defined in this file is called discard_input. It takes no arguments and returns no value.

4.
Begin the main function:

int main (void) {

Even though this application has a user-defined function, it still requires a main function. Remember that the main function is automatically called when an application is executed, so the core of any application must still go here.

5.
Prompt the user and read in a single character:

printf ("Enter some text and watch me
 do nothing with it: ");
getchar();

This application will do very little; its main purpose is to highlight the process of defining and using your own functions. Since the user-defined function helps ignore extraneous input, the user must be prompted for some input first. This first getchar() call creates that prompt, even though nothing is done with the read-in character.

6.
Call the user-defined function:

discard_input();

To call the function, simply type its name followed by empty parentheses. The line concludes with a semicolon, as does every statement in C.

7.
Add another getchar() call to pause execution on Windows:

getchar();

8.
Complete the main function:

     return 0;
} /* End of main() function. */

When your applications contain multiple functions, you may find it useful to comment on where each function terminates.

9.
After the main function, begin defining the discard_input() function:

void discard_input (void) {

This definition line is very similar to the main definition line, except that this function returns no value (hence the initial void). The curly ”braces mark the start of the function's contents.

10.
Define any necessary variables:

char junk;

Your own functions can, and often will, have their own variables. As you'll see at the end of the chapter (in the section “Understanding Variable Scope”), these are separate variables, pertaining only to this function.

The discard_input() function makes use of a single character called junk.

11.
Add the function's content:

do {
    junk = getchar();
while (junk != '
'),

You've seen this structure before and should recognize it as a handy way to read in and ignore standard input.

12.
Complete the discard_input() function:

} /* End of discard_input()
 function. */

This closing brace marks the end of the function definition. We've added a comment so that it's clear where the function begins and ends. Notice as well that there's no return statement here, as this function returns nothing.

13.
Save the file as discard_function.c, compile, and debug as necessary.

14.
Run the application (Figure 7.1).

Figure 7.1. While it's not the most exciting application, it does make use of your own user-defined function, creating a more modular code structure.




✓ Tips

  • Technically, you could define your own functions before the main function and therefore you would not have to use a function prototype. Or you could define your own functions within the main function. But it is standard procedure to code the main function first, as this example demonstrates.

  • If a large application makes use of the same function several times, or if you have a function or set of functions that you use in many applications, you'll want to place these in their own library file. You'll learn more about this in Chapter 8, “Using the C Preprocessor”.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset