pass by value program

hello there
please need your help with two things ...the first one is about the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//pass by value
#include<iostream>
#include<conio.h>
using namespace std;
double area(double r);
double circum(double r);
const double pi=3.14;
int main()
{ double radius;
cout<<"enter circle radius:";
cin>>radius;
cout<<"area="<<area(radius)<<endl;
cout<<"circumference ="<<circum(radius)<<endl;
}//end function main
//function definitions
double area(double r)//function header
(return pi*r*r;}//end area
double circum(double r)//function header
{return 2*pi*r;}//end area
            while(!kbhit());
            }
    

it says that double is a variable or something like that...

the other question
why cannot i use fact as a function?
i wrote a program that includes the following part of a code
1
2
3
{factorial = fact(n);
cout<<n<<"!is "<<factorial<<endl;
}


i'll appreciate anyhelp from you:)
hello sara,
your code looks like a mess because that its unreadable.. make sure u stick with some formatting... i think u need to look at the basics of C again too...
i dont know about the last two lines of your code ( 20, 21 lines), but i see that you missed a curly barcket "{" at the end of the line 16 or at the begining of the line 17.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//pass by value
#include<iostream>
#include<conio.h>
using namespace std;
double area(double r);
double circum(double r);
const double pi=3.14;
int main()
{
	double radius;
	cout<<"enter circle radius:";
	cin>>radius;
	cout<<"area="<<area(radius)<<endl;
	cout<<"circumference ="<<circum(radius)<<endl;
}//end function main

//function definitions
double area(double r)//function header
{
	(return pi*r*r;
}//end area

double circum(double r)//function header
{
	return 2*pi*r;
}//end area 


i cant understand your second question clearly... what do you mean... if you want to use a method called "fact()" from another header file, u just include it with the directive "#include".

just focus on the mathematical definition of factorials if you want to build it on ur own..
ill give a clue..

1
2
3
factorial 0 is 1 .
factorial 1 is 1 .
when n > 1 , factorial n is n*(n-1) .


so, if you create a function called fact() to return the vale of a factorial when the number is given as the parameter, it should return these values.
e.g.
1
2
3
           i = fact( 0 ) ;           // fact retuns 1 therefore, i = 1
           i = fact( 1 ) ;           // fact retuns 1 therefore, i = 1
           i = fact( 3 ) ;           // fact retuns 6 therefore, i = 1 
thank you for helping me:) lol that hurtsXD its my first code with the format so i guess it was a mass i'm with you:$
the seconed one
i've tried to you the fact function but i couldnt ..i've tried to use it several times didnt work out..that was an example of it..
i dont know why..i'm going to post another example next time..
Thank you again:)
hello sara,
you cannot use a function which is not defined.. can you post the whole code in here... then i can help you..
there is no function pre-defined in C language to find factorials... you have to implement it... its very simple if you use "Recursive Functions", but it takes too much time and memory.. you can implement it using a loop, which is good...
Method 1:
1
2
3
4
5
6
7
8
9
10
11
int factorial( int n )
{
   if (n==0)
   {
       return 1;
   }
   else
   {
        return (  n * factorial(n-1) );
    }
}

Method 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int factorial( int n )
{
   if (n==0)
   {
       return 1;
   }
   else
   {
        int answer = 1;
        int i = n;
        while( i > 1 )
        {
             answer = answer * i ;
             i-- ;
        }
        return answer;
    }
}
Topic archived. No new replies allowed.