Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. A function declaration is shown below. This example has a return type of void, indicating that
  2. the function does not provide a result to the caller. The caller must provide two arguments for the
  3. function to work with — an integer and a character.
  4. void myFunction(int i, char c);
  5. Without an actual defi nition to match this function declaration, the link stage of the compilation
  6. process will fail because code that makes use of the function myFunction() will be calling
  7. nonexistent code. The following defi nition prints the values of the two parameters.
  8. void myFunction(int i, char c)
  9. {
  10. std::cout << “the value of i is “ << i << std::endl;
  11. std::cout << “the value of c is “ << c << std::endl;
  12. }
  13. Elsewhere in the program, you can make calls to myFunction() and pass in constants or variables
  14. for the two parameters. Some sample function calls are shown here:
  15. myFunction(8, ‘a’);
  16. myFunction(someInt, ‘b’);
  17. myFunction(5, someChar);
  18. In C++, unlike C, a function that takes no parameters just has an empty
  19. parameter list. It is not necessary to use void to indicate that no parameters are
  20. taken. However, you should still use void to indicate when no value is returned.
  21. C++ functions can also return a value to the caller. The following function declaration and
  22. defi nition is for a function that adds two numbers and returns the result.
  23. int addNumbers(int number1, int number2);
  24. int addNumbers(int number1, int number2)
  25. {
  26. int result = number1 + number2;
  27. return result;
  28. }
  29. In C++11, every function has a local predefi ned variable __func__ that looks as follows:
  30. static const char __func__[] = “function-name”;
  31. This variable can for example be used for logging purposes:
  32. int addNumbers(int number1, int number2)
  33. {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement