Title: CSCI101 An Introduction to Programming using C
1CSCI101 An Introduction to Programming using
C
2Functions 1
3Procedural Abstraction and Functions that Return
a Value
4Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
5Introduction to Functions
-
- Programs are normally made up of subparts.
6Introduction to Functions
-
- 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. . .
7Introduction to Functions
- 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.
8Introduction to Functions
- Functions can receive data to be processed
within the function, and . . .
9Introduction to Functions
- . . . functions can return data to the part of
the program that called the function.
10Introductory Example ProgramsRunning 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
11Introductory Example ProgramsRunning 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
12A program with no functions
- http//www.annedawson.com/funcs00.cpp
13A program which uses a function
http//www.annedawson.com/funcs01.cpp
14Passing an int to a function
http//www.annedawson.com/funcs02.cpp
15Passing an int and a double to a function
http//www.annedawson.com/funcs03.cpp
16Returning a value back from a function
- http//www.annedawson.com/funcs04.cpp
17Using the predefined function sqrt()
- http//www.annedawson.com/funcs05.cpp
18Using a global constant
- http//www.annedawson.com/funcs06.cpp
19Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
20Top-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...
21Top-Down Design
-
- 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. . .
22Top-Down Design
-
- 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.
23Top-Down Design
24(No Transcript)
25Top-Down Design
-
- 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.
26Structured 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
27Advantages of Structured Programs
- Easy to write
- Easy to debug
- Easy to understand
- Easy to change
28Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
29Predefined Functions
-
- We have already seen the use of the predefined
function sqrt()in program
http//www.annedawson.com/funcs05.cpp
30Predefined Functions
-
- Predefined functions are provided for you by
the C language.
31Predefined Functions
- The definitions of predefined functions (the
predefined function code - the C statements
which make up the functions) are contained in a
library header file.
32Predefined Functions
- The header file must be included in your
program code with a include statement
33sqrt() - square root function
- Receives a double argument
- Returns a double value
- Example sqrt(4.0)
- Value 2.0
- Library ltmath.hgt or ltcmathgt
- Example program Display 4.1 page 183
34Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
35Programs which use 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
36Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
37Procedural 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
38Implementation 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.
39Implementation of Procedural Abstraction
- The file that contains the main function of the
program also contains just the functions
prototypes. -
40Implementation of Procedural Abstraction
- This way, you can hide the details of the
functions, maintaining procedural abstraction.
41Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
42Local 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!
43Local Variables
- A functions call-by-value formal parameters
are variables which are local to the function
definition. . . - in other words, they are local variables
44Scope
-
- The scope of a variable is that part of the
program where it can be used.
45Local and Global Scope
http//www.annedawson.com/funcs07.cpp
46Local Scope
- http//www.annedawson.com/local.cpp
47Global Scope
- http//www.annedawson.com/global.cpp
48Global constants
-
- const double PI 3.142
- int main ()
-
Good!
49Global Constant
- http//www.annedawson.com/constant.cpp
50Global variables
- // DO NOT DO THIS
- int number 10
- int main ()
-
-
Bad!!!!!
51Contents of Functions 1
- 0 Introduction to Functions
- 1 Top-Down Design
- 2 Predefined Functions
- 3 Programmer-defined Functions
- 4 Procedural Abstraction
- 5 Local Variables
- 6 Overloading Function Names
52Overloading Function Names
- If you have two or more function definitions
for the same function name, that is called
overloading. -
53Overloading Function Names
-
- When you overload a function name, the
function definitions must have different numbers
of formal parameters, or some formal parameters
of different data types.
54Calling 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.
55Overloaded function program
56Polymorphism
-
- Polymorphism means many forms and is an
important term in C. Function overloading is
our first example of polymorphism.
57End of CPP_Functions1.ppt
58- Last updated Friday 24th March 2006, 1347 PT,
AHD