From your program, you can make a function separate and out of main, that makes this calculation. The benefit of the functions, is that you can call them from anywhere in the program and will often save lines. For example, if for some reason you wanted to do the same calculation at the end of main, you would have to repeat the whole of it. If you make a function that does this calculation, just call it.
The prototype of a function is as follows:
<tipo> <nome_da_função>(<parâmetros>)
{
código;
...;
}
The type of the function defines the type that will be returned by the function. In the function to perform its calculation, the result of this, ie the variable soma
, will be an integer number, so the value returned will be an integer number, so the type is the int
.
The function name is worth the same rules for naming a variable, nothing new around here.
The parameter of a function is what you need to perform your calculation or what you need passed to your function to do anything within it. For you to do yours for
you need the element the user entered (the n
), because he’s part of for
. Therefore, it is a parameter for your function.
Observation: Note that the parameters are variables, and should be declared as variables as well.
So far, we know the type and parameters of the function, so we can create it.
int divisor(int elem)
{
int divisor = 0, soma = 0;
for (divisor = 1; divisor < elem; divisor++)
if ( elem % divisor == 0 )
soma = soma = + divisor;
return soma;
}
Note that the parameter does not necessarily have the same name as the variable you will pass, in other words: the name of the variable within the function may be different from the variable in the main.
Function created, just call her in main to make the necessary calculation.
Upshot:
#include <stdio.h>
#include <stdlib.h>
int divisor(int elem)
{
int divisor = 0, soma = 0;
for (divisor = 1; divisor < elem; divisor++)
if ( elem % divisor == 0 )
soma = soma = + divisor;
return soma;
}
int main()
{
int n, resultado;
printf("Digite um numero inteiro positivo: ");
scanf("%d", &n);
/*A função divisor retornará o resultado do cálculo, logo posso
igualá-la a uma variável (Essa variável será igual ao resultado
da conta) */
resultado = divisor(n);
if (resultado == n)
printf("O numero %d e perfeito \n", n);
else
printf("o numero %d nao e perfeito \n", n);
return 0;
}
If you have not understood something, ask here or look on the internet, there is a lot of material about it, otherwise it is just custom, I also did not understand, but if you use/see this a lot, you will get used and learn to use.
You want to make the sum through a function is that ? It’s not very clear what you’re looking for.
– Isac
What functions do you refer to? Something like
int soma(int x, int y){ return x+y; }
?– lazyFox
Allow me to refract. What functions do you want to build ? What logic is supposed to be done by which functions ? Start by clarifying these points first.
– Isac
Tip: Your program already declares a function (
main
) and use two other (printf
andscanf
).– bfavaretto
I believe a Google search solve in 1 minute
– Sveen