Monday, March 30, 2015

Functions

Hello!
Today I am going to tell you about functions. Functions are a set of instructions that can be used over and over. Each function has 1 job. void main ( ) is the only function that is in every single program no matter what. A function prototype tells what variable is going to return. In this case, the variable in the prototype is a float, which means a decimal number is going to return. A peramiter is information to give to the function. The peramiter holds a set of instructions for the . The function prototype goes before main, because if it goes after,  the computer would not  know what sphere, the name, meant. Here is a function example.
/* sphere.c */

   #include <stdio.h>                /* Include header file for printf. */
   #define PI 3.141592654            /* Define a constant. */

   float sphere( int rad );          /* Function prototype. */

   void main()                       /* Main program. */
   {
     float vol;                      /* Declare variable. */
     int radius = 3;                 /* Declare and initialize variable. */
     vol = sphere( radius );         /* Call function, get result, print it. */
     printf( "Volume: %f\n", vol );
   }

   float sphere( int rad )           /* Function. */
   { 
     float result;                   /* Local variable declaration. */
     result = rad * rad * rad;

     
result = 4 * PI * result / 3; return( result ); /* Result returned to main program. */

Over & Out
Elise

No comments:

Post a Comment