Programming Lesson Plans
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

3 years ago
  1. # Day 3
  2. ## Functions
  3. Mathematically speaking, functions are able to take in a set of inputs and
  4. return a specific output. C++ gives you an interface by which you can create
  5. your own custom functions for purposes of modularity or recursion (where
  6. functions are necessary).
  7. The general structure of a function is as follows:
  8. ```cpp
  9. (type that the function returns) (function name) (parameters) {
  10. // body
  11. }
  12. ```
  13. We will now more closely analyze each of the parts of this function.
  14. ### Parameters
  15. The parameters that you put into your function are a template for how the
  16. function will work when it is actually called.
  17. The format of the parameters must be `(type) (name)`, with the parameters
  18. separated by commas.
  19. For instance, if you had a function to add two numbers, it would look something
  20. like this:
  21. ```cpp
  22. int add (int a, int b) {
  23. return a+b;
  24. }
  25. ```
  26. Note that the variables `a` and `b` are simply aliases that you use to represent
  27. the actual values that you place into the function when you make a call.
  28. When you make the following function call,
  29. ```cpp
  30. cout << add(3, 2);
  31. ```
  32. `a` will represent 3 and `b` will represent 2, and thus this code block will
  33. print `5`. However, note that we cannot make the following call, as the
  34. arguments are not in the types that the compiler expects them to be in:
  35. ```cpp
  36. cout << add(3, "string");
  37. ```
  38. The function expects two integers, but received a string, which cannot be
  39. automatically converted to an integer for very obvious reasons.
  40. Note that c++ will conduct automatic type conversion whenever possible to allow
  41. the function to execute, so you can pass in a `long long` or `bool` and the
  42. function should still be able to convert them into `int`s.
  43. ### Type
  44. Sometimes, you want your functions to return back a value for your convenience.
  45. For instance, if you have a simple function to add two numbers, you would want
  46. to get the sum of the numbers back. In this case, you place the return type at
  47. the front of the function signature as shown above
  48. Note that if you do wish to return a type in your function, you **must** return
  49. the value of that type in **all possible cases**, or else your compiler will
  50. throw a warning.
  51. The `void` keyword is special, as it is used for a function that does not return
  52. any value and only has side effects.
  53. For instance, if I were doing a floodfill traversal of a graph (where we are
  54. simply trying to find the number of disjoint regions), I would probably do the
  55. following:
  56. ```cpp
  57. void dfs(int x, int y) {
  58. // mark as visited
  59. // propagate
  60. }
  61. ```
  62. Note that the DFS does not actually return any real value, we simply use it for
  63. the fact that it is able to fill out the visited array.
  64. When you use a function that returns `void`, you will not actually have to
  65. provide an explicit `return` statement.