Function Basics

 

Function definition format (syntax)

 

return-value-type[output]  function-name( parameter-list [input_1, input_2, input_3, input_...] )
{
   declarations and statements
}

 

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 ({}).

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

  1. Function name

  2. Parameters – what the function takes in

  3. 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

  1. Divide and conquer

  2. Software reusability

  3. Avoid code repetition

The C Standard Library

 

A programmer can create his own functions.

Programmers will often use the C library functions ( Use these as building blocks ).

 

Avoid re-inventing the wheel

Notes:

3 Ways to return control to the point at which the function was invoked:

  1. If the function does not return a result , the control is returned when the function ending right brace is reached .

  2. OR by executing the statement

  3. If the function does return a result , you should use