[ Pobierz całość w formacie PDF ]

other arguments, and returns type int.
int extern f2(const int ...);
In C, a comma is required before the ellipsis:
int extern f2(const int, ...);
Functionf3has a return type int, and takes a int argument with a default value
that is the value returned from functionf2:
const int j = 5;
int f3( int x = f2(j) );
Functionf6is a const class member function of classX, takes no arguments,
and has a return type of int:
class X
{
public:
int f6() const;
};
Functionf4takes no arguments, has return type void, and can throw class
objects of typesXandY.
class X;
class Y;
// ...
void f4() throw(X,Y);
Functionf5takes no arguments, has return type void, and will call
unexpected() if it throws an exception of any type.
Chapter 7. Functions 127
Function Declarations
void f5() throw();
v  Default Arguments in C++ Functions on page 137
v  const and volatile Member Functions on page 212
v  Exception Specifications on page 329
v  extern Storage Class Specifier on page 25
v  Chapter 4. Declarators on page 55
Function Definitions
A function definition contains a function declaration and the body of a function.
function_name (
extern type_specifier
static
,
)
parameter_declaration , ...
: constructor_initializer_list exception_specification
const
volatile
block_statement
A function definition contains the following:
v An optional storage class specifier extern or static, which determines the scope of
the function. If a storage class specifier is not given, the function has external
linkage.
v
A type specifier, which determines the type of value that the function returns.
In C, the type specifier is optional. If a type specifier is not given, the
function has type int.
The type specifier is not optional in C++.
v
A function declarator, which is the function name followed by a parenthesized list
of parameter types and names. It can further describe the type of the value that
the function returns, and lists the type and name of each parameter that the
function expects. In the following function dfinition,f(inta,intb)is the
function declarator:
int f(int a, int b) {
return a + b;
}
v Optional const or volatile specifiers after the function declarator. Only
member functions may have these.
128 C/C++ Language Reference
Function Definitions
v An optional exception specification, which limits the function from
throwing only a specified list of exceptions.
v
A block statement, which contains data definitions and code.
You can also have a function try block instead of a block statement. If the
function definition is a constructor, you can have a constructor initializer list
before the block statement. In the following class definition,x(0),y('c')is a
constructor initializer list:
class A {
int x;
char y;
public:
A() : x(0), y('c') { }
};
A function can be called by itself or by other functions. By default, function
definitions have external linkage, and can be called by functions defined in other
files. A storage class specifier of static means that the function name has global
scope only, and can be directly invoked only from within the same translation unit.
This use of static is deprecated in C++. Instead, place the function in the
unnamed namespace.
In C only, if a function definition has external linkage and a return type of
int, calls to the function can be made before it is visible because an implicit
declaration ofexternintfunc();is assumed.
All declarations for a given function must be compatible; that is, the return type is
the same and the parameters have the same type. Note that overloaded functions
have the same name.
The default type for the return value and parameters of a function is int,
and the default storage class specifier is extern. If the function does not return a
value, use the keyword void as the type specifier. You can use the keyword void
as a parameter declaration to indicate the function is not passed any arguments.
A function cannot return a function, array, or object with a volatile or const type,
but it can return a pointer to these or any other types.
In C, you cannot declare a function as a struct or union member.
In C, a function cannot return any type having the volatile or const
qualifier.
You cannot define an array of functions. You can, however, define an array of
pointers to functions.
The following example is a definition of the functionsum:
int sum(int x,int y)
{
return(x + y);
}
Chapter 7. Functions 129
Function Definitions
The functionsumhas external linkage, returns an object that has type int, and has
two parameters of typeintdeclared asxandy. The function body contains a
single statement that returns the sum ofxandy.
In the following example,aryis an array of two function pointers. Type casting is
performed to the values assigned toaryfor compatibility:
#include
typedef void (*ARYTYPE)();
int func1(void);
void func2(double a);
int main(void)
{
double num = 333.3333;
int retnum;
ARYTYPE ary[2];
ary[0]=(ARYTYPE)func1;
ary[1]=(ARYTYPE)func2;
retnum=((int (*)())ary[0])(); /* calls func1 */
printf("number returned = %i\n", retnum);
((void (*)(double))ary[1])(num); /* calls func2 */
return(0);
}
int func1(void)
{
int number=3;
return number;
}
void func2(double a)
{
printf("result of func2 = %f\n", a);
}
The following is the output of the above example:
number returned = 3
result of func2 = 333.333300
v  extern Storage Class Specifier on page 25
v  static Storage Class Specifier on page 28
v  Block Statement on page 145
v  Pointers on page 58
v  References on page 68
v  Structures on page 36
v  Unions on page 42
v  volatile and const Qualifiers on page 50
Ellipsis and void
An ellipsis at the end of the parameter specifications is used to specify that a
function has a variable number of parameters. The number of parameters is equal
to, or greater than, the number of parameter specifications. At least one parameter
declaration must come before the ellipsis.
int f(int, ...);
130 C/C++ Language Reference
Function Definitions
The comma before the ellipsis is optional. In addition, a parameter
declaration is not required before the ellipsis.
The comma before the ellipsis as well as a parameter declaration before the
ellipsis are both required in C.
Parameter promotions are performed as needed, but no type checking is done on
the variable arguments.
You can declare a function with no arguments in two ways:
int f(void);
int f();
An empty argument declaration list or the argument declaration list of
(void)indicates a function that takes no arguments.
An empty argument declaration list means that the function may take any
number or type of parameters.
The type void cannot be used as an argument type, although types derived from
void (such as pointers to void) can be used.
In the following example, the functionf()takes one integer argument and returns
no value, whileg()expects no arguments and returns an integer.
void f(int);
int g(void);
v  void Type on page 35
Examples of Function Definitions
The following example contains a function declaratori_sortwith table declared as
a pointer tointandlengthdeclared as type int. Note that arrays as parameters
are implicitly converted to a pointer to the element type.
/**
** This example illustrates function definitions.
** Note that arrays as parameters are implicitly
** converted to a pointer to the type.
**/
#include
void i_sort(int table[ ], int length);
int main(void)
{
int table[ ]={1,5,8,4};
int length=4;
printf("length is %d\n",length);
i_sort(table,length);
}
void i_sort(int table[ ], int length)
{ [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • michalrzlso.keep.pl