13 Oktober, 2009

Function Definitions & Function Prototypes

Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}

– Function-name: any valid identifier
– Return-value-type: data type of the result (default int)
• void – indicates that the function returns nothing
– Parameter-list: comma separated list, declares parameters
• A type must be listed explicitly for each parameter unless, the parameter is of type int
– Definitions and statements: function body (block)
• Variables can be defined inside blocks (can be nested)
• Functions can not be defined inside other functions
– Returning control
• If nothing returned
– return;
– or, until reaches right brace
• If something returned
– return expression;

Function prototype
– Function name
– Parameters – what the function takes in
– Return type – data type function returns (default int)
– Used to validate functions
– Prototype only needed if function definition comes after use in program
– The function with the prototype
int maximum( int x, int y, int z );
• Takes in 3 ints
• Returns an int
• Promotion rules and conversions
– Converting to lower types can lead to errors


0 komentar:

Posting Komentar