Thread: First steps (function isnt printing result of another function.)

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    2

    First steps (function isnt printing result of another function.)

    I dont understand why the line "printf(" o maior e %d e o menor e %d",maior_menor(n,maior,menor));" isnt printing the result of the function "maior_menor" for example, it should give me an output like:

    " o maior e 5 e o menor e 2" but instead it is giving me a bug. Any idea on whats going on?

    How the function works: You input a certain number, "n" that represents the number of numbers that you will input later. Then the function enters a cycle where u will input "n-1" numbers, (one at the time), they are represented by the variable "novo" and for example, if the numbers are 12,3,5,6 then the function should return (12,3) which are the biggest and the smallest number.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int n,maior,menor;
    
    printf("Digite um numero");
    scanf("%d", &n);
    
    maior = n;
    menor = n;
    
    printf(" o maior e %d e o menor e %d",maior_menor(n,maior,menor));
    return 0;
    }
    
    int maior_menor(n,maior,menor) {
    int contador,novo;
    contador = 0;
     
       while (contador <= n-1) {
         
          printf("digite um numero");
          scanf( "%d", &novo);
          if (novo> maior) maior = novo;
          if (novo< menor) menor = novo;
          contador = n+1;
         }     
       return maior, menor;
       }
    Last edited by Muradean; 02-23-2016 at 11:19 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    For starters, you need to indent your code properly:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n,maior,menor;
    
        printf("Digite um numero");
        scanf("%d", &n);
    
        maior = n;
        menor = n;
    
        printf(" o maior e %d e o menor e %d",maior_menor(n,maior,menor));
        return 0;
    }
    
    int maior_menor(n,maior,menor)
    {
        int contador,novo;
        contador = 0;
    
        while (contador <= n-1)
        {
            printf("digite um numero");
            scanf( "%d", &novo);
            if (novo> maior) maior = novo;
            if (novo< menor) menor = novo;
            contador = n+1;
        }
        return maior, menor;
    }
    Then, you need to compile at a high warning level and pay attention to the warnings:
    Code:
    test.c: In function ‘main’:
    test.c:13:5: warning: implicit declaration of function ‘maior_menor’ [-Wimplicit-function-declaration]
         printf(" o maior e %d e o menor e %d",maior_menor(n,maior,menor));
         ^
    test.c:13:5: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
    test.c: In function ‘maior_menor’:
    test.c:17:5: warning: type of ‘n’ defaults to ‘int’ [enabled by default]
     int maior_menor(n,maior,menor)
         ^
    test.c:17:5: warning: type of ‘maior’ defaults to ‘int’ [enabled by default]
    test.c:17:5: warning: type of ‘menor’ defaults to ‘int’ [enabled by default]
    test.c:30:17: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         return maior, menor;
                     ^
    What the warnings are telling you:
    • You forgot to forward declare the function maior_menor before calling it in the main function. ("implicit declaration of function ‘maior_menor’")
    • You have two %d format specifiers in the printf format string, but only one corresponding argument, i.e., the return value of the call to maior_menor. ("format ‘%d’ expects a matching ‘int’ argument")
    • You forgot to declare the types of the parameters of maior_menor. ("type of ‘n’ defaults to ‘int’")
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The last warning posted by laserlight is hinting that return maior, menor; isn't doing what you expect. You can only return a single value from a function.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    2
    Oh thanks! I think i got the first one, before typing the function main() i need to type first int maior_menor(n,maior,menor); so i can declare it first, is that right? but i dont understand the second warning.

    How can i print the last sentence then? For example, i wanted the function "maior_menor" to return (x,y) and the final output should be "O maior e x e o menor e y" so... i know that i have two "%d" but each one of them would be occupied respectively by the "x" and the "y".

    Im also not sure i got the third warning, i declared n at the beginning as an int, do i need to declare it again as a int in the function maior_menor? (Oh sorry, i just read what u said Matticus, so in C all functions always return one value?)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by Muradean
    I think i got the first one, before typing the function main() i need to type first int maior_menor(n,maior,menor); so i can declare it first, is that right?
    Close, but not quite. A proper forward declaration of a function prototype would look like this:
    Code:
    int maior_menor(int n, int maior, int menor);
    Notice that the return type and the types of each of the parameters are specified.

    Quote Originally Posted by Muradean
    How can i print the last sentence then? For example, i wanted the function "maior_menor" to return (x,y) and the final output should be "O maior e x e o menor e y" so... i know that i have two "%d" but each one of them would be occupied respectively by the "x" and the "y".
    Basically, you want to return two values from maior_menor, but as Matticus noted, this cannot be done. However, you have a few options, some of which are:
    • Break up maior_menor into two functions, each returning one value. A function should do one thing and do it well, so having separate responsibilities can be a Good Thing. The bad part is that sometimes combining responsibilities can be more efficient, so this may be inferior in that respect, but if it doesn't matter, then why not?
    • Use output parameters (also known as out parameters). This means that maior_menor would have pointer parameters whose sole purpose is to be written to. The caller (i.e., the main function) can then pass pointers to variables when calling the function, and after the function call use those variables.
    • Return a struct. The struct is collectively a single value, but it can have two members.


    Quote Originally Posted by Muradean
    Im also not sure i got the third warning, i declared n at the beginning as an int, do i need to declare it again as a int in the function maior_menor?
    The local variable named n in the main function and the parameter named n in maior_menor are two different variables.
    Last edited by laserlight; 02-23-2016 at 11:51 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-31-2011, 11:06 AM
  2. Function result as a string
    By nowber in forum C Programming
    Replies: 6
    Last Post: 10-28-2009, 05:40 AM
  3. Help understanding result of Function
    By deadhippo in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 02:56 AM
  4. problem getting result when calling a function
    By drb2k2 in forum Windows Programming
    Replies: 1
    Last Post: 04-14-2003, 09:51 AM
  5. Returning result of an external function
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-21-2001, 06:39 PM