How to properly use a function?

This is a discussion on How to properly use a function? within the C Programming forums, part of the General Programming Boards category; Hi guys, so I'm getting myself really confused with functions and I don't really understand them. I am given a ...

  1. #1
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    How to properly use a function?

    Hi guys, so I'm getting myself really confused with functions and I don't really understand them. I am given a simple task which is:


    The following program is supposed to calculate and print the area of a triangle. Finish writing the program. You will need to write the areaOfTriangle() function as part of your solution. Make sure you observe the format of the output. In case you forgot, the formula for area of a triangle is -
    Code:
    area = 1/2 * base * height
    
    
    ...
    
    int main()
    {
        double base, height, area;
        printf("Enter the base and height => ");
        scanf(" ? ? ",&base,&height);
    
        area = areaOfTriangle(base,height);
    
        printf("The area is ???\n", area);
    
    ...
    
    Sample Output
    
    Enter the base and height => 7.26 6.83
    The area is 24.793
    
    


    Just calculate using a function and then use that function to printf the answer right? I'm not sure how exactly to do that. After many different things I did, I'm just completely stumped. Heres what I have.

    Code:
     
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    //Prototypes
    unsigned areaOfTriangle(base,height);
    
    
    //Declarations
    
    
    int main ()
    {
        printf("Mark Bacani\n");
        printf("Lab Ex #6\n");
        printf("Code::Blocks\n");
    
    
    //Local Declarations
    double base;
    double height;
    double area;
    //Calculations
      printf("Enter the base:\n");
        scanf("%lf",&base);
        printf("Enter the height:");
        scanf("%lf",&height);
    
    
    area = areaOfTriangle(base,height);
    
    
    
    
    
    
    //Result
    printf("The area is %lf \n", area);
    return 0;
    }
    
    
    //Prototype
    unsigned areaOfTriangle (base,height)
    {
    unsigned areaOfTriangle;
    
    
    areaOfTriangle =(1/2) * base * height;
    
    
    return areaOfTriangle;
    
    
    }
    I realize the prototype section is a complete mess. Functions are my kryptonite >

    Any help would be greatly appreciated!


  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    You might be interested in the messages I got when I compiled your program:
    Code:
    C:\Users\Josh2\Documents\bar\main.c|7|warning: parameter names (without types) in function declaration [enabled by default]|
    C:\Users\Josh2\Documents\bar\main.c||In function 'areaOfTriangle':|
    C:\Users\Josh2\Documents\bar\main.c|45|warning: type of 'base' defaults to 'int' [enabled by default]|
    C:\Users\Josh2\Documents\bar\main.c|45|warning: type of 'height' defaults to 'int' [enabled by default]|
    ||=== Build finished: 0 errors, 3 warnings ===|
    If that doesn't make much sense, the first number after the file name is the line number. The compiler thinks something is wrong with that line, so if you look the error should be obvious.

    In addition to that your formula for area has a problem. (1/2) is 0 because the calculation is done by integers. The decimal point gets truncated. So you will want to fix that by using decimal points instead.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float triangle(float a);
    
    int main()
    {
        float b,h;
        printf("Enter base:\n");
        scanf("%f",&b);
        printf("Enter height:\n");
        scanf("%f",&h);
        
        float a=b*h;
    
        /* 1st call of the function, before finding area.*/
        triangle(a);
    
        /* 2nd call of the function, after finding area.*/
        printf("The area = %.3f",triangle(a));
        
    return(0);
    }
    
    float triangle(float a)
    {
        float area=a/2;
    
    return(area);
    }
    Last edited by LTA85; 02-01-2013 at 03:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working properly. What am I missing?
    By figarojones in forum C Programming
    Replies: 5
    Last Post: 11-14-2012, 12:56 PM
  2. advise on how to properly use pow function
    By libchk in forum C Programming
    Replies: 12
    Last Post: 08-31-2011, 01:50 AM
  3. modf function doesn't work properly
    By paranoidgnu in forum C Programming
    Replies: 1
    Last Post: 06-29-2011, 10:59 AM
  4. How to properly use a C function?
    By John_L in forum C Programming
    Replies: 4
    Last Post: 05-30-2008, 03:01 AM
  5. Loop doesn't seem to function properly
    By TeQno in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2005, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21