1. ## Function

Guys, see that code:
Code:
```int main ()
{
int x;
int resultado = 0;
cout << "Coloque um numero a ser fatorado e descubra se ele eh primo: " << endl <<"(Quanto maior o numero, mais demorado o cauculo.)" << endl;
cin>>x;
unsigned long int primo = x;
while (primo>0){
if ((x%primo == 0) && (primo != x) && (primo != 1)){
cout << "Eh divisivel por " << primo << "    (" << x << " dividido por " << primo << " eh iguau a " << x/primo << ")" << endl;
}
primo = primo-1;
}
cin.get();
if (resultado == 1)
cout << "Logo, esta bunda nao eh primo!";
else
cout << "Ele eh primo! Viva Deus!";
cin.get();
}```
Well, i want to modify the program to do EXACTLY the same thing, but the factoration thing in a separated function.
How do i do that? Thanks.

2. Take the code that you want to put into a separate function and move it outside main. Then put the opening and closing brace around the code. Then look for variables that you use within that code. For each variable, if it holds a value from outside the code you copied, then you must pass that value in as a parameter. If it does not use any value from outside the function code, then you can make it a local variable. If there are any values that are required back in main that are determined within the function code, then you have to return that value.

Do this and play around with what code needs to be in the function and what needs to stay in main, and you'll get what you want.

Popular pages Recent additions