Chapter 3

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6          Overloading Function Names

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6    Overloading Function Names

 

Introduction to Functions

  

Programs are normally made up of subparts. 

 

There may be a subpart to handle data input,

 

another subpart to handle the processing of the data

 

and another subpart to handle data output. . .

 

 

These subparts or subprograms are given a name and are considered as a separate entity. They are known as functions.

 

Functions are executed simply by calling their name. 

 

Functions can receive data to be processed within the function, and . . .

 

. . . functions can return data to the part of the program that called the function.

 

Examples

Running under Dev-C++

 

funcs00.cpp -  example program with no functions

funcs01.cpp  -  using a function

funcs02.cpp  -  passing an int to a function

funcs03.cpp  -  passing an int and a double 

funcs04.cpp  -  returning a value back from a function

funcs05.cpp  -  using the predefined function sqrt()

funcs06.cpp  -  using a global constant

 

Running under Borland version 5.02

 

funcs00_Borland.cpp -  example program with no functions

funcs01_Borland.cpp  -  using a function

funcs02_Borland.cpp  -  passing an int to a function

funcs03_Borland.cpp  -  passing an int and a double 

funcs04_Borland.cpp  -  returning a value back from a function

funcs05_Borland.cpp  -  using the predefined function sqrt()

funcs06_Borland.cpp  -  using a global constant

 

A program with no functions

http://www.annedawson.com/funcs00.cpp

 

A program which uses a function

http://www.annedawson.com/funcs01.cpp

 

Passing an int to a function

http://www.annedawson.com/funcs02.cpp

 

Passing an int and a double to a function

http://www.annedawson.com/funcs03.cpp

 

Returning a value back from a function

http://www.annedawson.com/funcs04.cpp

 

Using the predefined function sqrt()

http://www.annedawson.com/funcs05.cpp

 

Using a global constant

http://www.annedawson.com/funcs06.cpp

 

 

 

 

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

Top-Down Design

  

Top-down design is breaking down a problem into smaller and smaller problems, until the smallest problem can be implemented in a function in C++  which fits onto a single screen of the code editor...

 

Program design known as structured program design or top-down program design requires that programming problems are broken down into small problems which are executed one at a time. . .

  

The goal of top-down design is to break a problem into individual tasks, or modules, that can easily be transcribed into pseudocode, flowcharts or a program.

  

Modules continue to be broken down into further modules, until at the lowest level, the modules are small enough to be coded into a single function which performs a single task.

 

Structured Programming

  

A structured program is one which is based on a modular design and uses only the three types of control structures : sequences, decisions and loops

 

Advantages of Structured Programs

 

      Easy to write

      Easy to debug

      Easy to understand

      Easy to change

 

 

 

 

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

 

Predefined Functions

 

 

We have already seen the use of the predefined function sqrt()in program

http://www.annedawson.com/funcs05.cpp

 

 

Predefined functions are provided for you by the C++ language. 

 

The definitions of predefined functions (the predefined function code - the C++ statements which make up the functions) are contained  in a library header file. 

 

The header file must be  included in your program code with a #include statement

sqrt()    -    square root function

    Receives:         a double argument       

    Returns:           a double value

    Example:         sqrt(4.0)

    Value:              2.0

    Library:            <math.h>  or <cmath>

   Example program: 03-01.cpp

The square root function sqrt()

 

 

 

Contents of Chapter 3

 

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

Programmer-defined functions

 

funcs00.cpp -  example program with no functions

funcs01.cpp  -  using a function

funcs02.cpp  -  passing an int to a function

funcs03.cpp  -  passing an int and a double 

funcs04.cpp  -  returning a value back from a function

funcs05.cpp  -  using the predefined function sqrt()

funcs06.cpp  -  using a global constant

 

 

 

 

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

 

 

 

Procedural Abstraction

 

treating functions as black boxes

also known as information hiding

also known as procedural abstraction

it means that the function prototype and its comments should be all a programmer needs to know in order to use the function

 

All variables used by the function should be declared inside the function

 

 

Implementation of Procedural Abstraction

 

You can save your function definitions in a separate file, and compile the file separately, before linking it to the other files to generate the executable file. 

Implementation of Procedural Abstraction

The file that contains the main function of the program also contains just the functions’ prototypes.

 

Implementation of Procedural Abstraction

This way, you can hide the details of the functions, maintaining procedural abstraction.

 

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

 

Local Variables

 

Variables that are declared inside a function definition are known as local variables.

 

A variable of the same name in the main function is a different variable!

 

A function’s call-by-value formal parameters are variables which are local to the function definition. .   in other words, they are local variables

 

 

 

 

Scope

  

The scope of a variable is that part of the program where it can be used.

 

Local Scope

http://www.annedawson.com/local.cpp

 

Global Scope

http://www.annedawson.com/global.cpp

 

Global constants

                  

const double PI = 3.142;

int main ()

{

 

}

http://www.annedawson.com/constant.cpp

 

 

Global variables

 

    // DO NOT DO THIS:

 

  int number = 10;

  int main ()

 {

 

 }

 

 

 

 

 

 

 

 

 

 

Contents of Chapter 3

 

3.0     Introduction to Functions

3.1     Top-Down Design

3.2     Predefined Functions

3.3     Programmer-defined Functions

3.4     Procedural Abstraction

3.5     Local Variables

3.6     Overloading Function Names

 

 

Overloading Function Names

If you have two or more function definitions for the same function name, that is called overloading.

 

  

 

When you overload a function name, the function definitions must have different numbers of formal parameters, or some formal parameters of different data types.

 

 

Calling an overloaded function

  

When there is a function call, the compiler uses the function definition whose number of formal parameters and types of formal parameters match the arguments in the function call.

 

Overloaded function program

 

http://www.annedawson.com/03-15.cpp

 

(Text book references can be seen at the end of this presentation.)

 

 

Polymorphism

  

Polymorphism means “many forms” and is an important term in C++.

 

Function overloading is our first example of polymorphism. 

 

This Presentation uses the following files:

 

                 5th Ed   4th Ed   3rd Ed   2nd Ed

funcs00.cpp    ----    ----     ----     ----

funcs01.cpp    ----    ----     ----     ----

funcs02.cpp    ----    ----     ----     ----

funcs03.cpp    ----    ----     ----     ----

funcs04.cpp    ----    ----     ----     ----

funcs05.cpp    ----    ----     ----     ----

funcs06.cpp    ----    ----     ----     ----

03-01.cpp              p113     p111     p109

funcs07.cpp    ----    ----     ----     ----

local.cpp      ----    ----     ----     ----

global.cpp     ----    ----     ----     ----

constant.cpp   ----    ----     ----     ----

03-15.cpp              p159     p157     p152

 

This Presentation uses the following files:

 

                      5th Ed   4th Ed   3rd Ed   2nd Ed

funcs00_borland.cpp    ----    ----     ----     ----

funcs01_borland.cpp    ----    ----     ----     ----

funcs02_borland.cpp    ----    ----     ----     ----

funcs03_borland.cpp    ----    ----     ----     ----

funcs04_borland.cpp    ----    ----     ----     ----

funcs05_borland.cpp    ----    ----     ----     ----

funcs06_borland.cpp    ----    ----     ----     ----

03-01_borland.cpp              p113     p111     p109

funcs07_borland.cpp    ----    ----     ----     ----

local_borland.cpp      ----    ----     ----     ----

global_borland.cpp     ----    ----     ----     ----

constant_borland.cpp   ----    ----     ----     ----

03-15_borland.cpp              p159     p157     p152

 

End of CPP_Functions1_Ch03.ppt

 

Last Updated: Saturday 21st January 2006, 9:21 PT, AHD