Advertisement
Guest User

abracadabra2

a guest
Aug 24th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. You can imagine a function being a "smaller" program to which your main program ('int main') sends input data and uses its produced output data further for the execution. Just like programs do, a function must follow a structure in order to be understood by the computer.
  2.  
  3. # Examples
  4. A function which calculates twice an integer number n looks as follows :
  5. ```cpp
  6. int dublu(int x) {
  7. return 2 * x;
  8. }
  9. ```
  10. If we want a function to calculate a^b (a raised to the power of b) we could opt for the following way :
  11. ```cpp
  12. int putere(int a, int b) {
  13. int rezultat = 1;
  14. for (int i = 1; i <= b; ++i)
  15. rezultat *= a;
  16. return rezultat;
  17. }
  18. ```
  19.  
  20. # The Structure
  21. From the above examples we can deduce what is the structure we should follow when we write a function :
  22. ```
  23. tipul_rezultatului nume_functie (lista_parametri) {
  24. instructiuni
  25. return rezultat;
  26. }
  27. ```
  28. # data types
  29. As programs do, most of the functions produce output data. For being able to further use output data and making the code much easier to be understood by programmers, every function must specify the data type of its own produced result.
  30.  
  31. This step is very alike with the declaration of variables, every function having a data type.
  32.  
  33. For instance, a function which calculates the average of 2 rational numbers will be of type 'double' : `double medie(double x, double y) {`
  34.  
  35. ## `function-name`
  36.  
  37. Similarly to variable's declaration, when we write functions we should name them in order to use them in our program. The rules of naming are the same as variables' are.
  38.  
  39. It's a good practice to have a suggestive name for the function. It would have been weird to name our function "dividing" or "f" or any other one-letter name instead of "power".
  40.  
  41. ## `parameters-list`
  42.  
  43. Parameters of a function represent the input data received and the base upon which it calculates the output data.
  44.  
  45. In contrast to the way we worked until now when we were reading the input from keyboard or external files, data are transmitted to functions throughout variables which are called parameters.
  46.  
  47. For a better illustration, our function 'medie' from above has two parameters : 'x' and 'y', both of 'double' type. Parameters are separated by commas and we should specify the type of every parameter. The entire list of parameters should be included in parenthesis `(`, `)`.
  48.  
  49. ## `instructions`
  50.  
  51. As we want to be able to do complicated things in functions, C++ allows us to write any kind of instructions (correct from the perspective of the rules mentioned so far) inside of functions. As we've seen in function 'power' from above, we can write more instructions in order to calculate the result.
  52.  
  53. ## `return rezultat`
  54.  
  55. After finishing all the calculations, for being able to point out that we found out the result we should use the 'return' instruction.
  56. After return we write the variable or expression that keeps our result. In order to show that we have finished the instruction we put a semi-colon (';') at the end.
  57.  
  58. This result could be used further in the program as you have seen in the previous lesson.
  59.  
  60. ### **Pay attention!**
  61. When the program gets to the 'return' instruction the execution of the function is stopped untroubled by any other instruction that follows after 'return'.
  62. ```cpp
  63. #include <iostream>
  64. using namespace std;
  65.  
  66. int f(int a, int b) {
  67. return a;
  68. ++b;
  69. return b;
  70. }
  71.  
  72. int main() {
  73. cout<<f(1, 2);
  74. return 0;
  75. }
  76. ```
  77. In the above-stated program, the function will always return the value of 'a' while lines '2' and '3' from the inside of the function will never be executed.
  78.  
  79.  
  80. In order to better assimilate the information read the lesson carefully a couple times and be mindful of each question and answer from the quiz.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement