Docsity

User-Defined Functions in C++ - Exercise 7, Slides of Computer Science

Toggle sidebar
bg1
Exercise 7 -- Functions March 21-23, 2006
1
Exercise VII
Exercise VII –
–User
User-
-defined
defined
Functions
Functions
Larry Caretto
Computer Science 106
Computing in Engineering and
Science
March 21, 2006
2
Outline
• Exercise seven goals
• Outline tasks for exercise seven
• Provide details for task one
• Introduce task two
– Will discuss further on Thursday
• Exercise seven is linked to the first
programming project
– Functions developed in this exercise will be
used in project one
• Due date for exercise is April 4
3
Exercise Seven Goals
• As a result of this exercise you should
be able to accomplish the following:
– write programs with user-defined functions
• function header (with argument list)
• function body
• function prototype
– return values in function name
– pass variables to functions by value and by
reference
4
How do we write functions?
• C++ code is a collection of functions
• Each function, including main, has the
same level of importance
– Close code for each function before
starting a new function
int main()
{ // body of main
}
int myFunction( …… )
{ // body of myFunction
}
5
Use of Functions
• A function is designed to operate by
receiving data from a calling function
and returning results to that function
• Once a function is written, use it for
different applications by changing the
data in the call to the function
• You should not have to rewrite parts of
functions for different inputs
6
Tasks for Exercise Seven
•One –use of getValidInt function
– Provides utility that allows you to do data
validation for several int variables without
need to replicate code
•Two –write functions from pseudocode
to compute maximum days in a month,
determine if the year is a leap year, and
get valid input on month, day and year
– Routines will be used in first project
pf3
pf4
pf5

Partial preview of the text

Download User-Defined Functions in C++ - Exercise 7 and more Slides Computer Science in PDF only on Docsity!

Exercise VIIExercise VII – – User-User-defineddefined

FunctionsFunctions

Larry Caretto Computer Science 106

Computing in Engineering and

Science

March 21, 2006

2

Outline

  • Exercise seven goals
  • Outline tasks for exercise seven
  • Provide details for task one
  • Introduce task two
    • Will discuss further on Thursday
  • Exercise seven is linked to the first

programming project

  • Functions developed in this exercise will be used in project one
  • Due date for exercise is April 4

3

Exercise Seven Goals

  • As a result of this exercise you should

be able to accomplish the following:

  • write programs with user-defined functions
    • function header (with argument list)
    • function body
    • function prototype
  • return values in function name
  • pass variables to functions by value and by reference

4

How do we write functions?

  • C++ code is a collection of functions
  • Each function, including main, has the

same level of importance

  • Close code for each function before starting a new function int main() { // body of main } int myFunction( …… ) { // body of myFunction }

5

Use of Functions

  • A function is designed to operate by

receiving data from a calling function

and returning results to that function

  • Once a function is written, use it for

different applications by changing the

data in the call to the function

  • You should not have to rewrite parts of

functions for different inputs

6

Tasks for Exercise Seven

  • One – use of getValidInt function
    • Provides utility that allows you to do data validation for several int variables without need to replicate code
  • Two – write functions from pseudocode

to compute maximum days in a month,

determine if the year is a leap year, and

get valid input on month, day and year

  • Routines will be used in first project

7

Task One Functions

• main – you write this function to

get input using the getValidInt

function and write output

• getValidInt – copy this function from

the assignment

• Remember to write the function

prototype for getValidInt

8

getValidInt Function

  • Designed to provide simple approach to

obtaining integer input that is between

specified maximum and minimum value

  • Function parameters and minimum

value, maximum value, and string

description of parameter

  • Parameters passed to function by user
  • Function returns valid input through

function name

9

getValidInt

  • Function prototype

int getValidInt( int xMin, int xMax, string name );

  • This function returns a value for the integer

variable represented by the string, name,

that is between xMin and xMax

  • Example of use

int year = getValidInt( 1901, 2000,

“year from the twentieth century” );

10

What getValidInt Does

  • The function getValidInt( int xMin,

int xMax,string name ) does the

following tasks

  • Prompts the user for an input variable (named in the string passed in the third parameter) within a range defined by the first and second parameters
  • Gets the input from the user
  • Tells the user if there is an error and gets new input from the user in this case
  • Returns valid input to the calling function

11

Using getValidInt

  • The function getValidInt described

on the previous chart is used in exercise

seven and project one

  • Use #include
  • Examples of getValidInt use int month = getValidInt( 1, 12, “month”); int mayDay = getValidInt( 1, 31, “day of the month” ); int year = getValidInt( 1901, 2000, “year in the 20th century” );

12

Using getValidInt II

  • Examples of function use show different

variables are input by the same function

call

  • Only the data and the variable in which

the function result is returned change

  • Do not revise function code int month = getValidInt( 1, 12, “month”); int mayDay = getValidInt( 1, 31, “day of the month” );

Exercise VIIExercise VII – – User-User-defineddefined

FunctionsFunctions – – Day 2Day 2

Larry Caretto Computer Science 106

Computing in Engineering and

Science

March 23, 2006

20

Outline

  • Exercise seven goals
  • Summarize lecture material on functions
  • Outline tasks for exercise seven
  • Provide details for some tasks
  • Links between exercise seven and first

programming project

  • Exercise seven is due April 5 and

Project one is due April 7

21

Exercise Seven Goals

  • As a result of this exercise you should

be able to accomplish the following:

  • write programs with user-defined functions
    • function header (with argument list)
    • function body
    • function prototype
  • return values in function name
  • pass variables to functions by value and by reference

22

Function Example

bool leap( int year ) { if ( year % 4 != 0 ) return false; else if ( year % 400 == 0 ) return true; else if ( year % 100 == 0 ) return false; else return true; }

Header

Body

Name

Type

Argu- ment List (^) Multiple returns

23

Use of bool leap( int year )

bool leap ( year ); // prototype

int main() // examples of use

{ cout << “Enter a year: “;

int y; cin >> y; bool cond = leap( y ); if ( leap( y ) ) if ( leap( y ) && month == 2 )

24

Pass by Value and Reference

  • Pass by value is the normal operation
    • The value of the parameter in the calling code is passed to the function
    • If the corresponding dummy parameter in the function is changed, no change is made in the parameter in the calling code
  • Pass by reference is designated by

ampersand (&) in header

  • Parameter passed to function can be changed

25

Pass by

Value

// prototype int x2(int& x); // example of use int y = 5; cout << x2( y ) << “ “ << y //function int x2( int& x) { x = 2 * x; return x; } // output: 10 10

// prototype int x2(int x); // example of use int y = 5; cout << x2( y ) << “ “ << y; //function int x2( int x) { x = 2 * x; return x; } // output: 10 5

Pass by

Reference

26

Tasks for Exercise Seven

  • One – use of getValidInt function
    • Provides utility that allows you to do data validation for several int variables without need to replicate code
  • Two – write functions from pseudocode

to compute maximum days in a month,

determine if the year is a leap year, and

get valid input on month, day and year

  • Routines will be used in first project

27

Task One Functions

• main – you write this function to

get input using the getValidInt

function and write output

• getValidInt – copy this function from

the assignment

• Remember to write the function

prototype for getValidInt

28

Task Two Functions

  • main – you write this function that calls

the input function that you write

  • getMaxDays – write this function to get

maximum days in a month from

pseudocode in assignment

  • leap – write this function to tell if a year is

a leap year from pseudocode in

assignment

  • getValidInt – existing function from

task one

  • input – write this function that is similar

to main program from task one

29

getMaxDays

  • Function prototype

int getMaxDays( int month, int year );

  • This function returns the maximum number

of days in a month for an input month (1 to

12) and year.

  • Example of use

int mIn = 2, yIn = 2004;

int maxDays = getMaxDays( mIn, yIn );

30

leap

  • Function prototype bool leap( int year );
  • This function returns true or false if the

input year is or is not a leap year.

  • Example of use if( leap( year ) ) { cout << “February has 29 days”; }