Advertisement
35657

Untitled

Apr 5th, 2024
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int sum(int x, int y) {
  6.     return x + y;
  7. }
  8.  
  9. // x передается в функцию по значению, все действия выполняются с копией
  10. int square(int x) {
  11.     return x *= x;
  12. }
  13.  
  14. // передаем аргументы в функцию по указатели, все действия производим с оригиналом переменной
  15. void square2(int* x) {
  16.     *x *= *x;
  17. }
  18.  
  19. // передаем аргументы в функцию по ссылке, все действия проихводим с оригиналом переменной
  20. void square3(int& x) {
  21.     x *= x;
  22. }
  23.  
  24. int main() {
  25.     setlocale(LC_ALL, "ru");
  26.  
  27.     int a = 5;
  28.  
  29.     cout << square(a) << endl;
  30.  
  31.     cout << a << endl;
  32.  
  33.     square2(&a);
  34.  
  35.     cout << a << endl;
  36.  
  37.     int& x_ref = a;
  38.  
  39.     cout << x_ref << endl;
  40.  
  41.     square3(a);
  42.  
  43.     cout << a << endl;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement