Arguments in a function

Page 1 of 1

10 Replies - 3353 Views - Last Post: 02 December 2012 - 10:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302210&amp;s=aeeeb0fe885f97e9abf705e76214ab08&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 SalGiki

Reputation: 0
• Posts: 2
• Joined: 30-November 12

Arguments in a function

Posted 30 November 2012 - 11:50 AM

My problem is with the "default argument of 2 for p" part of this question:

Here's my code:
```#include<iostream>
using namespace std;
double power(double, int);
int main(){
double n;
int p;
double ans;
cout<<"Enter the number first and then the power to which it is to be raised: \n";
cin>>n>>p;
ans = power(n,p);
cout<<n<<" raised to the power "<<p<< " is "<<ans<<endl;
system("pause");
return 0;
}
double power(double n, int p)
{
double total = 1;
for(int i=1;i<=p;i++)
{
total = total*n;}
if(p<0)
{return n*n;}
else
}
```

The code works fine and gives the correct results but I have no idea how the value of p can be omitted by the user? I mean that is there any way in which the user can NOT enter the value of p with the code that I've made? Also, how should I modify the "if" part in my function so that my code is the correct answer to the question that I have posted?

Any help would be appreciated.

Is This A Good Question/Topic? 0

Replies To: Arguments in a function

#2 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

Re: Arguments in a function

Posted 30 November 2012 - 12:33 PM

In your function prototype, you can place a default argument to be used if the user doesn't input that argument.
For example:
```double power(double, int = 2);
```

Now 2 will be used as a default argument if one is not provided by the user. But keep in mind that omitted arguments must be on the end of the list or the compiler won't be able to tell which arguments were left out.

This post has been edited by Mrk: 30 November 2012 - 12:34 PM

#3 blackcompe

• D.I.C Lover

Reputation: 1159
• Posts: 2,547
• Joined: 05-May 05

Re: Arguments in a function

Posted 30 November 2012 - 12:34 PM

Have you searched for "C++ default function arguments"? I'm sure you'll find plenty of sources that will answer your question.

#4 AKMafia001

• </code.in.dream>

Reputation: 238
• Posts: 738
• Joined: 11-June 11

Re: Arguments in a function

Posted 30 November 2012 - 12:39 PM

Well! There could be many ways to do that. The first you might come up with is that ask the user whether he/she is going to enter the value for p and put on some conditions...

It would be really up to what you are intended to do. BTW, your whole program is using the value of p, why omit it then?

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...

And for the function, you can use a default parameter and check the default value in the body which would mean that the value is not passed. Or overload the function...

Hope this Helps!

#5 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

Re: Arguments in a function

Posted 30 November 2012 - 05:27 PM

```double power(double n, int p)
{
double total = 1;
for(int i=1;i<=p;i++)
{
total = total*n;}
if(p<0)
{return n*n;}
else
}

```

Lets take a look at your logic here. What would the result of the following function call

power(2, -2)

By your algorithm, the if statement condition p < 0 would evaluate to true, and therefore you would return 2 * 2 = 4...... 4 does not equal 2 ^ -2.

Here are three basic "properties" that you can use for your power function

n ^ 0 = 1
n ^ -p = 1 / (n ^ p)

#6 SalGiki

Reputation: 0
• Posts: 2
• Joined: 30-November 12

Re: Arguments in a function

Posted 01 December 2012 - 09:04 PM

Mrk, on 30 November 2012 - 12:33 PM, said:

In your function prototype, you can place a default argument to be used if the user doesn't input that argument.
For example:
```double power(double, int = 2);
```

Now 2 will be used as a default argument if one is not provided by the user. But keep in mind that omitted arguments must be on the end of the list or the compiler won't be able to tell which arguments were left out.

Got that, but how can I make user not enter the value of p? In my main function, I have put
```cin>>p
```
. So isn't it mandatory for user to give a value of p?

blackcompe, on 30 November 2012 - 12:34 PM, said:

Have you searched for "C++ default function arguments"? I'm sure you'll find plenty of sources that will answer your question.

Sorry, searched for it but couldn't find any helpful topics.

AKMafia001, on 30 November 2012 - 12:39 PM, said:

Well! There could be many ways to do that. The first you might come up with is that ask the user whether he/she is going to enter the value for p and put on some conditions...

It would be really up to what you are intended to do. BTW, your whole program is using the value of p, why omit it then?

That is the problem. How can I have the user NOT enter the value of p?

AKMafia001, on 30 November 2012 - 12:39 PM, said:

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...

Haven't done 'strings' yet in my CS class.

AKMafia001, on 30 November 2012 - 12:39 PM, said:

And for the function, you can use a default parameter and check the default value in the body which would mean that the value is not passed. Or overload the function...

Hope this Helps!

For default parameter, do you mean doing this to the prototype?:
```double power(double, int = 2);
```

As for overloading, I have been taught that overloading is to create the functions with the same names as original one but with different type of variables. Please give me any hints on how can that help me to make user OMIT the value?

jjl, on 30 November 2012 - 05:27 PM, said:

```double power(double n, int p)
{
double total = 1;
for(int i=1;i<=p;i++)
{
total = total*n;}
if(p<0)
{return n*n;}
else
}

```

Lets take a look at your logic here. What would the result of the following function call

power(2, -2)

By your algorithm, the if statement condition p < 0 would evaluate to true, and therefore you would return 2 * 2 = 4...... 4 does not equal 2 ^ -2.

Here are three basic "properties" that you can use for your power function

n ^ 0 = 1
n ^ -p = 1 / (n ^ p)

I just put p<0 so that the program could run correctly for +ve values of p. I have removed it now.

My modified code(with the prototype modified):
```#include<iostream>
using namespace std;
double power(double, int p =2);
int main(){
double n;
int p;
double ans;
cout<<"Enter the number first and then the power to which it is to be raised: \n";
cin>>n>>p;
ans = power(n,p);
cout<<n<<" raised to the power "<<p<< " is "<<ans<<endl;
system("pause");
return 0;
}
double power(double n, int p)
{
double total = 1;
for(int i=1;i<=p;i++)
{
total = total*n;}
}

```

#7 AKMafia001

• </code.in.dream>

Reputation: 238
• Posts: 738
• Joined: 11-June 11

Re: Arguments in a function

Posted 02 December 2012 - 03:14 AM

Not just with the prototype, you need your declaration and implementation to match... Also, remember that default parameters should be at the end of the parameter list...

So, you can call your functions in two ways:
```power(2, 3);   // provide both arguments
power(2);      // leave out the default parameter, the default value will be considered

```

The question is, why you want the exponent to be optional input? Is it that you don't need to calculate for negative exponent value?

Well, do you need something like:
```cin >> p;

if(p < 0)
power(n);
else
power(n, p);

//...
// power function
double power(double n, int p = 0) {
if(p < 0)
return 0.0;
else if(p == 0)
return 1.0;
else
// calculate power
}

```

Well! You need to be more clear of why you want the power to be optional...

#8 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

Re: Arguments in a function

Posted 02 December 2012 - 09:43 AM

I think the OP wants the end-user of the application to be able to omit entering any value for p.
You could do that like this:
```std::string line;
std::getline(std::cin, line);
//possibly trim the string (remove whitespace)
if (line.empty()) { // user jsut pressed enter
// handle it
} else {
// ... use stringstream to get things out of the "line"?
}
```

Of course if you used std::cin::operator>> before std::getline(), you may want to clear the std::cin's buffer (using std::istream::ignore()).

This post has been edited by Xupicor: 02 December 2012 - 09:45 AM

#9 AKMafia001

• </code.in.dream>

Reputation: 238
• Posts: 738
• Joined: 11-June 11

Re: Arguments in a function

Posted 02 December 2012 - 09:50 AM

@Xupicor

AKMafia001, on 01 December 2012 - 12:39 AM, said:

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...

SalGiki, on 02 December 2012 - 09:04 AM, said:

Haven't done 'strings' yet in my CS class.

#10 jimblumberg

Reputation: 5444
• Posts: 16,968
• Joined: 25-December 09

Re: Arguments in a function

Posted 02 December 2012 - 10:01 AM

Then use getline() using a C-string then check the length of this string. If it is greater than 0, use atoi() to convert this string to a number and call the function with both parameters, otherwise call the function with one argument. You will need to include the <cstring> header to use the strlen() function and <cstdlib> to use the atoi() function. And be sure to ignore() the end of line character left in the input buffer by the preceding cin.

Jim

This post has been edited by jimblumberg: 02 December 2012 - 10:03 AM

#11 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

Re: Arguments in a function

Posted 02 December 2012 - 10:06 AM

@AKMafia001
First quote - two very different things we're talking about. You - emulating default values of function parameters using a string parameter which would be then parsed; me - letting the end-user (not the programmer) skip some values on input (i.e. when interacting with the application).

As for the "I haven't learn that yet, therefore I can't use it", I already voiced my opinion on that matter I believe, at least once.
I'd just learn about them whether they are or not to be on the course, and then make two versions of the program. One using strings, and one using character arrays, just in case the teacher is not happy about using fancy things not covered yet.

This post has been edited by Xupicor: 02 December 2012 - 10:08 AM