-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupplement.cpp
More file actions
59 lines (51 loc) · 1.8 KB
/
supplement.cpp
File metadata and controls
59 lines (51 loc) · 1.8 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
/**************************
James Bertel
CS111
Lab
10-16-17
This program will...
**************************/
#include <iostream>
using namespace std;
//prototypes
int getNumber();
void getNumbers(int &n1, int& n2);
void printNum(int num1, int num2);
int main()
{
int num1, num2;
//get two numbers from the keyboard
num1 = getNumber();
num2 = getNumber();
//What is the difference between the following 2 statements?
{ num1 = getNumber(); v.s. getNumber();
//OR another way to get 2 numbers at once
getNumbers(num1, num2);
/*Do we need datatypes or & in function calls as in getNumber(int& num1, int& num2)?
Can we use n1 and n2 in main as in getNumber(n1, n2)?*/
printNum(num1, num2);
/*Can we call*/"printNum()"/* in a cout as in*/ "cout << printNum(num1, num2);" //?
}
return 0;
}
int getNumber() ß Why is the return type int?
{
int num; //declare the variable to be returned inside this function. Don’t pass it from main.
cout << “Enter a number: “;
cin >> num;
return num;
}
/*The parameters have to be passed by reference since we need to send 2 numbers back to the
caller(main).*/
void getNumbers(int &n1, int& n2) //Why do we need & for each parameter here? What does & do?
{// Can we use num1 and num2 as parameter names here?
cout << "Enter the first number: ";// Notice the return type is void with pass by reference.
cin >> n1; //Can we use num1 here instead of n1?
cout << "Enter the second number: ";
cin >> n2; //Can we use num2 here instead of n2?
}
/*The parameters should be passed by value since we don’t change the 2 numbers here.*/
void printNum(int num1, int num2) //Why don’t we need &s for the parameters here?
{// Can we use n1 and n2 as parameter names here?
cout << num1 << “ “ << num2 << endl; //Notice the return type is void.
}