prog8_03.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 25 行

CPP
25
字号
// Program 8.3 Failing to modify the original value of a function argument
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

double change_it(double it);              // Function prototype

int main() {
  double it = 5.0;
  double result = change_it(it);

  cout << "After function execution, it = " << it     << endl
       << "Result returned is "             << result << endl;
  return 0;
}

// Function to attempt to modify an argument and return it
double change_it(double it) {
  it += 10.0;                            // This modifies the copy of the original
  cout << endl
       << "Within function, it = " << it << endl;
  return it;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?