944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3938
  • C++ RSS
Jan 3rd, 2005
0

Stuck writing a Function

Expand Post »
I have to write a function to find the circumference of a circle. I am having problems cause I dont have any idea how to do that. I know ya'll like to have some kind of showing of my own program the only thing is that I dont understand what to do or where to start. My teacher is no help. I dont like the way he teaches. Maybe someone here can get me to understand a function with an example or something.

My program started
#include<iostream.h>
void my_sum(int r)
{
float sum;
sum = 2*3.14*r;
cout<<"The circumference of a circle is "<<sum<<"."<<endl;
}
int main()
{

Im stuck. I dont think this is even right. I dont know what I am doing. Please someone help.

Kelly
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kellyandtopher is offline Offline
6 posts
since Dec 2004
Jan 3rd, 2005
0

Re: Stuck writing a Function

Try compiling and running the code you are developing.
#include<iostream>
using namespace std;

void my_sum(int r)
{
   float sum;
   sum = 2 * 3.14 * r;
   cout << "The circumference of a circle is " << sum << "." << endl;
}

int main()
{
   my_sum(1);
   return 0;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 3rd, 2005
0

Re: Stuck writing a Function

Quote originally posted by kellyandtopher ...
Maybe someone here can get me to understand a function with an example or something.
your function looks correct...although you should probably change that "int r" to a float.

are you having trouble understanding the concept of functions?

if thats the case then a quick example would be something like this
C++ Syntax (Toggle Plain Text)
  1. int addone(int x)
  2. {
  3. return x+1;
  4. }
  5.  
  6. int main()
  7. {
  8. int z = 5;
  9. z = addone(z);
  10. z = addone(z);
  11. }
the end value of z here would be 7.
hope it helped...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kaiser<lucy> is offline Offline
13 posts
since Nov 2004
Jan 3rd, 2005
0

Re: Stuck writing a Function

Hello, sorry, be right back!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sadako is offline Offline
4 posts
since Jan 2005
Jan 5th, 2005
0

Re: Stuck writing a Function

#include<iostream.h>
float my_sum(int r);
void main()
{
int r;
cout<<"Enter the radius";
cin>>r;
my_sum(r);
}

float my_sum(int r)

{
float sum;
sum = 2*3.14*r;
cout<<"The circumference of a circle is "<<sum<<"."<<endl;
}


this will work.
please let me know if it is fine or not.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tresa is offline Offline
14 posts
since Nov 2004
Jan 5th, 2005
0

Re: Stuck writing a Function

Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Jan 5th, 2005
0

Re: Stuck writing a Function

It looks like you missed the Dave's post...
Why did you switch to void main() ?
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Audio File Matching Program
Next Thread in C++ Forum Timeline: Which components of Visual C++ should i install





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC