Sie sind auf Seite 1von 9

Arrays and Functions

Ref.: International Edition, Malik, C++ Programming: Program Design Including Data Structures, 6th edition, CENGAGE Learning

Functions
Functions - aka modules

Predefined Functions - examples


Function abs(x) |x| Meaning Function islower(x) Meaning islower(h) = 1; identifies lower case letters isupper(H) = 1 xy
x

ceil(x) cos(x) exp(x)

ceil(56.34) 57.0 ex

isupper(x) pow(x,y) sqrt(x)

fabs(x)
floor(x)

|x|
floor(45.67) = 45.0

tolower(x)
toupper(x)

tolower(H) = h
toupper(h) = H

User-Defined Functions
Value-returning functions have a return type Void functions do not have a return type

Value-Returning Functions
e.g.
heading or function header

int abs(int number) { if (number < 0) a parameter body number = -number; return number; } All of the above is called the functions definition.

Arrays
simple data type a variable can only store 1 value structured data type a variable can store several values e.g. input 5 integer test scores called test1, test2, , test5. Output the average and output those test scores that are less than the average

Arrays
int test1, test2, cin >> test1 >> test2 >> average = (test1 + test2 + if (test1 < average) if (test2 < average) This is clumsy!

Arrays
Array: A collection of a fixed number of components, all of the same type. 1-dimensional array: a list declaration: dataType arrayName [intExp]; e.g. int num[5] The components are num*0+, num*1+, num[4]

Accessing Array Components


[ ] is called array subscripting operator

arrayName [indexExp]
called index

an integer 0

Das könnte Ihnen auch gefallen