Home > Programming > Functions using the "C" programming language > example08.c

 

Previous

Next


/*
    In this example we demonstrate how to pass stuctures as function parameters.
    We 'll calculate the distance of a 3D point from the point(0, 0, 0) (axes beginning)
*/


#include <stdio.h>
#include <math.h>

/*
** COORDINATES is a structure to represent the coordinates of a 3D point
*/
struct COORDINATES {
    int x;
    int y;
    int z;
};

/*
    Function prototype. Function distance calculate the distance.
*/

float distance(struct COORDINATES);

/*
    Here is the main program
*/

void main(void)
{
    struct COORDINATES c;
/*
    Initial value for the variable c. Initial value could also be given by writing:
    struct COORDINATES c={5,2,4};
*/
    c.x = 5;
    c.y = 2;
    c.z = 4;
    printf("The distance of (%d,%d,%d) is %f\n",c.x,c.y,c.z,distance(c));
}

float distance(struct COORDINATES c)
{
    return sqrt(c.x*c.x+c.y*c.y+c.z*c.z);
}


© 2004 Jim Valavanis

Previous

Next

1