Wrox Press C++ Tutorial
Summary
In this chapter, you've learned about the basics of program structure. You should have a good grasp of how functions are defined, how data can be passed to a function and how results are returned to a calling program. Functions are fundamental to programming in C++, so everything we do from here on will involve using multiple functions in a program. The key points that you should keep in mind about writing your own functions are these:
-
Functions should be compact units of code with a well-defined purpose. A typical program will consist of a large number of small functions, rather than a small number of large functions.
-
Always provide a function prototype for each function defined in your program, positioned before you call that function.
-
Passing values to a function using a reference can avoid the copying implicit in the call-by-value transfer of arguments. Parameters that are not modified in a function should be specified as
const.
-
When returning a reference or a pointer from a function, ensure that the object being returned has the correct scope. Never return a pointer or a reference to an object which is local to a function.
The use of references as arguments is a very important concept, so make sure you are confident about using them. We'll see a lot more about references as arguments to functions when we look into object-oriented programming.
© 1998 Wrox Press