-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom_functions.cpp
More file actions
94 lines (76 loc) · 3.13 KB
/
random_functions.cpp
File metadata and controls
94 lines (76 loc) · 3.13 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream> //cin, cout
/**Functions:
*- Functions return a value (using the keyword "return").
*
*- Functions that don't return a value are called subroutines (subs),
* however they technically return a weird type called a void (holds no data)
*
*- In C++, functions are treated as variables in respect to their syntax.
*
*- SYNTAX:
* + DECLARATION:
* * structure>>> type fname(); // NOTE: all declarations end with a semi-colon
* bool isAutonomous();
* void stopRobot(void);//NOTE: void is also a keword, so this is legal
* bool isGreaterThan(int num1, int num2);
* int sum(int num1, int num2);
* char guessAnswer(void);
*
* + DEFINITION:
* * structure>>> type fname(){ /* code that gets run * / return value;}
*
* int sum(int a, int b){
* return a + b;
* }
*
* char guessAnswer(Question_t theQuestion){
* //notice the Question_t type (an object) that is a parameter
* //disregards the question because c is always the answer
* return 'c';
* }
*
* bool isGreaterThan(int num1, int num2){
* return(num1 > num2); //return looks like a subroutine
* }
*
* void stopRobot(void){ // the void keyword is legal here too
* //stop the program by throwing an exception (intentional error)
* throw 20;
* //you don't have to return anything, but if you need to stop the sub, you can use return
* return;
* }
*
*/
//all functions must be declared if defined after main after main() (look for definition on line: 83)
int getNegative(int number);//note arguments must also be defined
//short functions can be defined in the same place they are declared (And I won't kill you)
int randomNumber()//this function renturns an integer (int)
{return 6;}
int main(){//int main is technically a function that returns an int type.
int randInt = randomNumber(); //really not needed, but still
//print stuff out:
std::cout <<"\n1-This is a random number: " <<randInt <<std::endl
<<"\n2-This is the negative of that number: " <<getNegative(randInt) <<std::endl
<<"\n3-This calls the same function as the first one (equally random): "
<<randomNumber() <<std::endl
<<"\n4-You can set a function as another funciton\'s parameter: "
<<getNegative(randomNumber()) <<std::endl;
//start ignore --------------------------------------------------------------------
std::cout <<"\nXDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
std::cin.ignore(); //
std::cout <<"Don\'t get it?";
std::cin.ignore();
std::cout <<"Run this program several times then you will get it.\n";
std::cin.ignore();
//end ignore ----------------------------------------------------------------------
}
int getNegative(int number){//this function takes an int as an argument
int localVarNegativeVersionOfInput = - number; //this name is way too long and you don't actually need a variable
return localVarNegativeVersionOfInput;
}
/*A note about randomness:
* It is hard to tell if a number is truely random
* so our function which always returns 6, technically could be random, however the
* likeliness of it actually being random and returning 6 10000000000times is essentially
* zero.
*/