Thread: Dis-Function Issues

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    Angry Dis-Function Issues

    I have been learning C programming on my own and I am just trying to get the hang of functions. And so far I seem to be missing something. Maybe just a push in the right direction.

    Code:
    #include <stdio.h>
    
    
    #define PI 3.14
    
    
    int area_Circle(int a, int r);
    
    
    
    
    
    
    
    
    int main(){
    
    
     float r, a;
    
    
    
    
    
    
    
    
    while (printf("Enter a number to find the area of a circle,\nEnter Zero if you would like to quit!:")){
    
    
    
    
     scanf("%f", &r);
     if("%f", r>0)
     {
      
      printf("The area is %f\n", a);
     continue;
     }
    
    
    
    
     else
    
    
       {
    
    
         "%f", r < 1;
         break;
    
    
       }
       
    
    
    return 1;
    }
    
    
    
    
    int area_Circle(int a, int r);
    {
     a;
     a = PI *r*r;
    return a;     
        
    }
    
    
    }

    I though this would work this way, As I defined PI under the "#include" statement. Please help me understand what I missing here. Thanks in advance to any help.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    What trouble are you having exactly? Some comments:

    1. You have too much whitespace in your code, making it difficult to read. Try to limit blank lines to one at a time.
    2. You're using int in the calculation PI * r * r, so your answer will use integer arithmetic. It would be better to use double.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, you have to actually call the function if you want it to happen. You need to look at your if statements, because they work but don't make a lot of sense.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    As others have indicated, you are having problems without trying to use functions.
    Where does the function main() on line 16 end?
    Where does the while loop that begins on line 28 end?
    What is the condition for the while loop that begins on line 28? (See for example C loops)
    I would read the tutorials found here or at other sites.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Sorry Fixing white space now.

    And as far as my trouble goes. I am having a slight problem understanding why the function I defined at the bottom is not working. Meaning when I run the code I get 0.00000 as the area of a circle. (PI*r*r) is not doing what I tell it to do inside the fuction. So I guess the right question is?; why is my function
    Code:
    intarea_Circle(inta, intr);
    not woking?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you think the function actually happens. I would like to know why you think that.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by NewCode30 View Post
    Meaning when I run the code I get 0.00000 as the area of a circle. (PI*r*r) is not doing what I tell it to do inside the fuction.
    What makes you think 0.00 is the wrong answer? It depends on what you give it for a and r, also beause you are using int for your arguments, therefore your answer might be different from what you're expecting. By the way you need a space after "int" in your declaration:

    Code:
    int area_Circle(int a, int r);

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I don't think your using continue and break how you think

    while(number!=0){
    if(r>0){
    a=area_Circle(int a, int r);
    printf("The area is %f\n", a);
    }
    else

    etc etc.

    your using <1 as a break from the loop, why not just your while condition to <1 then

  9. #9
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    Question Clearer Picture

    Okay so I have removed the while loop and the if else statement. All that mucking about while, if/else was too confusing. All I am trying to do is understand writing a function and calling it. I have read many tutorials and watch many videos and am still having trouble with this. All I want this code to do is print the area of a circle using the formula (a = PI * r * r). When I run the code it compiles fine. But is not calculating the area from user input. Which is "r". I feel like I am missing something small here. And if I could just wrap my mind around said small thing I am missing I would finally understand. So here is the modified code. Again this is just so I can get a better understanding of what I need to do to get the outcome I want.

    Code:
    #include <stdio.h>
    #define PI 3.14
    
    
    int area (int a, int r); //Function Call?
    
    //Main Function right?
    int main(void){
     
     float r, a;
     printf("The area of a circle with a radius of: ");
     scanf("%f", &r);
     printf("is %f\n", a);
    return 0;
    }
    
    
    int area (int a, int r) //Function Definition?
    {
    a;
    a = PI*r*r;
    return ( a);
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the line you have marked as function call is ... well, not a function call. A function call would look like
    Code:
    a = area(4, 7)
    note the values, not types of variables, in the parentheses. The thing you have marked is a function prototype, which tells the compiler what types of values go into the function and what type of value comes out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function declaration issues
    By philgrek in forum C Programming
    Replies: 5
    Last Post: 03-01-2011, 12:28 AM
  2. Issues calling a function from within a function.
    By civix in forum C++ Programming
    Replies: 12
    Last Post: 11-06-2010, 12:23 AM
  3. function issues, please help!
    By mattagrimonti in forum C Programming
    Replies: 4
    Last Post: 12-05-2008, 02:52 AM
  4. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  5. Function issues
    By tammo21 in forum C Programming
    Replies: 3
    Last Post: 08-18-2002, 08:45 AM

Tags for this Thread