# Function problems

Page 1 of 1

## 3 Replies - 870 Views - Last Post: 11 November 2010 - 03:49 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=198980&amp;s=ad6dfd6e4b6109d3170a31f0201ac4d5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sion100

Reputation: 0
• Posts: 1
• Joined: 08-November 10

# Function problems

Posted 08 November 2010 - 08:09 AM

Folks,

I could do with a little help please

I have an assignment due and have had no time to study properly due to work commitments and was hoping one of you good people could point me in the right direction. Below is the code I have come up with to (or not as the case may be) to enter 3 numbers pass to a function then return the smallest number. I have the numbers entering ok and I think the sorting is good but will bow to superior knowledge on that one.
I am strugling and any help would be great

```//smallestnumber

#include<iostream>
#include<cstdlib>
using namespace std;
void smallestNumber(int,int&,int&);//declaring the function

int main()
{//To enter the numbers
int num1,num2,num3;
cout<<"\n Insert a number "<<endl;
cin>> num1;
cout<<"\n Insert another number  "<<endl;
cin>>num2;
cout<<"\n Insert another number  "<<endl;
cin>>num3;

smallestNumber (num1,num2,num3);// calling the function
{
for(num1=0;num1<2;num1++);
for(num2=num1+1;num2<3;num2++)
if(num1>num2);
}

cout<< "\n The smallest number is "<< num1 <<endl;

system("pause");
return 0;
}//end main
//defining the function
```

Is This A Good Question/Topic? 0

## Replies To: Function problems

### #2 newclearner

• D.I.C Regular

Reputation: 103
• Posts: 302
• Joined: 29-September 10

## Re: Function problems

Posted 08 November 2010 - 08:25 AM

Did you even compile this code even once?

1. Your cdoe is badle messed up. You have defined your function inside the main function, which is not correct, seperate it out. Write is outside main function and the call it from within function main(). This is how you call a function with return type int.
```main()
{
...//rest of the statments of your program
int x = smallestNumber(a,b,c);
....//rest of the statments of your program
}
```

int x is to recive the value returned by the function then you can simply print it.

2. In your function declaration what is & sign doing. You can simply pass them by value rather than by reference.
```smallestNumber(int,int[s]&[/s],int[s]&[/s]);
```

3. You function should return something to the calling function, don't you think so. Otherwise how would the calling function know what value was the smallest number identified by your smallesNumber function. so its return type would be int rather than void.

4. Can you please explain in words what, in your opinion, these statements are doing? Your logic is completely incorrect. Work on it again.
```for(num1=0;num1<2;num1++);
for(num2=num1+1;num2<3;num2++)
if(num1>num2);
```

5. A trivia: Never use sytem("pause"). It is considered to be a bad programming habbit. Check out my signatures to ind out why and to know the alternative for this.

Fix your code and post it here if you still have any problems.

This post has been edited by newclearner: 08 November 2010 - 08:30 AM

### #3 newclearner

• D.I.C Regular

Reputation: 103
• Posts: 302
• Joined: 29-September 10

## Re: Function problems

Posted 11 November 2010 - 03:37 AM

another dump and run case. ::

### #4 Anarion

• The Persian Coder

Reputation: 387
• Posts: 1,663
• Joined: 16-May 09

## Re: Function problems

Posted 11 November 2010 - 03:49 AM

newclearner, on 11 November 2010 - 01:07 PM, said:

another dump and run case. ::

Doesn't matter, posts are helpful for many here, not just the op