Thứ Ba, 21 tháng 1, 2014

C++ Basics - Functions for All Subtasks

Slide 5- 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
void-Functions

In top-down design, a subtask might produce

No value (just input or output for example)

One value

More than one value

We have seen how to implement functions that
return one value

A void-function implements a subtask that
returns no value or more than one value
Slide 5- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 5.1
void-Function Definition

Two main differences between void-function
definitions and the definitions of functions
that return one value

Keyword void replaces the type of the value returned

void means that no value is returned by the function

The return statement does not include and expression

Example:
void show_results(double f_degrees, double c_degrees)
{
using namespace std;
cout << f_degrees
<< “ degrees Fahrenheit is euivalent to “ << endl
<< c_degrees << “ degrees Celsius.” << endl;
return;
}
Slide 5- 7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using a void-Function

void-function calls are executable statements

They do not need to be part of another statement

They end with a semi-colon

Example:
show_results(32.5, 0.3);
NOT: cout << show_results(32.5, 0.3);
Slide 5- 8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
void-Function Calls

Mechanism is nearly the same as the function
calls we have seen

Argument values are substituted for the formal
parameters

It is fairly common to have no parameters in
void-functions

In this case there will be no arguments in the function call

Statements in function body are executed

Optional return statement ends the function

Return statement does not include a value to return

Return statement is implicit if it is not included
Slide 5- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 5.2 (1)
Display 5.2 (2)
Example:
Converting Temperatures

The functions just developed can be used in a
program to convert Fahrenheit temperatures to
Celcius using the formula
C = (5/9) (F – 32)

Do you see the integer division problem?
Slide 5- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 5.3
void-Functions
Why Use a Return?

Is a return-statement ever needed in a
void-function since no value is returned?

Yes!

What if a branch of an if-else statement requires
that the function ends to avoid producing more
output, or creating a mathematical error?

void-function in Display 5.3, avoids division by zero
with a return statement
Slide 5- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Main Function

The main function in a program is used like a
void function…do you have to end the program
with a return-statement?

Because the main function is defined to return a
value of type int, the return is needed

C++ standard says the return 0 can be omitted, but
many compilers still require it
Slide 5- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Section 5.1 Conclusion

Can you

Describe the differences between void-
functions and functions that return one value?

Tell what happens if you forget the return-
statementin a void-function?

Distinguish between functions that are used as
expressions and those used as statements?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5.2
Call-By-Reference Parameters
Slide 5- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Call-by-Reference Parameters

Call-by-value is not adequate when we need
a sub-task to obtain input values

Call-by-value means that the formal parameters
receive the values of the arguments

To obtain input values, we need to change the
variables that are arguments to the function

Recall that we have changed the values of
formal parameters in a function body, but we have not
changed the arguments found in the function call

Call-by-reference parameters allow us to change
the variable used in the function call

Arguments for call-by-reference parameters must be
variables, not numbers

Không có nhận xét nào:

Đăng nhận xét