Create a function to Calculate the Absolute Value - C Function

C examples for Function:Function Definition

Description

Create a function to Calculate the Absolute Value

Demo Code

#include <stdio.h>

float absoluteValue (float x){
    if (x < 0)
        x = -x;/*from  w  ww .ja  v  a 2 s . co  m*/

    return x;
}

int main (void){
    float f1 = -1.5, f2 = 2.0, f3 = -5.0;
    int i1 = -7;
    float result;

    result = absoluteValue (f1);
    printf ("result = %.2f\n", result);
    printf ("f1 = %.2f\n", f1);

    printf ("%.2f\n", absoluteValue (-6.0) / 4);

    return 0;
}

Result


Related Tutorials