-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
67 lines (42 loc) · 1.32 KB
/
functions.cpp
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
#include <iostream>
using namespace std;
//function that multiplies number by 2
int timestwo( int num) {
return num *2;
}
double squareroot (double n){
double root = n/ 2;
for (int i=0;i <20; i++){
root = 0.5 * (root + n / root );
// cout << "iteration " << i<< ", Root:" << root << endl;
}
return root;
}
void change_ValuesLocally( int inputVar){
// "Creating local name space", pass by value
int nextVar = inputVar *2;
inputVar = 4;
cout << "nextVar = " << nextVar << " inputVar = " << inputVar;
}
void change_ValuesGlobally(int &inputVar){
//Pass by reference
inputVar = 999;
}
void add_lists(const int first[], const int second[], int total[], int length){
//Lists are always pass by reference, const forces an error raise if we were to alter any of those variables
int element;
for (element = 0; element < length; element ++) {
total[element] = first[element] + second[element];
}
}
int main() {
cout << "Running times two function" << endl;
cout << "5 multiplied by 2 is " << timestwo(5) << endl;
cout << squareroot(256)<< endl;
int myVar = 10;
change_ValuesLocally( myVar );
cout << " myVar = " << myVar<<endl;
change_ValuesGlobally(myVar);
cout << "After global change, MyVar = " << myVar << endl;
return 0;
}