Sams Teach Yourself C in 24 Hours by Zhang Tony

Sams Teach Yourself C in 24 Hours by Zhang Tony

Author:Zhang, Tony.
Language: eng
Format: epub, pdf


19 067231861x pt 4 1/25/00 10:27 AM Page 241

PART IV

Functions and Dynamic

Memory Allocation

Hour

15 Working with Functions

16 Applying Pointers

17 Allocating Memory

18 Using Special Data Types and Functions

19 067231861x pt 4 1/25/00 10:27 AM Page 242

20 067231861x CH15 4.10.2000 11:03 AM Page 243

HOUR 15

Working with Functions

Form follows function.

—L. H. Sullivan

In Hour 14, “Understanding Scope and Storage Classes,” you might have

noticed that a function definition is always given first, before the function is called from a main() function. In fact, you can put a function definition anywhere you want, as long as you put the function declaration before the first place where the function is called. You’ll learn about many features of functions from the following topics covered in this lesson:

• Function declarations

• Prototyping

• Values returned from functions

• Arguments to functions

• Structured programming

In addition, several C library functions and macros, such as time(),

localtime(), asctime(), va_start(), va_arg(), and va_end() are

introduced in this hour.

20 067231861x CH15 4.10.2000 11:03 AM Page 244

244

Hour 15

Declaring Functions

As you know, you have to declare or define a variable before you can use it. This is also true for functions. In C, you have to declare or define a function before you can call it.

Declaration Versus Definition

According to the ANSI standard, the declaration of a variable or function specifies the interpretation and attributes of a set of identifiers. The definition, on the other hand, requires the C compiler to reserve storage for a variable or function named by an identifier.

A variable declaration is a definition, but a function declaration is not. A function declaration alludes to a function that is defined elsewhere and specifies what kind of value is returned by the function. A function definition defines what the function does, as well as gives the number and type of arguments passed to the function.

A function declaration is not a function definition. If a function definition is placed in your source file before the function is first called, you don’t need to make the function declaration. Otherwise, the declaration of a function must be made before the function is invoked.

For example, I’ve used the printf() function in almost every sample program in this book. Each time, I had to include a header file, stdio.h, because the header file contains the declaration of printf(), which indicates to the compiler the return type and prototype of the function. The definition of the printf() function is placed somewhere else.

In C, the definition of this function is saved in a library file that is invoked during the linking states.

Specifying Return Types

A function can be declared to return any data type, except an array or function. The return statement used in a function definition returns a single value whose type should match the one declared in the function declaration.

By default, the return type of a function is int, if no explicit data type is specified for the function. A data type specifier is placed prior to the name of a function like this: data_type_specifier

function_name();

Here data_type_specifier specifies the data type that the function should return.

function_name is the function name that should follow the rules of naming identifiers in C.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.