Thread: Help with current code calling functions

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    10

    Help with current code calling functions

    So im currently working on this solution thread but can't seem to make it work when running. Whats going on?

    Code:
    #define_CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<math.h>
    #definePi 3.14
    
    
    
    
    double radius(); 
    
    
    int main(){
    // get an integer from the user and returns it
    // make 3 calls to this function: 
    // To get the radius of the circle from the user and return it to main (look up the formula if you aren't sure)
        double radius();
    // To get the length of the rectangle from the user and return it to main 
    //To get the width of the rectangle from the user and return it to main 
    //(Hint: you can prompt from the main function BEFORE you call your function.You do NOT need to prompt from inside this function) int GetInt(void);
    // takes two arguments, the length of the square returns the area int CalculateAreaRec(int length, int width); 
    // takes one argument, the radius of the circle and returns the area double CalculateAreaCir(int radius);
    
    
        double radius();
    
        return (0);
    }
    
    
    double radius(){
        int  radius;
        double areaCir;
    
    
        printf("What is the radius of the circle:\n");
        scanf("%d", &radius);
        areaCir = radius*radius*Pi;
        printf("The area of the circle is: %.2f\n", areaCir);
    
    
        return areaCir;
    }
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of calling radius in main, you keep declaring it. So, in the body of the main function, remove one of the declarations of radius and change the other to:
    Code:
    radius();
    By the way, a function declaration with an empty parentheses for the arguments says that the number of types of the arguments are not specified. It is better to specify them, so in this case the declaration before main should be:
    Code:
    double radius(void);
    It is good to do the same for the definition of the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    double function(void)
    
    int main(void)
    {
    
    double var = function();
    printf("%lf\n",var);
    //or
    printf("%lf\n", function() );
    
    return 0;
    }
    
    double function(void)
    {
    
    code
    return answer;
    }
    or put the entire function above main, then main.
    Code:
    double function(void)
    {
    code
    return answer;
    }
    
    int main (void)
    {
    
    double var = function();
    printf("%lf\n", var);
    //or
    double var;
    var = function();
    printf("%lf\n",var);
    //or
    printf("%lf\n", function() ); 
    
    return 0;
    }
    but, you have your printf in your function already soo...that kind of defeats the point of a return value. Unless you're just checking it prior to getting that return value.
    as far as proper etiquette, the function would just do the math and return the value then the user would do his or hers printf on the textual formatting with the return value.

    Code:
    double function(int rad)
    {
      return (rad*rad*Pi);
    }
    just so you can see the different methods of doing this.
    Code:
    #include <stdio.h>
    
    double function( int rad)
    {
     #define Pi 3.14
     
     return (rad*rad*Pi);
    }
    
    int main(void)
    {
     int rady;
     printf("give me something\n");
     scanf("%d", &rady);
    printf("whatever you want to say here: = %lf \n",function(rady) );
    
    return 0;
    }
    P.S. you do not need the math header either, because you are doing the math in your function. Not using a function included in the math header .

    mod: I just read the comments in your code: Hint:

    to get two variables off the cli one after the other just print out what you want then scanf twice, one after the other in logical order to grab the two of them, then process them accordingly.

    these are touching, they need spaces
    Code:
    #include<stdio.h>
    #include<math.h>
    #definePi 3.14
    
    //like this
    
    #include <stdio.h>
    #include <math.h>
    #define Pi 3.14
    
    Last edited by userxbw; 10-09-2017 at 12:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling C functions from C++ code
    By starshiptrooper in forum C++ Programming
    Replies: 34
    Last Post: 02-17-2012, 11:05 AM
  2. Replies: 4
    Last Post: 05-01-2010, 12:17 PM
  3. calling functions & assembly code
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2004, 03:27 PM
  4. How do I get the current date in my code?
    By FromHolland in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 01:37 PM

Tags for this Thread