C++ Tutorial/Function/function definition

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Compare pass-by-value and pass-by-reference with references

#include <iostream>
using std::cout;
using std::endl;
int byValue( int ); 
void byReference( int & ); 
                                                   
int main()
{
   int x = 2; 
   int z = 4; 
   cout << byValue( x ) << endl;  
   cout << "x = " << x << endl;
   byReference( z );
   cout << "z = " << z << endl;
   return 0;
} 
int byValue( int number )
{
   return number *= number;
}
void byReference( int &numberRef )
{
   numberRef *= numberRef; 
}
4
x = 2
z = 16

Default arguments with parameters

#include <iostream>
using std::cout;
using std::endl;
int f( int length = 1, int width = 1, int height = 1 );
int main()
{
   cout << f();
   
   cout << f( 10 );
        
   cout << f( 10, 5 );
   
   cout << f( 10, 5, 2 ) << endl;
   return 0;
} 
int f( int length, int width, int height )
{ 
   return length * width * height;
}
11050100

Define function to add two parameters together

#include <iostream>
 
 int Add (int x, int y)
 {
     std::cout << "In Add(), received " << x << " and " << y << "\n";
     return (x+y);
 }
 
 int main()
 {
     std::cout << "The value returned is: " << Add(3,4);
     return 0;
 }
In Add(), received 3 and 4
The value returned is: 7

Definition of function template maximum

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

template < class T >  
T maximum( T value1, T value2, T value3 )
{
   T maximumValue = value1; 
   if ( value2 > maximumValue )
      maximumValue = value2;
   if ( value3 > maximumValue )
      maximumValue = value3;
   return maximumValue;
}

int main()
{
   int int1, int2, int3;
   int1 = 1;
   int2 = 2;
   int3 = 3;
   cout << "\nThe maximum integer value is: " << maximum( int1, int2, int3 );
      
   double double1, double2, double3;
   double1 = 1.1;
   double2 = 2.2;
   double3 = 3.3;
   cout << "\nThe maximum double value is: "<< maximum( double1, double2, double3 );
   char char1, char2, char3;
   char1 = "a";
   char2 = "b";
   char3 = "c";
   cout << "\nThe maximum character value is: "<< maximum( char1, char2, char3) << endl;
   return 0;
}
The maximum integer value is: 3
The maximum double value is: 3.3
The maximum character value is: c

Functions in an expression: order of evaluation

#include <iostream>
#include <ostream>
using namespace std;
int x = 1;
int f()
{
  x = 2;
  std::cout << "f()";
  return x;
}
int g()
{
  std::cout << "g()";
  return x;
}
int main()
{
  std::cout << f() / g() << "\n";
}
f()g()1

Functions that take no arguments.

#include <iostream>
using std::cout;
using std::endl;
void f1(); 
void f2( void ); 
int main()
{
   f1(); 
   f2(); 
   return 0; 
} 
void f1()
{
   cout << "f1 takes no arguments" << endl;
}
void f2( void )
{
   cout << "f2 also takes no arguments" << endl;
}
f1 takes no arguments
f2 also takes no arguments

The function call stack and activation records

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int f( int );
int main()
{
   int a = 10; 
   cout << a << " fd: " << f( a ) << endl;
   return 0;
} 
int f( int x )
{
   return x * x;
}
10 fd: 100

This program contains two functions: main() and f().

#include <iostream> 
using namespace std; 
 
void f(); // f"s protoype 
 
int main() 
{ 
  cout << "In main()\n"; 
 
  f(); // call f() 
 
  cout << "Back in main()\n"; 
 
  return 0; 
} 
 
// This if the function"s definition. 
void f() 
{ 
  cout << "Inside f()\n"; 
}
In main()
Inside f()
Back in main()