函数调用

 1 //示例代码    
 2  // return the greatest common divisor 
 3      int gcd(int v1, int v2) 
 4      { 
 5          while (v2) { 
 6              int temp = v2; 
 7              v2 = v1 % v2; 
 8              v1 = temp; 
 9          } 
10          return v1; 
11      }      // return the greatest common divisor 
12      int gcd(int v1, int v2) 
13      { 
14          while (v2) { 
15              int temp = v2; 
16              v2 = v1 % v2; 
17              v1 = temp; 
18          } 
19          return v1; 
20      } 
1   // get values from standard input 
2      cout << "Enter two values: \n"; 
3      int i, j; 
4      cin >> i >> j; 
5      // call gcd on arguments i and j 
6      // and print their greatest common divisor 
7      cout << "gcd: " << gcd(i, j) << endl; 
8  

如果给定 15 和 123 作为程序的输入,程序将输出 3。
函数调用做了两件事情:用对应的实参初始化函数的形参,并将控制权转移给被调用函数。 主调函数的执行被挂起, 被调函数开始执行。 函数的运行以形参的 (隐式)定义和初始化开始。也就是说,当我们调用 gcd 时,为 v1 和 v2 的 int 型变量, 并将这两个变量初始化为调用 gcd 时传递的实参值。在上例中,v1 的初值为 i,而 v2 则初始化为 j 的值。

原文地址:https://www.cnblogs.com/tingshuixuan2012/p/2984612.html