In your own words discuss similarities and differences between a function and a small program.
What will be an ideal response?
A function definition is like a small program. Differences are that a program uses input, say cin, for input, or an fstream object, whereas a function uses parameters for inputs. Similarities are many. A program uses cout for output to the screen. A function can use cout to output to the screen. The function can return a value to the caller using a return statement. The program can, in fact, do this too, but such values can only be success codes. The function and the program each have bodies of code that call other functions and do computation to produce the values to satisfy specified. Each has local variables that are declared within the body. Finally, each can use global constants and variables (which are declared outside any function.)
You might also like to view...
Which of the following is the number of bits used for IPv6?
A. 128 B. 32 C. 64 D. 256
In principle, recursion is never necessary. It can always be replaced by an iterative construct, such as while or until. Rewrite makepath (page 502) as a nonrecursive function. Which version do you prefer? Why?
What will be an ideal response?