fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. // The function prototype of a function with a reference parameter. To specify the
  9. // reference parameter, simply type and ampersand after the data type.
  10. // Note that it does not return anything, as the data will be passed back through
  11. // the reference parameter.
  12. void addNumber(int &addTo, int value);
  13.  
  14. // This function prototype includes an array, which automatically acts as a reference
  15. // parameter -- changes to the array within the function will also effect MAIN.
  16. // Note that you do not need to specify the array size.
  17. void setArray(int arrayParam[], int arraySize, int setValue);
  18.  
  19. int main() {
  20.  
  21. int result = 5;
  22.  
  23. cout << "Number before function: " << result << endl;
  24.  
  25. // Passes the result value as a reference parameter, so that its value will
  26. // be changed within MAIN.
  27. addNumber(result, 10);
  28.  
  29. cout << "Number after function: " << result << endl;
  30.  
  31. // This declares an array of integers with a size of ten.
  32. int array[10];
  33.  
  34. cout << endl;
  35.  
  36. // This will output a garbage value, as the values in the array have not been
  37. // initialized. Note the use of array[0] to denote the first element.
  38. cout << "Garbage first value in array: " << array[0] << endl;
  39.  
  40. // For loops are particularly useful in looping through arrays. This loop sets
  41. // the first value in the array to 1, the second to 2, and so on.
  42. for(int i = 0; i < 10; i++) {
  43. array[i] = i + 1;
  44. }
  45.  
  46. // Output the fourth and sixth values in the array, which will now be 4. Note the
  47. // use of array[3] and array[5] to denote the fourth and sixth elements.
  48. cout << "After the loop, the fourth value in the array is: " << array[3] << endl;
  49. cout << "After the loop, the sixth value in the array is: " << array[5] << endl;
  50.  
  51. cout << endl;
  52.  
  53. // This calls the setArray function, which will set the first 10 values in the
  54. // array "array" to 25. This will effect the "array" variable in MAIN.
  55. setArray(array, 10, 25);
  56.  
  57. // Again, loop through the array, outputting each value. This shows that all the
  58. // elements in the array have indeed been set to 25.
  59. for(int i = 0; i < 10; i++) {
  60. cout << "Array element " << i << ": " << array[i] << endl;
  61. }
  62.  
  63. cout << endl;
  64.  
  65. // Declares a c-string that can hold 50 characters, or 49 characters if you don't
  66. // count the null character.
  67. char testString[50];
  68.  
  69. // Input the string "in the aggregate," so that the word from the console will be
  70. // automatically copied to the array, with a null terminator at the end.
  71. cout << "Enter string: ";
  72. cin >> testString;
  73.  
  74. // Loop though the array until you get to the null character, and output each
  75. // character in the string on a new line
  76. for(int i = 0; testString[i]; i++)
  77. cout << testString[i] << endl;
  78.  
  79. cout << endl;
  80. system("pause");
  81.  
  82. return 0;
  83. }
  84.  
  85. // This is the implementation of the function with a reference parameter. As with the
  86. // prototype, the reference parameter is specified with an ampersand. The function
  87. // simply plus-equals the value into the result, but the important part is that this
  88. // code will change the value of the "result" variable in MAIN
  89. void addNumber(int &addTo, int value) {
  90. addTo += value;
  91. }
  92.  
  93. // This function loops through the elements in "arrayParam," setting each one to the
  94. // value specified by the parameter "setValue." Note that this will change the values
  95. // in the "array" variable in MAIN.
  96. void setArray(int arrayParam[], int arraySize, int setValue) {
  97. for(int i = 0; i < arraySize; i++) {
  98. arrayParam[i] = setValue;
  99. }
  100. }
  101.  
Success #stdin #stdout #stderr 0s 3460KB
stdin
hello
stdout
Number before function: 5
Number after function: 15

Garbage first value in array: -1220107004
After the loop, the fourth value in the array is: 4
After the loop, the sixth value in the array is: 6

Array element 0: 25
Array element 1: 25
Array element 2: 25
Array element 3: 25
Array element 4: 25
Array element 5: 25
Array element 6: 25
Array element 7: 25
Array element 8: 25
Array element 9: 25

Enter string: h
e
l
l
o

stderr
sh: 1: pause: not found