Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int sum(int x, int y) {
- return x + y;
- }
- // x передается в функцию по значению, все действия выполняются с копией
- int square(int x) {
- return x *= x;
- }
- // передаем аргументы в функцию по указатели, все действия производим с оригиналом переменной
- void square2(int* x) {
- *x *= *x;
- }
- // передаем аргументы в функцию по ссылке, все действия проихводим с оригиналом переменной
- void square3(int& x) {
- x *= x;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- int a = 5;
- cout << square(a) << endl;
- cout << a << endl;
- square2(&a);
- cout << a << endl;
- int& x_ref = a;
- cout << x_ref << endl;
- square3(a);
- cout << a << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement