How to add functions in C programs?

Asked

Viewed 266 times

-1

I need to know how to introduce functions into any program. If someone can give me an example in this program here, maybe I can apply it to others as well. I need to understand the logic of how to apply the functions.

#include <stdio.h>
#include <stdlib.h>

int main(){
  int n, some, divisor;

  printf("Digite um numero inteiro positivo: ");
  scanf("%d", &n);

  soma = 0;

  for (divisor = 1; divisor < n; divisor++){
    if ( n % divisor == 0 )
      soma = soma = + divisor;
  }

  if ( n == soma )
    printf("O numero %d e perfeito \n", n);
  else
    printf("o numero %d nao e perfeito \n", n);

  return 0;
}
  • You want to make the sum through a function is that ? It’s not very clear what you’re looking for.

  • What functions do you refer to? Something like int soma(int x, int y){ return x+y; } ?

  • 1

    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.

  • Tip: Your program already declares a function (main) and use two other (printf and scanf).

  • I believe a Google search solve in 1 minute

2 answers

1

I think your goal is this:

int main(){
   int n;
   printf("Digite um numero inteiro positivo: ");
   scanf("%d",&n);

   // AQUI SE CHAMA A FUNCAO E MANDAM OS PARAMETROS NECESSARIOS NESTE CASO É 
   //SÓ UMA VARIAVEL   
   if(CheckIfPerfect(n)){
      printf("O numero %d e perfeito \n". n);
   }else{
      printf("O numero %d nao e perfeito \n",n);

   }

int CheckIfPerfect(int n){
   int soma = 0;
   int divisor;

   for(divisor = 1;divisor<n; divisor++){
      if(n % divisor == 0){
         soma+=divisor;
      }
   }
   if(n == soma){
      //DEVOLVE 0 EQUIVALE A TRUE  
      return 0;
   }else{
      //DEVOLVE 1,2,3.... EQUIVALE A false  
      return 1;
   }

}
  • 6

    It seems that the code meets what was requested, but it would be nice to click on [Edit] and explain what was done in the code, so that it understands the characteristics of a C function, valuing the teaching for the author of the question and future visitors

  • 2

    I agree with what @Bacco said. But in addition there are several errors with your code, please consider a fix.

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.