Value Returning Function with Examples - Lecture Slides | CPE 112, Exams of Engineering

Material Type: Exam; Class: INTRO COMPUTER PROG FOR ENGR; Subject: Computer Engineering; University: University of Alabama - Huntsville; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/23/2009

koofers-user-r9i
koofers-user-r9i 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Electrical and Computer Engineering 1of 24
UAH CPE 112
Value-Returning Functions
A call to a value-returning function is part of an
expression.
A value-returning function is used when there is
only one result returned by a function and that
result is to be used directly in an expression.
A value-returning function returns one value, not
through a parameter but by means of a return
statement.
Electrical and Computer Engineering 2of 24
UAH CPE 112
Value-Returning Function Example
int Power (/* in */ int x, // Base number
/* in */ int n) // Power to raise base to
{
int result; // Holds intermediate powers of x
result = 1;
while (n > 0)
{
result = result * x;
n--;
}
return result;
}
Usage
y = Power(5, 3)
Electrical and Computer Engineering 3of 24
UAH CPE 112
Another Value-Returning
Function Example
#include <iostream>
using namespace std;
int Min (int val1, int val2, int val3);
int main()
{
int num1;
int num2;
int num3;
int min_num;
Electrical and Computer Engineering 4of 24
UAH CPE 112
Another Value-Returning
Function Example
cout << "Please enter an integer value for num1: "
<< endl;
cin >> num1;
cout << "Please enter an integer value for num2: ”
<< endl;
cin >> num2;
cout << "Please enter an integer value for num3: "
<< endl;
cin >> num3;
min_num = Min(num1, num2, num3);
cout << "The minimum number of the three entered is ”
<< min_num << endl;
}
Electrical and Computer Engineering 5of 24
UAH CPE 112
Another Value-Returning
Function Example
int Min (int val1, int val2, int val3)
{
int result;
result = val1;
if (val2 < result)
result = val2;
if (val3 < result)
result = val3;
return result;
}
Electrical and Computer Engineering 6of 24
UAH CPE 112
Revisiting the Power Function
#include <iostream>
#include <cmath>
using namespace std;
float power (int base, int exponent);
int main()
{
float result;
int base;
int exponent;
cout << "Please enter integer base value " << endl;
cin >> base;
pf3
pf4

Partial preview of the text

Download Value Returning Function with Examples - Lecture Slides | CPE 112 and more Exams Engineering in PDF only on Docsity!

Electrical and Computer Engineering

1 of 24

Value-Returning Functions

  • A call to a value-returning function is part of an

expression.

  • A value-returning function is used when there is

only one result returned by a function and that

result is to be used directly in an expression.

  • A value-returning function returns one value, not

through a parameter but by means of a return

statement.

Electrical and Computer Engineering

2 of 24

Value-Returning Function Example

int Power (/* in / int x, // Base number / in */ int n) // Power to raise base to { int result; // Holds intermediate powers of x result = 1; while (n > 0) { result = result * x; n--; } return result; } Usage y = Power(5, 3)

Electrical and Computer Engineering

3 of 24

UAH CPE 112

Another Value-Returning

Function Example

#include using namespace std;

int Min (int val1, int val2, int val3);

int main() { int num1; int num2; int num3; int min_num;

Electrical and Computer Engineering

4 of 24

UAH CPE 112

Another Value-Returning

Function Example

cout << "Please enter an integer value for num1: " << endl; cin >> num1; cout << "Please enter an integer value for num2: ” << endl; cin >> num2; cout << "Please enter an integer value for num3: " << endl; cin >> num3;

min_num = Min(num1, num2, num3);

cout << "The minimum number of the three entered is ” << min_num << endl; }

Electrical and Computer Engineering 5 of 24

UAH CPE 112

Another Value-Returning

Function Example

int Min (int val1, int val2, int val3) { int result;

result = val1;

if (val2 < result) result = val2; if (val3 < result) result = val3;

return result; } Electrical and Computer Engineering 6 of 24

UAH CPE 112

Revisiting the Power Function

#include #include using namespace std;

float power (int base, int exponent);

int main() { float result; int base; int exponent;

cout << "Please enter integer base value " << endl; cin >> base;

Electrical and Computer Engineering

7 of 24

Revisiting the Power Function

cout << "Please enter integer exponent value " << endl; cin >> exponent;

result = power(base, exponent); cout << base << " raised to the " << exponent << " power is " << result << endl; }

Electrical and Computer Engineering

8 of 24

Revisiting the Power Function

float power (int base, int exponent) { int i =1; float result = 1.0;

while (i <= abs(exponent)) { result = result * base; i++; } if (exponent < 0) result = 1.0/result; return result; }

Electrical and Computer Engineering

9 of 24

UAH CPE 112

Problem-Solving Case Study

(Starship Weight and Balance)

  • Problem: Write a program that determines the

weight and center of gravity of a new plane, based

on the number of crew members and passengers as

well as the weight of the baggage, closet contents,

and fuel.

  • Input: Number of crew members, number of

passengers, weight of closet contents, baggage

weight, fuel in gallons.

  • Output: Total weight, center of gravity.

Electrical and Computer Engineering

10 of 24

UAH CPE 112

Starship Weight and Balance (continued)

  • Discussion: Sum all of the weights, using the

standard average weight of a person of 170

pounds, and the fact that a gallon of fuel weighs

6.7 pounds.

  • totalWeight = emptyWeight + (crew + passengers) *

170 + baggage + closet + fuel * 6.

  • centerofGravity = (emptyMoment + crewMoment +

passengerMoment + cargoMoment +

fuelMoment)/totalWeight

Electrical and Computer Engineering 11 of 24

UAH CPE 112

Main (Level 0)

Get data

Set totalWt = EMPTY_WEIGHT + (passengers +

crew) * 170 + baggage + closet + fuel * 6.

Set centerof Gravity = (CrewMoment(crew) +

PassengerMoment(passengers) + CargoMoment(closet,

baggage) + FuelMoment(fuel) + EMPTY

_MOMENT)/totalWt

Print totalWt, centerof Gravity

Print warning

Electrical and Computer Engineering 12 of 24

UAH CPE 112

Level 1 Functional Decomposition

Get Data (Out: crew, passengers, closet, baggage, fuel)

Prompt for numer of crew, number of passengers, weight in closet
and baggage compartments, and gallons of fuel
Read crew, passengers, closet, baggage, fuel
Echo print the input

CrewMoment (In: crew) Out Function Value

Return crew * 170 * 143

Cargo Moment (In: closet, baggage) Out: Function value

Return closet * 182 + baggage * 386

Electrical and Computer Engineering

19 of 24

Case Study Implementation

float CrewMoment( /in/ int crew ) // Number of crew members

// Computes the crew moment arm in inch-pounds from the number of // crew members. Global constant PERSON_WT is used as the weight // of each crew member // Precondition: // crew == 1 OR crew == 2 // Postcondition: // Function value == Crew moment arm, based on the crew // parameter

{ const float CREW_DISTANCE = 143.0; // Distance to crew seats // from front

return float(crew) * PERSON_WT * CREW_DISTANCE; } Electrical and Computer Engineering

20 of 24

Case Study Implementation

float PassengerMoment( /in/ int passengers ) { const float ROW1_DIST = 219.0; // Distance to row 1 seats const float ROW2_DIST = 265.0; // Distance to row 2 seats const float ROW3_DIST = 295.0; // Distance to row 3 seats const float ROW4_DIST = 341.0; // Distance to row 4 seats float moment = 0.0; // Running total of moment if (passengers > 6) // For passengers 7 and 8 { moment = moment + float (passengers - 6) * PERSON_WT * ROW4_DIST; passengers = 6; // 6 remain } if (passengers > 4).. // For passengers 5 and 6 . return moment; }

Electrical and Computer Engineering

21 of 24

UAH CPE 112

Case Study Implementation

float CargoMoment( /in/ int closet, // Weight in closet /in/ int baggage ) // Weight of baggage

// Computes the total moment arm for cargo loaded into the // front closet and aft baggage compartment // Precondition: // 0 <= closet <= 160 && 0 <= baggage <= 525 // Postcondition: // Function value == Cargo moment arm, based on the closet // and baggage parameters

{ const float CLOSET_DIST = 182.0; // Distance to closet const float BAGGAGE_DIST = 386.0; // Distance to baggage return float(closet) * CLOSET_DIST + float(baggage) * BAGGAGE_DIST; } Electrical and Computer Engineering

22 of 24

UAH CPE 112

Case Study Implementation

float FuelMoment( /* in */ int fuel ) // Fuel in gallons { float fuelWt; // Weight of fuel in pounds float fuelDistance; // Distance from front of plane

fuelWt = float(fuel) * LBS_PER_GAL; if (fuel < 60) fuelDistance = float(fuel) * 314.6; else if (fuel < 361) fuelDistance = 305.8 + (-0.01233 * float(fuel - 60)); else if (fuel < 521) fuelDistance = 303.0 + ( 0.12500 * float(fuel - 361)); else fuelDistance = 323.0 + (-0.04444 * float(fuel - 521)); return fuelDistance * fuelWt; }

Electrical and Computer Engineering 23 of 24

UAH CPE 112

Testing & Debugging – Stubs

  • A stub is has the same name and interface as a function

that actually would be called by the part of the program

being tested, but it is usually much simpler

  • Example void GetYear (/inout/ ifstream& dataIn, // Input file /out/ string& year) //Four digits of year

// Stub for GetYear function in the ConvertDates program

{ cout << “Get Year was called here. Returning \”1949\”.” << endl; year = “1948”; }

Electrical and Computer Engineering 24 of 24

UAH CPE 112

Testing & Debugging - Drivers

  • A driver is a simple main function which cals a function

being tested and permits direct control of testing.

  • Example

float FuelMoment (int); int main() { int testVal; cout << “Fuel moment for gallons from 10 – 565 in steps” << “of 15:” << endl; testVal = 10; while (testVal <= 565) { cout << FuelMoment(testVal) << endl; testVal = testVal + 15; } }