home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor1.zip / CHAP05.TXT < prev    next >
Text File  |  1989-11-10  |  32KB  |  705 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                     Chapter 5
  6.                          FUNCTIONS, VARIABLES, AND PROTOTYPES
  7.  
  8.  
  9. OUR FIRST USER DEFINED FUNCTION
  10. ____________________________________________________________
  11.  
  12. Load and examine the file SUMSQRES.C for an    ==============
  13. example of a C program with functions.           SUMSQRES.C
  14. Actually this is not the first function we     ==============
  15. have encountered because the main program we
  16. have been using all along is technically a
  17. function, as is the printf() function.  The printf() function
  18. is a library function that was supplied with your compiler. 
  19.  
  20. Notice the executable part of this program which begins in
  21. line 8 with a line that simply says "header();", which is the
  22. way to call any function.  The parentheses are required
  23. because the C compiler uses them to determine that it is a
  24. function call and not simply a misplaced variable.  When the
  25. program comes to this line of code, the function named
  26. header() is called, its statements are executed, and control
  27. returns to the statement following this call.  Continuing on
  28. we come to a for loop which will be executed 7 times and which
  29. calls another function named square() each time through the
  30. loop, and finally a function named ending() will be called and
  31. executed.  For the moment ignore the variable name index in
  32. the parentheses of the call to square.  We have seen that this
  33. program calls a header, 7 square calls, and an ending.  Now
  34. we need to define the functions.
  35.  
  36.  
  37. DEFINING THE FUNCTIONS
  38. ____________________________________________________________
  39.  
  40. Following the main program you will see another program
  41. beginning in line 14 that follows all of the rules set forth
  42. so far for a main program except that it is named header(). 
  43. This is the function which is called from line 8 of the main
  44. program.  Each of these statements are executed, and when they
  45. are all complete, control returns to the main program. 
  46.  
  47.  
  48. The first statement sets the variable named sum equal to zero
  49. because we plan to use it to accumulate a sum of squares. 
  50. Since the variable named sum is defined prior to the main
  51. program, it is available to be used in any of the following
  52. functions.  It is called a global variable, and its scope is
  53. the entire program and all functions.  It is also sometimes
  54. referred to as a file variable because it is available
  55. throughout the file.  More will be said about the scope of
  56. variables near the end of this chapter.  The statement in line
  57. 17 outputs a header message to the monitor.  Program control
  58.  
  59.                                                      Page 5-1
  60.  
  61.                Chapter 5 - Functions, Variables, & Prototypes
  62.  
  63. then returns to the main program since there are no additional
  64. statements to execute in this function.  Essentially, we drop
  65. out of the bottom of the function and return to the caller.
  66.  
  67. It should be clear to you that the two executable lines from
  68. this function could be moved to the main program, replacing
  69. the header call, and the program would do exactly the same
  70. thing that it does as it is now written.  This does not
  71. minimize the value of functions, it merely illustrates the
  72. operation of this simple function in a simple way.  You will
  73. find functions to be very valuable in C programming.
  74.  
  75.  
  76. PASSING A VALUE TO A FUNCTION (CLASSIC METHOD)
  77. ____________________________________________________________
  78.  
  79. Going back to the main program, and the for loop specifically,
  80. we find the new construct from the end of the last lesson used
  81. in the last part of the for loop, namely the index++ used in
  82. line 9.  You should get familiar with this construct, as you
  83. will see it in a lot in C programs.
  84.  
  85. In the call to the function named square(), we have an added
  86. feature, namely the variable name index within the
  87. parentheses.  This is an indication to the compiler that when
  88. you go to the function, you wish to take along the value of
  89. index to use in the execution of that function.  Looking ahead
  90. at the function named square(), we find that another variable
  91. name is enclosed in its parentheses, the variable number. 
  92. This is the name we prefer to call the variable passed to the
  93. function when we are in the function.  We can call it anything
  94. we wish as long as it follows the rules of naming an
  95. identifier.  Since the function must know what type the
  96. variable is, it is defined following the function name but
  97. before the opening brace of the function itself.  Thus, line
  98. 21 containing "int number;" tells the function that the value
  99. passed to it will be an integer type variable.  With all of
  100. that out of the way, we now have the value of index from the
  101. main program passed to the function square(), but renamed
  102. number, and available for use within the function.  This is
  103. the classic style of defining function variables and has been
  104. in use since C was originally defined.  A newer method is
  105. gaining in popularity due to its many benefits and will be
  106. discussed later in this chapter.
  107.  
  108. Following the opening brace of the function, we define another
  109. variable named numsq for use only within the function itself,
  110. (more about that later) and proceed with the required
  111. calculations.  We set the variable named numsq equal to the
  112. square of the value of number, then add numsq to the current
  113. total stored in the variable named sum.  Remember that "sum
  114. += numsq;" has the identical meaning as "sum = sum + numsq;"
  115. from the last lesson.  We print the number and its square in
  116. line 27, and return to the main program.
  117.  
  118.                                                      Page 5-2
  119.  
  120.                Chapter 5 - Functions, Variables, & Prototypes
  121.  
  122.  
  123. MORE ABOUT PASSING A VALUE TO A FUNCTION
  124. ____________________________________________________________
  125.  
  126. When we passed the value of the variable named index to the
  127. function, a little more happened than meets the eye.  We did
  128. not actually pass the value of index to the function, we
  129. actually passed a copy of the value.  In this way the original
  130. value is protected from accidental corruption by a called
  131. function.  We could have modified the variable named number
  132. in any way we wished in the function named square(), and when
  133. we returned to the main program, the variable named index
  134. would not have been modified.  We thus protect the value of
  135. a variable in the main program from being accidentally
  136. corrupted, but we cannot return a value to the main program
  137. from a function using this technique.  We will find a well
  138. defined method of returning values to the main program or to
  139. any calling function when we get to arrays and another method
  140. when we get to pointers.  Until then the only way you will be
  141. able to communicate back to the calling function will be with
  142. global variables.  We have already hinted at global variables
  143. above, and will discuss them in detail later in this chapter.
  144.  
  145. Continuing in the main program, we come to the last function
  146. call, the call to the function named ending() in line 11. 
  147. This line calls the last function which has no local variables
  148. defined.  It prints out a message with the value of the
  149. variable sum contained in it to end the program.  The program
  150. ends by returning to the main program and finding nothing else
  151. to do.  Compile and run this program and observe the output.
  152.  
  153.  
  154. NOW TO CONFESS A LITTLE LIE
  155. ____________________________________________________________
  156.  
  157. I told you a short time ago that the only way   =============
  158. to get a value back to the main program was       SQUARES.C
  159. through use of a global variable, but there     =============
  160. is another way which we will discuss after
  161. you load and display the file named
  162. SQUARES.C.  In this file we will see that it is simple to
  163. return a single value from a called function to the calling
  164. function.  But once again, it is true that to return more than
  165. one value, we will need to study either arrays or pointers.
  166.  
  167. In the main program, we define two integers and begin a for
  168. loop in line 6 which will be executed 8 times.  The first
  169. statement of the for loop is "y = squ(x);", which is a new and
  170. rather strange looking construct.  From past experience, we
  171. should have no trouble understanding that the squ(x) portion
  172. of the statement is a call to the function named squ() taking
  173. along the value of x as a variable.  Looking ahead to line 15
  174. of the function itself, we find that the function prefers to
  175. call the input variable in, and it proceeds to square the
  176.  
  177.                                                      Page 5-3
  178.  
  179.                Chapter 5 - Functions, Variables, & Prototypes
  180.  
  181. value of in and call the result square.  Finally, a new kind
  182. of a statement appears in line 21, the return statement.  The
  183. value within the parentheses is assigned to the function
  184. itself and is returned as a usable value in the main program. 
  185. Thus, the function call "squ(x)" is assigned the value of the
  186. square and returned to the main program such that the variable
  187. named y is then set equal to that value.  If the variable
  188. named x were therefore assigned the value 4 prior to this
  189. call, y would then be set to 16 as a result of this line of
  190. code.
  191.  
  192. Another way to think of this is to consider the grouping of
  193. characters squ(x) as another variable with a value that is the
  194. square of x, and this new variable can be used any place it
  195. is legal to use a variable of its type.  The values of the
  196. variables x and y are then printed out.
  197.  
  198. To illustrate that the grouping of squ(x) can be thought of
  199. as just another variable, another for loop is introduced in
  200. line 11 in which the function call is placed in the printf()
  201. statement rather than assigning it to a new variable.
  202.  
  203. One last point must be made, the type of variable returned
  204. must be defined in order to make sense of the data, but the
  205. compiler will default the type to integer if none is
  206. specified.  If any other type is desired, it must be
  207. explicitly defined.  How to do this will be demonstrated in
  208. the next example program.
  209.  
  210. Be sure to compile and run this program which also uses the
  211. classic method of defining function variables.
  212.  
  213.  
  214. FLOATING POINT FUNCTIONS
  215. ____________________________________________________________
  216.  
  217. Load the program FLOATSQ.C for an example of    =============
  218. a function with a floating point type of          FLOATSQ.C
  219. return.  It begins by defining a global         =============
  220. floating point variable named z which we will
  221. use later.  Then in the main part of the
  222. program, an integer is defined, followed by two floating point
  223. variables, and then by two strange looking definitions.  The
  224. expressions sqr() and glsqr() look like function calls and
  225. they are.  This is the proper way in C to define that a
  226. function will return a value that is not of type int, but of
  227. some other type, in this case float.  This tells the compiler
  228. that when a value is returned from either of these two
  229. functions, it will be of type float.  This is, once again, the
  230. classic method of defining functions.
  231.  
  232. Now refer to the function named sqr() starting in line 22 and
  233. you will see that the function name is preceded by the keyword
  234. float.  This is an indication to the compiler that this
  235.  
  236.                                                      Page 5-4
  237.  
  238.                Chapter 5 - Functions, Variables, & Prototypes
  239.  
  240. function will return a value of type float to any program that
  241. calls it.  The function is now compatible with the call to it. 
  242. The line following the function name contains float inval;,
  243. which indicates to the compiler that the variable passed to
  244. this function from the calling program will be of type float.
  245.  
  246. The function named glsqr() beginning in line 31, will also
  247. return a float type variable, but it uses a global variable
  248. for input.  It does the squaring right within the return
  249. statement and therefore has no need to define a separate
  250. variable to store the product.
  251.  
  252. The overall structure of this program should pose no problem
  253. and will not be discussed in any further detail.  As is
  254. customary with all example programs, compile and run this
  255. program.
  256.  
  257.  
  258. THE CLASSIC STYLE
  259. ____________________________________________________________
  260.  
  261. The three programs we have studied in this chapter so far use
  262. the classic style of function definition.  Although this style
  263. was the first defined for C, it is rapidly being replaced with
  264. the modern method of function definition because it does so
  265. much for you in detecting and flagging errors.  As you read
  266. articles on C, you will see programs written in the classic
  267. style, so you need to be capable of reading them.  It would
  268. be highly recommended, however, that you learn and use the
  269. modern method which will be covered shortly in this tutorial.
  270.  
  271. The remainder of this tutorial will use the modern method as
  272. recommended and defined by the ANSI standard.  If you have an
  273. older compiler, it may not work on some of these files and it
  274. will be up to you to modify the programs as needed to conform
  275. to the classic style.
  276.  
  277.  
  278. SCOPE OF VARIABLES
  279. ____________________________________________________________
  280.  
  281. Load the next program, SCOPE.C, and display     =============
  282. it for a discussion of the scope of variables      SCOPE.C
  283. in a program.  You can ignore the 4             =============
  284. statements in lines 2 through 5 of this
  285. program for a few moments.  We will discuss
  286. them later.
  287.  
  288. The first variable defined is a global variable named count
  289. which is available to any function in the program since it is
  290. defined before any of the functions.  It is always available
  291. because it does not come and go as the program is executed. 
  292. (That will make sense shortly.)  Farther down in the program,
  293. another global variable named counter is defined in line 25
  294.  
  295.                                                      Page 5-5
  296.  
  297.                Chapter 5 - Functions, Variables, & Prototypes
  298.  
  299. which is also global but is not available to the main program
  300. since it is defined following the main program.  A global
  301. variable is any variable that is defined outside of any
  302. function.  Note that both of these variables are sometimes
  303. referred to as external variables because they are external
  304. to any functions, and they are sometimes called file
  305. variables.
  306.  
  307. Return to the main program and you will see the variable named
  308. index defined as an integer in line 11.  Ignore the word
  309. register for the moment.  This variable is only available
  310. within the main program because that is where it is defined. 
  311. In addition, it is an automatic variable, which means that it
  312. only comes into existence when the function in which it is
  313. contained is invoked, and ceases to exist when the function
  314. is finished.  This really means nothing here because the main
  315. program is always in operation, even when it gives control to
  316. another function.  Another integer is defined within the for
  317. braces named stuff.  Any pairing of braces can contain a
  318. variable definition which will be valid and available only
  319. while the program is executing statements within those braces. 
  320. The variable will be an automatic variable and will cease to
  321. exist when execution leaves the braces.  This is convenient
  322. to use for a loop counter or some other very localized
  323. variable.
  324.  
  325.  
  326. MORE ON AUTOMATIC VARIABLES
  327. ____________________________________________________________
  328.  
  329. Observe the function named head1() in line 26 which looks a
  330. little funny because of void being used twice.  The purpose
  331. of the uses of the word void will be explained shortly. The
  332. function contains a variable named index, which has nothing
  333. to do with the variable named index in line 11 of the main
  334. program, except that both are automatic variables.  When the
  335. program is not actually executing statements in this function,
  336. this variable named index does not even exist.  When head1()
  337. is called, the variable is generated, and when head1()
  338. completes its task, the variable in head1() named index is
  339. eliminated completely from existence.  (The automatic variable
  340. is stored on the stack.  This topic will be covered later.) 
  341. Keep in mind however that this does not affect the variable
  342. of the same name in the main program, since it is a completely
  343. separate entity.
  344.  
  345. Automatic variables therefore, are automatically generated and
  346. disposed of when needed.  The important thing to remember is
  347. that from one call of a function to the next call, the value
  348. of an automatic variable is not preserved and must therefore
  349. be reinitialized.
  350.  
  351.  
  352.  
  353.  
  354.                                                      Page 5-6
  355.  
  356.                Chapter 5 - Functions, Variables, & Prototypes
  357.  
  358. WHAT ARE STATIC VARIABLES?
  359. ____________________________________________________________
  360.  
  361. An additional variable type must be mentioned at this point,
  362. the static variable.  By putting the keyword static in front
  363. of a variable definition within a function, the variable or
  364. variables in that definition are static variables and will
  365. stay in existence from call to call of the particular
  366. function. 
  367.  
  368. By putting the same keyword in front of an external variable,
  369. one outside of any function, it makes the variable private and
  370. not accessible to use in any other file.  (This is a
  371. completely different use of the same keyword.)  This implies
  372. that it is possible to refer to external variables in other
  373. separately compiled files, and that is true.  Examples of this
  374. usage will be given in chapter 14 of this tutorial.
  375.  
  376.  
  377. USING THE SAME NAME AGAIN
  378. ____________________________________________________________
  379.  
  380. Refer to the function named head2().  It contains another
  381. definition of the variable named count.  Even though count has
  382. already been defined as a global variable, it is perfectly all
  383. right to reuse the name in this function.  It is a completely
  384. new variable that has nothing to do with the global variable
  385. of the same name, and causes the global variable to be
  386. unavailable in this function.  This allows you to write
  387. programs using existing functions without worrying about what
  388. names were used for variables in the functions because there
  389. can be no conflict.  You only need to worry about the
  390. variables that interface with the functions.
  391.  
  392.  
  393. WHAT IS A REGISTER VARIABLE?
  394. ____________________________________________________________
  395.  
  396. Now to fulfill a promise made earlier about what a register
  397. variable is.  A computer can keep data in a register or in
  398. memory.  A register is much faster in operation than memory
  399. but there are very few registers available for the programmer
  400. to use.  If there are certain variables that are used
  401. extensively in a program, you can designate that those
  402. variables are to be stored in a register in order to speed up
  403. the execution of the program.  The method of doing this is
  404. illustrated in line 11.  Your compiler probably allows you to
  405. use one or more register variables and will ignore additional
  406. requests if you request more than are available.  The
  407. documentation for your compiler will list how many registers
  408. are available with your compiler.  It will also inform you of
  409. what types of variables can be stored in a register.  If your
  410. compiler does not allow the use of register variables, the
  411. register request will simply be ignored.
  412.  
  413.                                                      Page 5-7
  414.  
  415.                Chapter 5 - Functions, Variables, & Prototypes
  416.  
  417.  
  418. WHERE DO I DEFINE VARIABLES?
  419. ____________________________________________________________
  420.  
  421. Now for a refinement on a general rule stated earlier.  When
  422. you have variables brought to a function as arguments to the
  423. function, and you are using the classic style of programming,
  424. they are defined immediately after the function name and prior
  425. to the opening brace for the executable statements.  Other
  426. variables used in the function are defined at the beginning
  427. of the function, immediately following the opening brace of
  428. the function, and before any executable statements.
  429.  
  430.  
  431. WHAT IS PROTOTYPING?
  432. ____________________________________________________________
  433.  
  434. A prototype is a model of a real thing and when programming
  435. with a good up-to-date C compiler, you have the ability to
  436. define a model of each function for the compiler.  The
  437. compiler can then use the model to check each of your calls
  438. to the function and determine if you have used the correct
  439. number of arguments in the function call and if they are of
  440. the correct type.  By using prototypes, you let the compiler
  441. do some additional error checking for you.  The ANSI standard
  442. for C contains prototyping as part of its recommended
  443. standard.  Every good C compiler will have prototyping
  444. available, so you should learn to use it.  Much more will be
  445. said about prototyping throughout the remainder of this
  446. tutorial.
  447.  
  448. Returning to lines 3, 4, and 5 in SCOPE.C, we have the
  449. prototypes for the three functions contained within the
  450. program.  The first void in each line tells the compiler that
  451. these particular functions do not return a value, so that the
  452. compiler would flag the statement index = head1(); as an error
  453. because nothing is returned to assign to the variable named
  454. index.  The word void within the parentheses tells the
  455. compiler that this function requires no parameters and if a
  456. variable were included, it would be an error and the compiler
  457. would issue a warning message.  If you wrote the statement
  458. head1(index);, it would be a error.  This allows you to use
  459. type checking when programming in C in much the same manner
  460. that it is used in Pascal, Modula-2, or Ada, although the type
  461. checking in C is very weak.
  462.  
  463. Note the addition of the word void in line 9.  This is an
  464. indication to the system that we do not plan to return a value
  465. to the operating system when we terminate operation of this
  466. program.  The main program also can return a value in the same
  467. manner as any other function.
  468.  
  469. You should enable prototype checking at this time, if it is
  470. available with your compiler.  Check your documentation for
  471.  
  472.                                                      Page 5-8
  473.  
  474.                Chapter 5 - Functions, Variables, & Prototypes
  475.  
  476. the details of how to do it.  Prototyping will be used
  477. throughout the remainder of this tutorial.  If your compiler
  478. does not support prototyping and the modern method of function
  479. definition, you will have to modify the remaining example
  480. programs.  A much better solution would be to purchase a
  481. better compiler.
  482.  
  483. Line 2 of SCOPE.C tells the system to go to the include files
  484. and get the file named STDIO.H which contains the prototypes
  485. for the standard input and output functions so they can be
  486. checked for proper variable types.  Don't worry about the
  487. include yet, it will be covered in detail later in this
  488. tutorial.  Be sure to compile and execute this program. 
  489.  
  490.  
  491. STANDARD FUNCTION LIBRARIES
  492. ____________________________________________________________
  493.  
  494. Every compiler comes with some standard predefined functions
  495. which are available for your use.  These are mostly
  496. input/output functions, character and string manipulation
  497. functions, and math functions.  We will cover most of these
  498. in subsequent chapters.  Prototypes are defined for you by the
  499. writer of your compiler for all of the functions that are
  500. included with your compiler.  A few minutes spent studying
  501. your reference guide will give you an insight in where the
  502. prototypes are defined for each of the functions.
  503.  
  504. Most compilers have additional functions predefined that are
  505. not standard but allow the programmer to get the most out of
  506. his particular computer.  In the case of the IBM-PC and
  507. compatibles, most of these functions allow the programmer to
  508. use the BIOS services available in the operating system, or
  509. to write directly to the video monitor or to any place in
  510. memory.  These will not be covered in any detail as you will
  511. be able to study these unique aspects of your compiler on your
  512. own.  Many of these kinds of functions are used in the example
  513. programs in chapter 14.
  514.  
  515.  
  516.  
  517. WHAT IS RECURSION?
  518. ____________________________________________________________
  519.  
  520. Recursion is another of those programming      ==============
  521. techniques that seem very intimidating the       RECURSON.C
  522. first time you come across it, but if you      ==============
  523. will load and display the example program
  524. named RECURSON.C, we will take all of the
  525. mystery out of it.  This is probably the simplest recursive
  526. program that it is possible to write and it is therefore a
  527. stupid program in actual practice, but for purposes of
  528. illustration, it is excellent.
  529.  
  530.  
  531.                                                      Page 5-9
  532.  
  533.                Chapter 5 - Functions, Variables, & Prototypes
  534.  
  535. Recursion is nothing more than a function that calls itself. 
  536. It is therefore in a loop which must have a way of
  537. terminating.  In the program on your monitor, the variable
  538. named index is set to 8 in line 9, and is used as the argument
  539. to the function named count_dn().  The function simply
  540. decrements the variable, prints it out in a message, and if
  541. the variable is not zero, it calls itself, where it decrements
  542. the variable again, prints it, etc. etc. etc.  Finally, the
  543. variable will reach zero, and the function will not call
  544. itself again.  Instead, it will return to the prior time it
  545. called itself, and return again, until finally it will return
  546. to the main program and from there return to DOS.
  547.  
  548. For purposes of understanding you can think of it as having
  549. 8 copies of the function named count_dn() available and it
  550. simply called all of them one at a time, keeping track of
  551. which copy it was in at any given time.  That is not what
  552. actually happened, but it is a reasonable illustration for you
  553. to begin understanding what it was really doing.
  554.  
  555.  
  556. WHAT DID IT DO?
  557. ____________________________________________________________
  558.  
  559. A better explanation of what actually happened is in order. 
  560. When you called the function from itself, it stored all of the
  561. variables and all of the internal flags it needs to complete
  562. the function in a block somewhere.  The next time it called
  563. itself, it did the same thing, creating and storing another
  564. block of everything it needed to complete that function call. 
  565. It continued making these blocks and storing them away until
  566. it reached the last function when it started retrieving the
  567. blocks of data, and using them to complete each function call. 
  568. The blocks were stored on an internal part of the computer
  569. called the stack.  This is a part of memory carefully
  570. organized to store data just as described above.  It is beyond
  571. the scope of this tutorial to describe the stack in detail,
  572. but it would be good for your programming experience to read
  573. some material describing the stack.  A stack is used in nearly
  574. all modern computers for internal housekeeping chores.
  575.  
  576. In using recursion, you may desire to write a program with
  577. indirect recursion as opposed to the direct recursion
  578. described above.  Indirect recursion would be when a function
  579. A calls the function B, which in turn calls A, etc.  This is
  580. entirely permissible, the system will take care of putting the
  581. necessary things on the stack and retrieving them when needed
  582. again.  There is no reason why you could not have three
  583. functions calling each other in a circle, or four, or five,
  584. etc.  The C compiler will take care of all of the details for
  585. you.
  586.  
  587. The thing you must remember about recursion is that at some
  588. point, something must go to zero, or reach some predefined
  589.  
  590.                                                     Page 5-10
  591.  
  592.                Chapter 5 - Functions, Variables, & Prototypes
  593.  
  594. point to terminate the loop.  If not, you will have an
  595. infinite loop, and the stack will fill up and overflow, giving
  596. you an error and stopping the program rather abruptly.
  597.  
  598. Be sure to compile and run this program. 
  599.  
  600.  
  601. ANOTHER EXAMPLE OF RECURSION
  602. ____________________________________________________________
  603.  
  604. The program named BACKWARD.C is another        ==============
  605. example of recursion, so load it and display     BACKWARD.C
  606. it on your screen.  This program is similar    ==============
  607. to the last one except that it uses a
  608. character array.  Each successive call to the
  609. function named  forward_and_backwards() causes one character
  610. of the message to be printed.  Additionally, each time the
  611. function ends, one of the characters is printed again, this
  612. time backwards as the string of recursive function calls is
  613. retraced.
  614.  
  615. This program uses the modern method of function definition and
  616. includes full prototype definitions.  The modern method of
  617. function definition moves the types of the variables into the
  618. parentheses along with the variable names themselves.  The
  619. final result is that the line containing the function
  620. definition looks more like the corresponding line in Pascal,
  621. Modula-2, or Ada.  The prototype in line 5 is simply a copy
  622. of the function header in line 18 followed by a semicolon. 
  623. The designers of C even allow you to include a variable name
  624. along with each type.  The name is ignored by the compiler but
  625. including the name in the prototype could give you a good idea
  626. of how the variable is used, acting like a comment.
  627.  
  628. Don't worry about the character array defined in line 9 or the
  629. other new material presented here.  After you complete chapter
  630. 7 of this tutorial, this program will make sense.  It was felt
  631. that introducing a second example of recursion was important
  632. so this file is included here.
  633.  
  634. Compile and run this program with prototype checking enabled
  635. and observe the results.
  636.  
  637.  
  638. IF YOU CAN'T USE PROTOTYPING WITH YOUR COMPILER
  639. ____________________________________________________________
  640.  
  641. If your compiler does not support prototyping remove line 5
  642. from BACKWARD.C and replace line 18 with the following lines;
  643.  
  644.    forward_and_backwards(line_of_char, index)
  645.    char line_of_char[];
  646.    int index;
  647.  
  648.  
  649.                                                     Page 5-11
  650.  
  651.                Chapter 5 - Functions, Variables, & Prototypes
  652.  
  653. Load and display the program named FLOATSQ2.C  =============
  654. which is an exact copy of the program             FLOATSQ2.C
  655. FLOATSQ.C which we considered earlier with      =============
  656. prototyping added.  The use of prototyping is
  657. a good practice for all C programmers to get
  658. into.
  659.  
  660. Several things should be mentioned about this program.  First,
  661. the word float at the beginning of lines 27 and 35 indicate
  662. to the compiler that these functions are functions that return
  663. float type values.  Also, since the prototypes are given
  664. before main, the functions are not required to be identified
  665. in line 12 as they were in line 7 of FLOATSQ.C earlier in this
  666. chapter. 
  667.  
  668. Notice also that the type of the variable named inval is
  669. included within the parentheses in line 27.  It would be very
  670. educational for you to modify this program so that you
  671. included a call to sqr() with a variable of type int within
  672. the parentheses to see what kind of a warning you would get. 
  673. Do the same thing in the program without prototype checking,
  674. FLOATSQ.C.
  675.  
  676.  
  677. PROGRAMMING EXERCISES
  678. ____________________________________________________________
  679.  
  680. 1.   Rewrite  TEMPCONV.C, from an earlier chapter, and move
  681.      the temperature calculation to a function.
  682.  
  683. 2.   Write a program that writes your name on the monitor 10
  684.      times by calling a function to do the writing. Move the
  685.      called function ahead of the main function to see if your
  686.      C compiler will allow it.
  687.  
  688. 3.   Add prototyping to the programs named SUMSQRES.C and
  689.      SQUARES.C, and change the function definitions to the
  690.      modern method.
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.                                                     Page 5-12
  705.