User defined power function?

Mar 26, 2015 at 2:36pm
Hi,

I have coded this program.

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
27
  #include <iostream>
#include <math.h>
using namespace std;

int main()
{
	double x=0, y=0, v=0;

	cout<<"Enter base: ";
	cin>>x;
	cout<<"Enter exponent: ";
	cin>>y;

	while (x<999 && y<999)
	{
	v=pow(x,y);
	
	cout<<"Value: "<<v<<endl;
	
	cout<<"Enter base: ";
	cin>>x;
	cout<<"Enter exponent: ";
	cin>>y;
	}
	
	return 0;
}


Instead of using v=pow(x,y), I want to use v=n(x,y). How could I get that?
Mar 26, 2015 at 2:43pm
I have no idea what you're actually asking, could you give examples?

Do you mean you want to create a function called "n". That returns pow(x,y) which is then stored in the variable v?
Last edited on Mar 26, 2015 at 2:45pm
Mar 26, 2015 at 3:00pm
Exactly! Instead of using pow(base, exponent). I want to create my own function with any variable. Like: a(b,c) or anything else.
Mar 26, 2015 at 3:02pm
Then do just that. Create a function, of type double/float.

It takes two integer as parameter.

And it returns the pow of them.

Then you call for it v = thatFunction(x,y)

and give it the two integers that it will work with.

Get started on this, and I'll help you out if you get stuck.

Edit: There are lots of answers on google, and videos on youtube explaining all of this :) Use it to your advantage.
Last edited on Mar 26, 2015 at 3:12pm
Mar 26, 2015 at 3:14pm
Can you just give me some hints on how to start because I don't know how to create a function?
Mar 26, 2015 at 3:18pm
https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

Video 9-11 is about functions. You specifically want the one with multiple parameters.

(You can just google or youtube how to create functions man, questions like this are useless and are easily accessed on the internet)
Last edited on Mar 26, 2015 at 3:19pm
Mar 26, 2015 at 3:22pm
Do you want to allow the exponent to be any floating point numbers or just integers? The first is quite complicated, the latter is much easier.
Mar 31, 2015 at 3:37pm
Okay!

@Tarik: I have watched all these videos, but I couldn't understand anything. Maybe I am dumb!
Can you please help me with this program?

@Peter: Exponent should be integer, but I have no idea of how to do.
Mar 31, 2015 at 4:33pm
Well, to get you started with making the function:

1
2
3
4
int n(int x, int y)
{

}


So to break this down, you declare the return type of a function first before you name it (so in this case, int is the return type). Then you name the function after that (n is the name of the function, but you can change to whatever suits your boat).

Finally, you declare any parameters you want the function to take in. Let's start with understanding parameters: a parameter can be any type of variable (an int, float, or whatever) that you want a function to take in and use with itself. So, in this function I have declared two parameters (an int named 'x', and an int named 'y' (these names can be whatever you want)).

Now, you will need to do the math inside the function setting up a variable to store the value of 'x'. From there, you should multiply that variable against itself 'y' times. A simple way to do this involves using a for loop and setting the variable using '*=' which means it will equal itself * another value.

And that is pretty much all there is to it!
Topic archived. No new replies allowed.