The language spectrum



Download 0.53 Mb.
Page8/26
Date13.06.2017
Size0.53 Mb.
#20510
1   ...   4   5   6   7   8   9   10   11   ...   26

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.2.

TYPE
Listing 3.2. 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: }


OUTPUT
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 from a DOS prompt on my machine:

C:\app> 03L02

The addition of 5 and 12 is 17.

C:\app>


ANALYSIS
Line 1 in Listing 3.2 is a comment about the program. As you learned in Hour 2, 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() function, respectively. An integer variable, sum, is declared in line 13.

The statement in line 15 calls the integer_add() function that we examined in the previous section. Note that two integer constants, 5 and 12, are passed to the integer_add() function, and that the sum variable is assigned the result returned from the integer_add() function.

You first saw the C standard library function printf() in Hour 2. Here you may find something new added to the function in line 16. You're right. This time, there are two arguments that are passed to the printf() function. They are the string "The addition of 5 and 12 is %d.\n" and the variable sum.

Note that a new symbol, %d, is added into the first argument. The second argument is the integer variable sum. Because the value of sum is going to be printed out on the screen, you might think that the %d has something to do with the integer variable sum. You're right again. %d tells the computer the format in which sum should be printed on the screen.

More details on %d are covered in Hour 4, "Data Types and Names in C." The relationship between %d and sum is discussed in Hour 5, "Reading from and Writing to Standard I/O."

More importantly, you should focus on the program in Listing 3.2 and pay attention to how to call either a user-generated function or a standard C library function from the main() function.


Summary


In this lesson you've learned the following:

  • A constant in C is a value that never changes. A variable, on the other hand, can present different values.

  • A combination of constants, variables, and operators is called an expression in the C language. An expression is used to denote different computations.

  • The arithmetic operators include +, -, *, /, and %.

  • A statement consists of a complete expression suffixed with a semicolon.

  • The C compiler treats a statement block as a single statement, although the statement block may contain more than one statement.

  • The function type of a function determines the type of the return value made by the function.

  • You have to follow certain rules to make a valid function name.

  • An argument contains information that you want to pass to a function. An argument list contains two or more arguments that are separated by commas.

  • The opening brace ({) and closing brace (}) are used to mark the start and end of a C function.

  • A function body contains variable declarations and statements. Usually, a function should accomplish just one task.

In the next lesson you'll learn more about data types in the C language.

Q&A


Q What is the difference between a constant and a variable?

A The major difference is that the value of a constant cannot be changed, while the value of a variable can. You can assign different values to a variable whenever it's necessary in your C program.

Q Why do you need a statement block?

A Many C keywords can only control one statement. A statement block provides a way to put more than one statement together and put the statement block under the control of a C keyword. Then, the statement block is treated as a single statement.

Q Which arithmetic operators have a higher precedence?

A Among the five arithmetic operators, the multiplication, division, and remainder operators have a higher precedence than the addition and subtraction operators.

Q How many parts does a function normally have?

A A function normally has six parts: the function type, the function name, the arguments, the opening brace, the function body, and the closing brace.

Workshop


To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."

Quiz


  1. In the C language, is 74 a constant? How about 571?

  2. Is x = 570 + 1 an expression? How about x = 12 + y?

  3. Are the following function names valid?

  4. 2methods

  5. m2_algorithm

  6. *start_function

  7. Room_Size

  8. .End_Exe

  9. _turbo_add

  10. Is 2 + 5 * 2 equal to (2 + 5) * 2?

  11. Does 7 % 2 produce the same result as 4 % 3?

Exercises


  1. Given two statements, x = 3; and y = 5 + x;, how can you build a statement block with the two statements?

  2. What is wrong with the following function?

  3. int 3integer_add( int x, int y, int z)

  4. {

  5. int sum;

  6. sum = x + y + z;

  7. return sum;

  8. }

  9. What is wrong with the following function?

  10. int integer_add( int x, int y, int z)

  11. {

  12. int sum;

  13. sum = x + y + z

  14. return sum;

  15. }

  16. Write a C function that can multiply two integers and return the calculated result.

  17. Write a C program that calls the C function you just wrote in exercise 4 to calculate the multiplication of 3 times 5 and then print out the return value from the function on the screen.

4Zen saying

In the previous lessons, you've learned the basics of the C program, several important C functions, standard I/O, and some useful operators. In this lesson you'll learn a very important feature of the C language—looping. Looping, also called iteration, is used in programming to perform the same set of statements over and over until certain specified conditions are met.

Three statements in C are designed for looping:



The following sections explore these statements.


Download 0.53 Mb.

Share with your friends:
1   ...   4   5   6   7   8   9   10   11   ...   26




The database is protected by copyright ©ininet.org 2024
send message

    Main page