Help with functions

This is a discussion on Help with functions within the C++ Programming forums, part of the General Programming Boards category; I am brand new to programming and am takinga class in C++. So far I have been able to successfully ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    16

    Help with functions

    I am brand new to programming and am takinga class in C++. So far I have been able to successfully create the programs requested and get the needed output but this problem has got me stumped. The original problem is to create a program that prompts the user to enter the center and a point on the circle. Then it should output (to screen) the circle's radius, diameter, circumference, and area. I have defined all the parameters and created the functions but I have the following errors in the debugger (btw I am using visual studio2010).

    errors:
    IntelliSense: no instance of overloaded function "distance" matches the argument list. line 65 column 7

    IntelliSense: too few arguments in function call. Line 65, 69, 73, 77 and 81

    Because of these errors nothing runs/outputs.

    Here is the code that I have:

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    	//function to figure out the distance
    	int distance (int x1, int y1, int x2, int y2)		
                 {  
    		int dx = x2 - x1;  
    		int dy = y2 - y1;  
    		double dsquared = dx*dx + dy*dy;  
    		double result = sqrt (dsquared);  
    		return result;
    	} 
    	
                    //function to figure out the radius
    	int radius (int x1, int y1, int x2, int y2) 	
                  {  
    		double radius = distance (x1, y1, x2, y2);  
    		return radius;
    	} 
    
                    //function to figure out the circumference
    	int circumference(double radius) 	
                   {
    		double circumference = 3.1416 * (radius * 2);
    		return circumference;
    	}  
    
                     //function to figure out the area
    	int area(double radius)	
                   {   
    		double area = 3.1416 * radius * radius;   
    		return area;
    	} 
    
                    //function to figure out the diameter
    	int diameter(double radius)
    	{
    		double diameter = 2 * radius; 
    		return diameter;
    
    	}
    
    
    int main()
    { 
    	int x1;	//variable to store the center point of the circle
    	int x2;	//variable to store the second point on the circle
    	int y1;	//variable to store the center point of the circle
    	int y2;	//variable to store the second point on the circle
    
    	cout <<"Enter a center point: ";
    	cin  >> x1, y1; 
    	
    	cout <<"Enter a point on the circle: ";
    	cin  >> x2, y2; 
    	
    	//distance return
    	cout <<"The distance of the circle is: "
    		 << distance() << endl; 
    	
    	//area return
    	cout <<"The area of the circle is: "
    		 << area() << endl; 
    	
    	//radius return
    	cout <<"The radius of the circle is: "
    		 << radius() << endl; 
    	
    	//circumference return
    	cout <<"The circumference of the circle is: "
    		 << circumference() << endl; 
    	
    	//diameter
    	cout <<"The diameter of the circle is: "
    		 << diameter() << endl; 
    
    	return 0;
    
    }
    Any help or direction at this point would be greatly appricated. I have tried several different things to get it to work but to no avail.

    Many thanks.

    lostinprogramC+
    Last edited by lostinprogramC+; 01-20-2011 at 12:19 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    In your code you have defined your function as:

    Code:
    int distance (int x1, int y1, int x2, int y2)
    and then you call your function as:

    Code:
    distance()
    Your function call and function definition must agree as to the number an type of arguments.

    Also this line will cause an error:

    Code:
    cin  >> x1, y1;
    It should be:

    Code:
    cin  >> x1 >> y1;
    You are making these mistakes in several places in your code.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    So if the function is

    Code:
    int distance( int x1, int y1, int x2, int y2)
    then the call to the function should look the same...? Sorry functions have gotten me very confused.

    like this
    Code:
    distance( int x1, int y1, int x2, int y2)
    Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by lostinprogramC+ View Post
    So if the function is

    Code:
    int distance( int x1, int y1, int x2, int y2)
    then the call to the function should look the same...? Sorry functions have gotten me very confused.

    like this
    Code:
    distance( int x1, int y1, int x2, int y2)
    Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.
    yup, the function you call must always match the function prototype, if it doesn't the compiler doesn't know what to do and starts moaning. however, unless you included the function from another file.

    distance()
    distance(int,int,int,int) have the same name, but have completely different signatures.
    when you get further..you'll learn of overloaded functions.
    You ended that sentence with a preposition...Bastard!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lostinprogramC+ View Post
    So if the function is

    Code:
    int distance( int x1, int y1, int x2, int y2)
    then the call to the function should look the same...? Sorry functions have gotten me very confused.

    like this
    Code:
    distance( int x1, int y1, int x2, int y2)
    Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.
    Nope... the function call names the actual variables being passed in...

    as in ... distance(a,b,c,d);

    Re-listing the various types of the variables would trigger a syntax error.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    I have changed the call of the function to match the function that has been declared and I receive the following errors:

    Error 6 error C2144: syntax error : 'int' should be preceded by ')'
    Error 7 error C2660: 'distance' : function does not take 0 arguments
    Error 8 error C2059: syntax error : ')'
    Error 9 error C2664: 'area' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int,int)' to 'double'

    and there are several of them.

    I am following the examples that are in the book but nothing seems to be working correctly. I know that it is my lack of understanding when it comes to functions. Could you please elaborate on how to use them and call them. (act like you are talking to a child maybe then I will understand it. lol ;-)

    examples would be great!

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by lostinprogramC+ View Post
    I have changed the call of the function to match the function that has been declared and I receive the following errors:

    Error 6 error C2144: syntax error : 'int' should be preceded by ')'
    Error 7 error C2660: 'distance' : function does not take 0 arguments
    Error 8 error C2059: syntax error : ')'
    Error 9 error C2664: 'area' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int,int)' to 'double'

    and there are several of them.

    I am following the examples that are in the book but nothing seems to be working correctly. I know that it is my lack of understanding when it comes to functions. Could you please elaborate on how to use them and call them. (act like you are talking to a child maybe then I will understand it. lol ;-)

    examples would be great!
    ok..you are returning doubles instead of ints.
    an int simply is a whole number.
    a float is a number with decimal part. a double is a float but allows bigger memory.

    example
    Code:
       int foo()
      { 
          double a = 10.0f;
          return a ; 
      }
    I am returning a, which is double but the return type is an int..there is a mismatch
    so i suggest you change all the return type to the type you're returning.
    so use double area() or whatever it is.

    also the functions must match the exact order of their prototype

    void foo(a,b,c)
    cannot be void foo(b,c,a)
    You ended that sentence with a preposition...Bastard!

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    ok so if I create the following function:

    Code:
    double distance (double x1, double y1, double x2, double y2)			{  
    		double dx = x2 - x1;  
    		double dy = y2 - y1;  
    		double dsquared = dx*dx + dy*dy;  
    		double result = sqrt (dsquared);  
    		return result;
    	}
    this is how it should be called?

    Code:
    	cout <<"The distance of the circle is: "
    	        << distance(double x1, double y1, double x2, double y2) 
    	        << endl;

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't think you have any variables called "double x1". You might have a variable called "x1", though.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lostinprogramC+ View Post
    this is how it should be called?

    Code:
    	cout <<"The distance of the circle is: "
    	        << distance(x1, y1, x2, y2) 
    	        << endl;
    see above...

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    I know I need to change my declarations but am still totally frustrated as to why this isn't working. Is the code even close to being written correctly for calling the distance function?

    If I throw the cumputer will that make it work properly???????

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lostinprogramC+ View Post
    I know I need to change my declarations but am still totally frustrated as to why this isn't working. Is the code even close to being written correctly for calling the distance function?

    If I throw the cumputer will that make it work properly???????
    The thing to keep in mind is that your functions aren't any different than anybody else's functions. You call sqrt (correctly) by putting the variable inside the parentheses. So do the same for distance: put the variable names inside the parentheses.

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    If I throw the cumputer will that make it work properly???????
    you'll find quickly that functions are the easiest thing next to variables. I think they are.

    if you've modified your code. can you repost it?
    You ended that sentence with a preposition...Bastard!

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    ok changed some code so here is the big sha bang again..... :-P

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    		
    	double distance (double x1, double y1, double x2, double y2)		//function to figure out the distance
    	{  
    		double dx = x2 - x1;  
    		double dy = y2 - y1;  
    		double dsquared = dx*dx + dy*dy;  
    		double result = sqrt (dsquared);  
    		return result;
    	} 
    		
    	double radius (double x1, double y1, double x2, double y2)			//function to figure out the radius
    	{  
    		double radius = distance (x1, y1, x2, y2);  
    		return radius;
    	} 
    
    	double circumference(double radius)					//function to figure out the circumference
    	{
    		double circumference = 3.1416 * (radius * 2);
    		return circumference;
    	}  
    
    	double area(double radius)								//function to figure out the area
    	{   
    		double area = 3.1416 * radius * radius;   
    		return area;
    	} 
    
    	double diameter(double radius)							//function to figure out the diameter
    	{
    		double diameter = 2 * radius; 
    		return diameter;
    
    	}
    
    
    int main()
    { 
    	double x1;								//variable to store the center point of the circle
    	double x2;								//variable to store the second point on the circle
    	double y1;								//variable to store the center point of the circle
    	double y2;								//variable to store the second point on the circle
    
    	cout <<"Enter a center point: ";
    	cin  >> x1 >> y1; 
    		
    	cout <<"Enter a point on the circle: ";
    	cin  >> x2 >> y2; 
    	
    	
    	cout <<"The distance of the circle is: "
    		 << distance(double x1, double y1, double x2, double y2) 
    		 << endl; 
    	
    	//area return
    	cout <<"The area of the circle is: "
    		 << area(radius) << endl; 
    	
    	//radius return
    	cout <<"The radius of the circle is: "
    		 << radius(x1, y1, x2, y2) << endl; 
    	
    	//circumference return
    	cout <<"The circumference of the circle is: "
    		 << circumference(radius) << endl; 
    	
    	//diameter
    	cout <<"The diameter of the circle is: "
    		 << diameter(radius) << endl; 
    
    	return 0;
    
    }
    ok silly question, can I have more than one variable in the parameter? or is it best ot have one parameter?

    ie:

    Code:
    distance(double x1, double y1, double x2, double y2)
    
    or 
    
    distance(x1)
    *I still think that throwing the cumpter will help ;-P

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    	cout <<"The distance of the circle is: "
    		 << distance(double x1, double y1, double x2, double y2) 
    		 << endl;
    You can't specify the argument type when calling a function. You should have this instead:
    Code:
    	cout <<"The distance of the circle is: "
    		 << distance(x1, y1, x2, y2) 
    		 << endl;
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM

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