Sie sind auf Seite 1von 33

Introduction to

Function & Array


COMPUTER PROGRAMMING
BFC 20802
Part 1: Function
Modular Programming

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Modular Programming
 Modular programming: breaking a program up into smaller,
manageable functions or modules

 Function: a collection of statements to perform a task


 Motivation for modular programming:
 Improves maintainability of programs

 Simplifies the process of writing programs

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 4


Modular Programming

Figure 4-1: Structure Chart

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 6
Defining and Calling
Functions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Three steps in using functions
1. Declare the function:
 Known as function declaration or function prototyping.
 Write a function prototype that specifies:
 the name of the function
 the type of its return value
 its list of arguments and their types

2. Define the function:


 Known as function definition or function implementation.
 Write the block of statements (body) of the function to
define processes should be done by the function.

3. Call the function:


 Known as function call or function invocation.
 Call the name of the function in order to execute it.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Defining and Calling Functions

 Function call: statement causes a function to


execute
 Function definition: statements that make up a
function

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 9


Function Definition

 Definition includes:
 return type: data type of the value that
function returns to the part of the program
that called it
 name: name of the function. Function
names follow same rules as variables
 parameter list: variables containing values
passed to the function
 body: statements that perform the function’s
task, enclosed in {}

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 10


Function Definition

Note: The line that reads int main() is the


function header.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 11
Function Return Type
 If a function returns a value, the type of the
value must be indicated:
int main()
 If a function does not return a value, its return
type is void:
void printHeading()
{
cout << "Monthly Sales\n";
}

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 12


Calling a Function
 To call a function, use the function name followed by () and ;
printHeading();

 When called, program executes the body of the called function

 After the function terminates, execution resumes in the calling


function at point of call.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 13


Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 14
Flow of Control in Program 6-1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 15


Calling Functions
 main can call any number of functions
 Functions can call other functions
 Compiler must know the following about a function before it is
called:
 name

 return type

 number of parameters

 data type of each parameter

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 16


Part 2:Array
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Arrays Hold Multiple Values
 Array: variable that can store multiple values of the same type
 Values are stored in adjacent memory locations
 Declared using [] operator:
int tests[5];

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 19


Array - Memory Layout
 The definition:
int tests[5];
allocates the following memory:

first second third fourth fifth


element element element element element

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 20


Array Terminology

In the definition int tests[5];


 int is the data type of the array elements

 tests is the name of the array

 5, in [5], is the size declarator. It shows the


number of elements in the array.
 The size of an array is (number of elements) *
(size of each element)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 21


Array Terminology

 The size of an array is:


 the total number of bytes allocated for it

 (number of elements) * (number of bytes for


each element)
 Examples:
int tests[5] is an array of 20 bytes,
assuming 4 bytes for an int
long double measures[10]is an array of 80
bytes, assuming 8 bytes for a long double

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 22


Size Declarators
 Named constants are commonly used as size declarators.

const int SIZE = 5;


int tests[SIZE];
 This eases program maintenance when the size of the array needs
to be changed.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 23


Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Accessing Array Elements

 Each element in an array is assigned a unique


subscript.
 Subscripts start at 0

subscripts:
0 1 2 3 4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 25


Accessing Array Elements

 The last element’s subscript is n-1 where n is the


number of elements in the array.

subscripts:
0 1 2 3 4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 26


Accessing Array Elements

 Array elements can be used as regular variables:


tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
 Arrays must be accessed via individual elements:
cout << tests; // not legal

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 27


(Program Continues)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 28


Here are the contents of the hours array, with the values
entered by the user in the example output:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 29


Accessing Array Contents

 Can access element with a constant or literal


subscript:
cout << tests[3] << endl;

 Can use integer expression as subscript:


int i = 5;
cout << tests[i] << endl;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 30


Using a Loop to Step Through an Array
 Example – The following code defines an array,
numbers, and assigns 99 to each element:

const int ARRAY_SIZE = 5;


int numbers[ARRAY_SIZE];

for (int count = 0; count < ARRAY_SIZE; count++)


numbers[count] = 99;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 31


A Closer Look At the Loop

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 32


Default Initialization
 Global array  all elements initialized to 0 by
default

 Local array  all elements uninitialized by


default

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 33

Das könnte Ihnen auch gefallen