forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
67 lines (54 loc) · 1.26 KB
/
functions.cpp
File metadata and controls
67 lines (54 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
*
* Program: Function Examples
*
* Description: Examples covering an introduction to functions in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=_N3G5L6k_rI
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
double travelTime(double, double);
void printErrorMessage();
void addOne(int number);
int main()
{
// f(x) : x + 1
// f(2) = 2 + 1 = 3
//
double tripTime1 = travelTime(200, 40);
cout << "Trip 1 (h): " << tripTime1 << endl;
double tripTime2 = travelTime(100,20);
cout << "Trip 2 (h): " << tripTime2 << endl;
double d = 150;
double s = 55;
double tripTime3 = travelTime(d,s);
cout << "Trip 3 (h): " << tripTime3 << endl;
int number = 10;
addOne(number);
cout << "number: " << number << endl;
return 0;
}
void addOne(int number)
{
number = number + 1;
}
void printErrorMessage()
{
cout << "Error: cannot divide by zero" << endl;
}
double travelTime(double distance, double speed)
{
if (speed == 0)
{
printErrorMessage();
return -1;
}
else
{
return distance / speed;
}
}