Function Basics |
Function definition format (syntax) |
It's called a function definition because we are defining the function.
We are saying, "This is a function named function_name, whose inputs are
input_1, input_2, etc., and whose output is output.
When it is called, the
function will execute the code in between its curly braces ({}).
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
Declarations and statements: function body (block)
Variables can be declared 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;
At this point, let's refine our sample function definition. When programmers talk about functions, instead of the word input they usually use the word parameter. A parameter to a function is nothing more than an input to a function. At the same time, instead of using the word output, programmers generally refer to the return of a function. A particular function "returns" a value. So, here is our updated function definition:
return_type function_name (parameter_1, parameter_2, parameter_3, parameter_...)
{
// code to execute inside function
}
Notice that in place of output, the function definition
says return_type. That's because when we are actually writing a function
definition, we'll put the return type there, immediately preceding the name of
the function. The return type is nothing more than a plain old variable type,
such as int, or
double, etc.
All variables declared inside functions are local variables known only in function defined.
Function Prototypes |
Function name
Parameters – what the function takes in
Return type – data type function returns (default int)
Used to validate function calls.
Prototype only needed if function definition comes after use in program.
| With Prototype | Without Prototype |
|
|
|
#include <stdio.h>
int square( int ); /* function prototype */
int main()
{
int x;
for ( x = 1; x <= 10; x++ )
printf( "%d ", square( x ) );
printf( "\n" );
return 0;
}
/* Function definition */
int square( int y )
{
return y * y;
}
==
Finding the maximum of
three integers */
#include <stdio.h>
int maximum( int, int, int ); /* function prototype */
int main()
{
int a, b, c;
printf( "Enter three integers: " );
scanf( "%d%d%d", &a, &b, &c );
printf( "Maximum is: %d\n", maximum( a, b, c ) );
return 0;
}
/* Function maximum definition */
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}
Benefits of functions |
Divide and conquer
Manageable program development
Software reusability
Use existing functions as building blocks for new programs.
Abstraction - hide internal details (library functions).
Avoid code repetition
The C Standard Library |