Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,530 people online right now. Registration is fast and FREE... Join Now!




function problem

 
Reply to this topicStart new topic

function problem

jaclyn_85
6 Nov, 2006 - 07:23 PM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2006
Posts: 17


My Contributions
the result for side_square works, but the results for perimeter and area come back as zero. what am i doing wrong????



This is the function:
CODE
/*  3 parameters  length (in), area (out), perimeter (out), no result */
  double area_and_perimeter_of_square ( double side_square, double area_square,
{
  area_square=  side_square * side_square;
return area_square;
  perimeter_square= side_square + side_square+ side_square + side_square;
  return perimeter_square;    
}
  


This is the main() to call the function:
CODE

/* area and perimeter of square */
  
   side_square = 3.2;
   area_and_perimeter_of_square( side_square, area_square, perimeter_square );
   printf("Square with side %.4lf has area = %.4lf and perimeter = %.4lf\n",
         side_square, area_square, perimeter_square );

}


User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Function Problem
6 Nov, 2006 - 07:28 PM
Post #2

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
CODE

double area_and_perimeter_of_square ( double side_square, double area_square, <----------here how bout closing it with ")"
{
  area_square=  side_square * side_square;
return area_square;
  perimeter_square= side_square + side_square+ side_square + side_square;
  return perimeter_square;    
}


and then function is limited to two...value to pass on unless you use a
refernce sort of like tha....




User is offlineProfile CardPM
+Quote Post

jaclyn_85
RE: Function Problem
6 Nov, 2006 - 07:37 PM
Post #3

New D.I.C Head
*

Joined: 16 Sep, 2006
Posts: 17


My Contributions
QUOTE(NyeNye @ 6 Nov, 2006 - 08:28 PM) *

CODE

double area_and_perimeter_of_square ( double side_square, double area_square, <----------here how bout closing it with ")"
{
  area_square=  side_square * side_square;
return area_square;
  perimeter_square= side_square + side_square+ side_square + side_square;
  return perimeter_square;    
}


and then function is limited to two...value to pass on unless you use a
refernce sort of like tha....




I don't understand what you mean. how would that help?
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Function Problem
6 Nov, 2006 - 07:46 PM
Post #4

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
you should close your declaration


double area_and_perimeter_of_square ( double side_square, double area_square)


This post has been edited by NyeNye: 6 Nov, 2006 - 07:47 PM
User is offlineProfile CardPM
+Quote Post

jaclyn_85
RE: Function Problem
6 Nov, 2006 - 07:52 PM
Post #5

New D.I.C Head
*

Joined: 16 Sep, 2006
Posts: 17


My Contributions
QUOTE(NyeNye @ 6 Nov, 2006 - 08:46 PM) *

you should close your declaration


double area_and_perimeter_of_square ( double side_square, double area_square)


and i tried that, and the results didn't change. which is why i was asking how would that help?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Function Problem
6 Nov, 2006 - 09:04 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Even though you used the same name variables in your main method and inside your function, they are not the same variables.

Currently in main, the only variable that you assign a value to is side_square = 3.2; and yet you are passing 3 values to your function.

Did you assign any values to the other variables before making a call to the function?

Also your function is returning a type double, but you are calling it as if it was a void function that is not returning a value. You either need to store the returned value in another variable or else use it inside a print statement.

Function call should look like this:
CODE

newValue = area_and_perimeter_of_square( side_square, area_square, perimeter_square );


Or this:
CODE

printf("%.2f", area_and_perimeter_of_square( side_square, area_square, perimeter_square );


User is offlineProfile CardPM
+Quote Post

horace
RE: Function Problem
6 Nov, 2006 - 11:04 PM
Post #7

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
Unlessyou return a structure a function can only return a single value as the function result, e.g. to a double. You could have two seperate functions one for calculating the perimeter and the other for the area.

To return function results via parameters you either use pointers, see
http://www.codeproject.com/cpp/pointers.asp

or in C++ references
http://en.wikipedia.org/wiki/Reference_(C++)

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month