[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci201] / functions
[Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Oct 8 09:59:00 PDT 2007

Contents


    Introducing Functions in C++

      Introduction

      This page is a quick summary of things you may need to know about C++ functions at the CS201 (CS1) level. With out these facts you will make more mistakes and will not be able to use the full power of C++ to make your programming easier.

      Basics

      A C++ function is a sub-program. It is a small, named piece of a program that is called up from other parts of the same program.

      A function must be defined once and can be called any number of times. A function has a name, arguments, and a body. A common problem with programs is that a function call can not be matched up to any function in the rest of the program.

      The body is compiled and stored in memory. When the call is executed the computer jumps to the start of the body. Once in the body it executes its code until it gets to the end or a 'return' statement. If it executes an "exit" statement the whole program terminates. If the computer gets to a return or the end of the function then it jumps to the instruction in the program immediately after the call.

      The Main Function

      All executable programs must have a special function with name "main" and special arguments. The program run starts when the operating system calls this "main" function.

      The main program should return an positive int that indicates if any errors have been discovered by the program. The number zero indicates that zero errors have occurred. A number other than zero indicates that the program did not complete its task and the precise number often is used to indicate the reason for the program failing. The operating system has the power to find out this returned number.

      Compilation

      The compiler has to match up the function call with the right function. It uses the name of the function plus the number and type of the arguments in the call. to find a matching function. If no function is found that has the right name and arguments then the compiler reports an error and the program can not be run.

      A function definition has two parts: a header and a body. The header specifies how the function can be called. The body defines what happens if and when the function is called. We say that the header specifies the function. The complete body and header is said to be an implementation of the function.

      In C++ there is a special syntax for giving the header without defining the body: The body is replaced by a semicolon. The result is called a function prototype, or specification. A file that contains a series of function prototypes is called a header file. Typically a compiler learns about the functions you plan to use in a program file by compiling a series of these header files #included at the start of the program. These are often predefined library files that come with the compiler. Here, for example, [ cmath.txt ] is a simplified version of our own <cmath> library.

      Handling arguments

        Call by Value

      1. The normal argument is an expression that is evaluated before the function is called. The resulting value is the initial value of the variable listed in the functiuon header.

        Call by Reference

        Some arguments are called by reference. These must be variables not expressions. They are not evaluated. The function is instead given the address of the variables and uses these in place of the symbol in the function header.

        A call by reference is shown in the header by adding an ampersand symbol after the type:

                 type name (...., type & argument, ....)

        However this also permits the function to change the arguments. This can make it difficult to find out what is going on in a program. It is therefore possible to indicate that an argument is called by reference but is safe from being changed:

                 type name (...., const type & argument, ....)

      . . . . . . . . . ( end of section Handling arguments) <<Contents | End>>

      Return Values

      A function header can specify that a value must and will be returned by a function. If so there must be one or more return_statement's that contain an expression. If the expression is executed than the expression is evaluated and the value left behind (on the runtime stack) for the calling program to collect and use.

      Overloading

      A C++ function is matched, by the compiler+linker, to its calls by using both its name and the types of its arguments. So the same name can be used for several similar functions with different types of data.

      This works well as long as the names you choose actually reflect what the functions do.

      Jargon

    1. int::data_type=used to store integers -- whole numbers, use for counting things.
    2. float::data_type=used for real numbers when precision is not important, good for old or small computers.
    3. double::=data_type=used for real numbers when precision is important, use for all measurements.
    4. char::data_type=used to store a single 7, 8 or 9 bit character.
    5. string::data_type=used to store a sequence of numbered char.

    6. data_type::=a collection of objects that are stored in similar formats and have the same operations and operators.
    7. function_header::=the part of the function that comes first and describes how to call it.
    8. function_body::=the part of a function between braces that is after the header.

    . . . . . . . . . ( end of section Introducing Functions in C++) <<Contents | End>>

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End