Sams Teach Yourself C in 24 Hours

Sams Teach Yourself C in 24 Hours by Tony. Zhang

Book: Sams Teach Yourself C in 24 Hours by Tony. Zhang Read Free Book Online
Authors: Tony. Zhang
Ads: Link
statements.
    The Function Body
    The function body in a function is the place that contains variable declarations and other C statements. The task of a function is accomplished by executing the statements inside the function body one at a time.
    It is important to remember that any variable declarations must be placed at the beginning of the function body. It is illegal to put variable declarations anywhere other than the very beginning of a statement block.
    If your function body contains variable declarations, they must all be placed first, before any other statements.
    Listing 3.2 demonstrates a function that adds two integers specified by its arguments and returns the result of the addition.
    05 067231861x CH03 4.10.2000 10:59 AM Page 49
    Learning the Structure of a C Program
    49
    LISTING 3.2
    A Function that Adds Two Integers
    1: /* 03L01.c: This function adds two integers and returns the result */
    2: int integer_add( int x, int y )
    3: {
    4: int result;
    5: result = x + y;
    6: return result;
    7: }
    As you learned in Hour 2, line 1 of Listing 3.1 is a comment that describes what ANALYSIS the function can do.
    In line 2, you see that the int data type is prefixed prior to the function name. Here int is used as the function type, which signifies that an integer is returned by the function.
    3
    The function name shown in line 2 is integer_add. The argument list contains two arguments, int x and int y, in line 2, where the int data type specifies that the two arguments are both integers.
    Line 3 contains the opening brace ({) that marks the start of the function.
    The function body is in lines 4–6 in Listing 3.1. Line 4 gives the variable declaration of result, and is specified by the int data type as an integer. The statement in line 5 adds the two integers represented by x and y and assigns the computation result to the result variable. The return statement in line 6 then returns the computation result represented by result.
    Last, but not least, the closing brace (}) in line 7 is used to close the function.
    When you create a function in your C program, don’t assign the function too much work. If a function has too much to do, it will be very difficult to write and debug. If you have a complex programming project, break it into smaller pieces. Try your best to make sure that each function has just one task to do.
    Making Function Calls
    Based on what you’ve learned so far, you can write a C program that calls the integer_add() function to calculate an addition and then print out the result on the screen. An example of such a program is demonstrated in Listing 3.3.
    05 067231861x CH03 4.10.2000 10:59 AM Page 50
    50
    Hour 3
    LISTING 3.3
    A C Program that Calculates an Addition and Prints the Result to the
    Screen
    1: /* 03L02.c: Calculate an addition and print out the result */
    2: #include
    3: /* This function adds two integers and returns the result */
    4: int integer_add( int x, int y )
    5: {
    6: int result;
    7: result = x + y;
    8: return result;
    9: }
    10:
    11: int main()
    12: {
    13: int sum;
    14:
    15: sum = integer_add(5, 12);
    16: printf(“The addition of 5 and 12 is %d.\n”, sum);
    17: return 0;
    18: }
    The program in Listing 3.2 is saved as a source file called 03L02.c. After this program is compiled and linked, an executable file for 03L02.c is created. On my machine, the executable file is named 03L02.exe. The following is the output printed on the screen after I run the executable on my machine:
    The addition of 5 and 12 is 17.
    OUTPUT
    Line 1 in Listing 3.2 is a comment about the program. As you learned in Hour 2, ANALYSIS the include directive in line 2 includes the stdio.h header file because of the printf() function in the program.
    Lines 3–9 represent the integer_add() function that adds two integers, as discussed in the previous section.
    The main() function, prefixed with the int data type, starts in line 11. Lines 12 and 18
    contain the opening brace and closing brace for the main()

Similar Books

The Gladiator

Simon Scarrow

The Reluctant Wag

Mary Costello

Feels Like Family

Sherryl Woods

Tigers Like It Hot

Tianna Xander

Peeling Oranges

James Lawless

All Night Long

Madelynne Ellis

All In

Molly Bryant