a function (procedure) - PowerPoint PPT Presentation

About This Presentation
Title:

a function (procedure)

Description:

primitive types - parameter is a copy of the argument ... if (mySuit == SPADES) --- switch (mySuit) { case CLUBS: ---- case SPADES: --- typedef statement ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 27
Provided by: wat88
Category:

less

Transcript and Presenter's Notes

Title: a function (procedure)


1
a function (procedure)
---- Func (arguments) ---
Return value
Return_type Func(parameters)
code to perform a task (carry out an algorithm)
2
Parameters
  • Java parameters are either
  • primitive types - parameter is a copy of the
    argument (argument cannot be changed)
  • objects - parameter is an alias for the argument
    (argument can be changed)
  • C parameters are either
  • value parameters
  • reference parameters
  • constant reference parameters
  • array parameters

3
Java parameter passing
public void myMethod (int number, SomeClass
myObj) --- ---
4
C Parameters
  • C parameters are either
  • value - (type param_name)
  • parameter is a copy of the argument, thus
    argument cannot be modified
  • reference - (type param_name)
  • parameter is an alias for the argument, thus
    argument is modified if parameter is modified
  • constant reference - (const type param_name)
  • parameter is an alias for the argument, but
    compilerdisallows assignment to parameter

5
C parameter passing
void myFunction (int val, int ref, const int
constref) --- ---
6
Decomposition Example
from Figure C.6 Using Functions on p.781
7
/ Program to compute wages for several
employees. Written at ___________ by
_________________- Input Id-number, number
of dependents, hours worked,
and hourly rate for each of several employees
Output Id-number, hours worked, gross pay,
taxes withheld and net pay _______________________
_____________________________/ include
ltiostreamgt include ltiomanipgt using namespace
std void Instruct ( ) void GetEmployeeInfo
(int empNumber, int dependents,
double hours,
double rate, bool done) double GrossWages
(int dependents, double hours, double rate) void
ComputeNetPay (double grossPay, int dependents,
double
tax, double netPay) void PrintEmpInfo (int
idNumber, double hours, double grossPay,
double taxes, double
netPay) int main ( ) -----
8
int main ( ) int idNumber,
// employee's id-number
numDependents // number of
dependents double hoursWorked, //
hours worked this pay period
hourlyRate, // dollars per hour
grossPay, //
pay before taxes taxWithheld,
// amount of tax withheld
netPay // grossPay -
tasWithheld bool endOfData
// signals end of data Instruct ( )
for ( ) GetEmployeeInfo
(idNumber, numDependents, hoursWorked,
hourlyRate,
endOfData) if (endOfData) break //
MUST BE CHANGED!! grossPay GrossWages
(numDependents, hoursWorked, hourlyRate)
ComputeNetPay (grossPay, numDependents,
taxWithheld, netPay) PrintEmpInfo
(idNumber, hoursWorked, grossPay, taxWithheld,
netPay) return 0
9
int main ( ) int idNumber,
// employee's id-number
numDependents // number of
dependents double hoursWorked, //
hours worked this pay period
hourlyRate, // dollars per hour
grossPay, //
pay before taxes taxWithheld,
// amount of tax withheld
netPay // grossPay -
tasWithheld // bool endOfData
// signals end of data Instruct ( )
while (GetEmployeeInfo (idNumber,
numDependents, hoursWorked,
hourlyRate))
// if (endOfData) break // TO
BE CHANGED!! grossPay GrossWages
(numDependents, hoursWorked, hourlyRate)
ComputeNetPay (grossPay, numDependents,
taxWithheld, netPay) PrintEmpInfo
(idNumber, hoursWorked, grossPay, taxWithheld,
netPay) return 0
10
Change Needed?
void GetEmployeeInfo (int empNumber, int
dependents,
double hours, double rate, bool done)
11
/ bool GetEmployeeInfo (int empNumber, int
dependents,
double hours, double rate)
Purpose read information for one employee
Precondition(s) none Postcondition(s) The
four parameters have been assigned values
Returns false if end of data reached true
otherwise ----------------------------------------
--------------------------------------------------
--/ bool GetEmployeeInfo (int empNumber, int
dependents,
double hours, double rate) cout
ltlt "\nEnter employee number (0 to stop) "
cin gtgt empNumber if (empNumber 0)
return false cout ltlt "Enter of
dependents, hours worked, and hourly rate for "
ltlt empNumber ltlt " " cin gtgt
dependents gtgt hours gtgt rate return true
12
/ double GrossWages (int dependents, double
hours, double rate) Purpose compute gross
wages as determined by number of hours
employee worked plus a dependency
allowance for each
dependent. Precondition(s) dependents,
hours and rate gt 0 Postcondition(s) none
Returns Gross wages ------------------------
--------------------------------------------------
------------------/ double GrossWages (int
dependents, double hours, double rate)
const double DEP_ALLOWANCE 100 // bonus per
dependent double wages
// wages earned if
(hours lt 40)
// no overtime wages hours
rate else
// overtime
wages 40 rate 1.5 rate (hours -
40) return wages DEP_ALLOWANCE
dependents
13
/ void Instruct ( ) Purpose Display
instructions to the user Precondition(s)
none Postcondition(s) instructions have
been displayed on the screen Returns
nothing
/ /
void ComputeNetPay(double grossPay, int
dependents,
double tax, double netPay)
Purpose compute taxes withheld and net pay
Precondition(s) grossPay and dependents are
valid Postcondition(s) tax and netPay have
been assigned computed values Returns
nothing
/ /
void PrintEmpInfo( int idNumber, double hours,
double grossPay
double taxes, double netPay) Purpose
display payroll information regarding one
employee Precondition(s) idNumber, hours,
grossPay, taxes and netPay are valid
Postcondition(s) payroll information has been
displayed on the screen Returns
nothing
/
14
Programming Problem Solving
Real world results
Real world problem
PROBLEM DOMAIN
PROGRAM DOMAIN
Representation of results
Representation of real world problem
Processing algorithms
15
Variables (data objects)
  • Components of a data object
  • name (programmer defined)
  • address (memory location)
  • type
  • value (determined by interpretation of the
    sequence of bits stored in memory)
  • type name value

16
Types
  • predefined (built-in) vs programmer defined
  • simple (fundamental) vs structured
  • type of a data object determines
  • number of bytes used to store the value
  • interpretation of the bit pattern stored in those
    bytes
  • possible operations
  • data abstraction refers to the separation of the
    use of a data type from the implementation details

17
C Types
Fundamental Types
Structured Types
array
Arithmetic
void
struct
pointers
union
bool
complex
Integral
Floating point
(reals)
float
Characters
Integers
Enumerations
double
C Standard Library classes
long double
int
char
short int
priority_queue
unsigned char
long int
signed char
unsigned
unsigned short
unsigned long
18
C arrays
  • groups data items of same type under one name
  • double Numbers4
  • int List 45, 63, 22
  • individual items accessed by indexing
  • Numbers2 85.3
  • first array element is at index position 0
  • C arrays are not objects
  • no length data member
  • number of elements is determined either
  • at compile-time (both examples above)
  • at run-time (coming later)

19
C array implementation
  • array name is a variable whose value is the base
    address of the array, i.e. a pointer variable
  • accessing an individual element involves
    calculating its address
  • base_address index element_size

20
Alternative Syntax
  • Given int List 45, 63, 22
  • first element can be accessed by
  • List0
  • List // is the dereferencing operator
  • second element can be accessed by
  • List1
  • (List 1)
  • what is the value of List?
  • always use the indexing notation

21
C array parameters
void func (double A , int N) ---
---
arguments provided when func is called
Numbers
3.2 2.1 5.9 7.0
N
4
22
C structs
  • struct - group of data items of different types
  • struct person string Name int Age
  • person is a type grouping together 2 data members
  • person OneStudent //represents 1 person
  • person Class25 //represents 25 persons
  • dot operator (.) used to access members
  • Classi.Age
  • struct to struct assignments are legal
  • Classi Classi1

23
Creating New C Types
  • enumeration
  • allows defining your own set of values for a
    scalar type
  • typedef
  • allows for defining a synonym or alias for an
    existing type
  • struct
  • allows a variable to have multiple values of
    different types
  • class
  • allows encapsulating data members and operations

24
Enumeration
  • used to make a program more readable
  • associates a set of programmer defined values
    with the integer values 0, 1, 2, - -
  • Example
  • enum Suit CLUBS, SPADES,
    HEARTS, DIAMONDS
  • Suit mySuit HEARTS
  • if (mySuit SPADES) ---
  • switch (mySuit) case CLUBS
    ---- case SPADES ---

25
typedef statement
  • used to declare a new name (synonym) for an
    already existing type
  • const int SIZE 100
  • typedef int intArrayTypeSIZE
  • intArrayType numberList
  • usually given global (file) scope
  • used to make a program easier to modify

26
C strings
  • early versions of C used C-style strings
  • array of char with a special character (null
    character) marking the end
  • ltstring.hgt or ltcstringgt have functions to
    manipulate C-style strings
  • C standard library provides the class string
  • include ltstringgt
  • use C string class, not C-style strings
  • see Ch.3 sec.4 for information and examples also
    TableD.4
Write a Comment
User Comments (0)
About PowerShow.com