Learn C - C Function






Variable Scope and Lifetime

This example involves a nested block of a loop:

You declare and define count2 inside the loop block:

count2 is re-created on every loop iteration with the initial value 0.

During each loop iteration, count2 is created, initialized, incremented, and destroyed.

The variable count1 exists at the main() block level.

It continues to exist while it is incremented.


    #include <stdio.h>
/*from   w ww .j ava  2  s.c  o m*/
    int main(void) {
      int count1 = 1;                               // Declared in outer block

      do {
        int count2 = 0;                             // Declared in inner block
        ++count2;
        printf("count1 = %d     count2 = %d\n", count1, count2);
      } while( ++count1 <= 10);

      // count2 no longer exists

      printf("count1 = %d\n", count1);
      return 0;
    }

The code above generates the following result.





A new scope

The following code has two variables called count.

Inside the loop block the local variable will "hide" the version of count that exists at the main() block level.

count referes to the one that was declared in the current block.

Inside the do-while loop, only the local version of count can be reached.

The printf() inside the loop block displays the local count value, which is always 1.

As soon as you exit the loop, the outer count variable becomes visible, and the last printf() displays its final value from the loop.


    #include <stdio.h>
/* w w  w.  j ava  2s . c  o  m*/
    int main(void) {
      int count = 0;                                // Declared in outer block
      do {
        int count = 0;                              // This is another variable called count
        ++count;                                    // this applies to inner count
        printf("count = %d\n", count);
      }
      while( ++count <= 10);                // This works with outer count

      printf("count = %d\n", count);       // Inner count is dead, this is outer count
      return 0;
    }

The code above generates the following result.





Variable Scope and Functions

The body of every function is a block.

The variables declared within a function are local to the function.

Defining a Function

The general form of a function looks like this:

Return_type  Function_name(type1 para1, type2 para2,...) {
   // Statements...
}

The statements in the function body can be absent.

The return value from a function with statements in the body is specified as void.

For a function with a non-void return type, its return statement must return a value of the specified return type.

The parameter specifies the type of value that should be passed to the function when it is called.

The value passed to a function corresponding to a parameter is referred to as an argument.

A function parameter consists of the type followed by the parameter name.

The general form for calling a function is the following expression:

Function_name(argument1, argument2, ...)

You use the function's name followed by a list of arguments separated by commas in parentheses.

A function call can appear as a statement on a line by itself, like this:

printf("hi.");

A function that's called like this can be a function with a return value.

In this case the returned value is discarded.

A function that has been defined with a return type of void can only be called like this.

A function that returns a value can be in an expression.

For example:

result = 2.0*sqrt(2.0);

Function Naming and Parameters

The name of a function can be any legal name in C.

Function parameters are placeholders for the arguments that need to be specified.

The parameters for a function are a list of parameter names with their types, and successive parameters are separated by commas.

When passing an argument to a function the argument value is copied to the function.

Return Value Type

Let's take another look at the general form of a function:

Return_type  Function_name(...
{
   // Statements...
}

The Return_type specifies the type of the value returned by the function.

Function Prototypes

You could define the function main() first and then the function Average().

// #include ...

int main(void) {
   ...

   Average...
}

double Average(double x[], size_t n) {
   ...
}

This won't compile.

When the compiler sees the Average() function in main(), it will not know the Average() function.

We can use the function prototype to solve this issue.

A function declaration, or function prototype, is a statement that defines the prototype of a function.

A function prototype defines function's name, its return value type, and the type of each of its parameters.

A prototype for a function is exactly the same as the function header with a semicolon at the end.

The following code shows how to create Function Prototypes.

// Function prototypes
double Average(double data_values[], size_t count);

int main(void) {
  ...
}

// Definitions for Average()