1.2.4 Function and parameter types

Factorial (keyboard typing)

The first call a function fact

#include <cstdio> 
#include <the iostream>
 the using  namespace STD;
 
// function definition can not fall
int FACT ( int ) // function called FACT INR main () { int I = . 5 ; COUT << FACT (I + . 1 ) < <endl; // get used to that output, direct call function, which is the factorial of 6 return 0 ; } int FACT ( int n-) { int I, F; F = . 1 ; for (I = . 1 : I <= n-; I ++) F = F * I; return F;Note that there% return }

The second definition of a reference type variable with the result // Note that the definition and use of reference type variable

#include <cstdio> 
#include <the iostream>
 the using  namespace STD;
 
// function definition can not fall
void fact1 ( int n-, int & x); // function type is empty, the front surface of the variable x & Ming added as a reference type x and n is a value type int main () { int I = . 5 , Result; fact1 (I + . 1 , Result); % Since the functions are not directly output type empty, to write out alone, i.e., can not be written cout << fact1 ( +. 1 I) COUT << Result << endl; return 0 ; } void fact1 ( int n-, int & X) { intI; X = . 1 ; for (I = . 1 ; I <= n-; I ++) X = X * I; % Note here no return value, because the reference shape corresponds to the type used by participants results back }

The last two can be together than to write

#include <cstdio> 
#include <the iostream>
 the using  namespace STD; 

int FACT ( int ); // function. 1 

void fact1 ( int n-, int & x); // variable x & preceded by a reference surface next x type, and n is a value of type 
    
    
int main () 
{ 
    int I = . 5 , Result; 
    COUT << FACT (I + . 1 ) << endl; 
    fact1 (I + . 1 , Result); 
    COUT << Result << endl;
     return  0 ; 
 } 
 
 int FACT ( int n)
 {
     int i,f;
     f=1;
     for(i=1;i<=n;i++)f=f*i;
     return f; 
     
 }
 
 void fact1(int n,int & x)
 {
     int i;
     x=1;
     for(i=1;i<=n;i++)x=x*i;
      
 }

 

Guess you like

Origin www.cnblogs.com/lysun/p/12563213.html