PS-Trainer C - Entwicklung
Programm - Flusskontrolle: Funktionen
Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer
PS-Trainer PS-Trainer

Program Flow Control  
Functions Overview of Functions
Return Type The return type of a function establishes the size and type of the value returned by the function
Storage Class The storage-class specifier in a function definition gives the function either extern or static storage class.
Arguments The arguments are a list of expressions (separated by commas), passed to the function by the calling program..
return Statement The return statement terminates the execution of a function and returns control to the calling function


Overview of Functions
Functions must have a definition and should have a declaration, although a definition can serve as a declaration if the declaration appears before the function is called. The function definition includes the function body—the code that executes when the function is called.

A function declaration establishes the name, return type, and attributes of a function that is defined elsewhere in the program. A function declaration must precede the call to the function.
This is why the header files containing the declarations for the run-time functions are included in your code before a call to a run-time function.
If the declaration has information about the types and number of parameters, the declaration is a prototype. See Function Prototypes for more information.
The compiler uses the prototype to compare the types of arguments in subsequent calls to the function with the function's parameters and to convert the types of the arguments to the types of the parameters whenever necessary.

A function call passes execution control from the calling function to the called function.
The arguments, if any, are passed by value to the called function.
Execution of a return statement in the called function returns control and possibly a value to the calling function.

Return Type of Functions
The return type of a function establishes the size and type of the value returned by the function
and corresponds to the type-specifier in the syntax below:

Syntax
function-definition :
declaration-specifiers opt attribute-seq opt declarator declaration-list opt
compound-statement /* attribute-seq is Microsoft Specific */

declaration-specifiers :
storage-class-specifier declaration-specifiers opt
type-specifier declaration-specifiers opt
type-qualifier declaration-specifiers opt

type-specifier :

void
char
short
int
long
float
double
signed
unsigned

struct-or-union-specifier
enum-specifier
typedef-name


The type-specifier can specify any fundamental, structure, or union type.
If you do not include type-specifier, the return type int is assumed.
The return type given in the function definition must match the return type in declarations of the function elsewhere in the program.

A function returns a value when a return statement containing an expression is executed. The expression is evaluated, converted to the return value type if necessary, and returned to the point at which the function was called.
If a function is declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

You need not declare functions with int return type before you call them, although prototypes are recommended so that correct type checking for arguments and return values is enabled.
The following examples illustrate function return values.
typedef struct
{
tabchar name[20];
tabint id;
tablong class;
} STUDENT;

/* Return type is STUDENT: */

STUDENT sortstu( STUDENT a, STUDENT b )
{ return ( (a.id < b.id) ? a : b ); }

This example defines the STUDENT type with a typedef declaration and defines the function sortstu to have STUDENT return type. The function selects and returns one of its two structure arguments. In subsequent calls to the function, the compiler checks to make sure the argument types are STUDENT.

Note Efficiency would be enhanced by passing pointers to the structure, rather than the entire structure.

char *smallstr( char s1[], char s2[] )
{
tabint i;

tabi = 0;
tabwhile ( s1[i] != '\0' && s2[i] != '\0' )
tabi++;
tabif ( s1[i] == '\0' )
tabreturn ( s1 );
tabelse
tabreturn ( s2 );
}

This example defines a function returning a pointer to an array of characters. The function takes two character arrays (strings) as arguments and returns a pointer to the shorter of the two strings. A pointer to an array points to the first of the array elements and has its type; thus, the return type of the function is a pointer to type char.


Storage Class of Functions
The storage-class specifier in a function definition gives the function either extern or static storage class.

Syntax
function-definition :
declaration-specifiers opt attribute-seq opt declarator declaration-list opt
compound-statement /* attribute-seq is Microsoft Specific */

declaration-specifiers :
storage-class-specifier declaration-specifiers opt
type-specifier declaration-specifiers opt
type-qualifier declaration-specifiers opt

storage-class-specifier : /* For function definitions */
extern
static


If a function definition does not include a storage-class-specifier, the storage class defaults to extern.
You can explicitly declare a function as extern, but it is not required.

If the declaration of a function contains the storage-class-specifier extern, the identifier has the same linkage as any visible declaration of the identifier with file scope. If there is no visible declaration with file scope, the identifier has external linkage. If an identifier has file scope and no storage-class-specifier, the identifier has external linkage. External linkage means that each instance of the identifier denotes the same object or function. See Lifetime, Scope, Visibility, and Linkage for more information about linkage and file scope.

Block-scope function declarations with a storage-class specifier other than extern generate errors.
A function with static storage class is visible only in the source file in which it is defined. All other functions, whether they are given extern storage class explicitly or implicitly, are visible throughout all source files in the program. If static storage class is desired, it must be declared on the first occurrence of a declaration (if any) of the function, and on the definition of the function.

Microsoft Specific !
When the Microsoft extensions are enabled, a function originally declared without a storage class (or with extern storage class) is given static storage class if the function definition is in the same source file and if the definition explicitly specifies static storage class.
When compiling with the /Ze compiler option, functions declared within a block using the extern keyword have global visibility. This is not true when compiling with /Za. This feature should not be relied upon if portability of source code is a consideration.

Arguments of Functions
The arguments in a function call have this form:
expression ( expression-list opt ) /* Function call */

In a function call, expression-list is a list of expressions (separated by commas).
The values of these latter expressions are the arguments passed to the function.
If the function takes no arguments, expression-list should contain the keyword void.

An argument can be any value with fundamental, structure, union, or pointer type.
All arguments are passed by value. This means a copy of the argument is assigned to the corresponding parameter.
The function does not know the actual memory location of the argument passed.
The function uses this copy without affecting the variable from which it was originally derived.

Although you cannot pass arrays or functions as arguments, you can pass pointers to these items.
Pointers provide a way for a function to access a value by reference. Since a pointer to a variable holds the address of the variable, the function can use this address to access the value of the variable. Pointer arguments allow a function to access arrays and functions, even though arrays and functions cannot be passed as arguments.

The order in which arguments are evaluated can vary under different compilers and different optimization levels. However, the arguments and any side effects are completely evaluated before the function is entered.
The expression-list in a function call is evaluated and the usual arithmetic conversions are performed on each argument in the function call. If a prototype is available, the resulting argument type is compared to the prototype's corresponding parameter. If they do not match, either a conversion is performed, or a diagnostic message is issued. The parameters also undergo the usual arithmetic conversions.

The number of expressions in expression-list must match the number of parameters, unless the function's prototype or definition explicitly specifies a variable number of arguments. In this case, the compiler checks as many arguments as there are type names in the list of parameters and converts them, if necessary, as described above. See Calls with a Variable Number of Arguments for more information.

If the prototype's parameter list contains only the keyword void, the compiler expects zero arguments in the function call and zero parameters in the definition. A diagnostic message is issued if it finds any arguments.
Calls with a Variable Number of Arguments
A partial parameter list can be terminated by the ellipsis notation, a comma followed by three periods (, ...), to indicate that there may be more arguments passed to the function, but no more information is given about them. Type checking is not performed on such arguments. At least one parameter must precede the ellipsis notation and the ellipsis notation must be the last token in the parameter list. Without the ellipsis notation, the behavior of a function is undefined if it receives parameters in addition to those declared in the parameter list.

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.

All the arguments specified in the function call are placed on the stack unless the __fastcall calling convention is specified. The number of parameters declared for the function determines how many of the arguments are taken from the stack and assigned to the parameters. You are responsible for retrieving any additional arguments from the stack and for determining how many arguments are present. The STDARGS.H file contains ANSI-style macros for accessing arguments of functions which take a variable number of arguments. Also, the XENIX- style macros in VARARGS.H are still supported.

This sample declaration is for a function that calls a variable number of arguments:
int average( int first, ...);
Microsoft Specific !
To maintain compatibility with previous versions of Microsoft C, a Microsoft extension to the ANSI C standard allows a comma without trailing periods (,) at the end of the list of parameters to indicate a variable number of arguments. However, it is recommended that code be changed to incorporate the ellipsis notation.

return Statement
The return statement terminates the execution of a function and returns control to the calling function.
Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function. See Return Type for more information.

Syntax
jump-statement :
return expression opt ;


The value of expression, if present, is returned to the calling function. If expression is omitted, the return value of the function is undefined. The expression, if present, is converted to the type returned by the function. If the function was declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined. If a return value is not required, declare the function to have void return type; otherwise, the default return type is int.

Many programmers use parentheses to enclose the expression argument of the return statement.
However, C does not require the parentheses.

This example demonstrates the return statement:
void draw( int I, long L );
long sq( int s );
int main()
{
long y;
int x;

y = sq( x );
draw( x, y );
return();
}

long sq( int s )
{ return( s * s ); }

void draw( int I, long L )
{
/* Statements defining the draw function here */
return;
}

In this example, the main function calls two functions: sq and draw. The sq function returns the value of x * x to main, where the return value is assigned to y. The draw function is declared as a void function and does not return a value. An attempt to assign the return value of draw would cause a diagnostic message to be issued.


Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1