Connect with us

Function and their types

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jan 21, 2018.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    I have read it about function and their types in c programming. so after reading I tried to write program for function with no argument and no return type

    This is my program ( void function_name (void)
    Code:
    #include<stdio.h>
    
    void delay (void);
    
    int main (void)
    {
      int number;
      number = delay();
     
      printf(" Print number : %d",number);
     
       return 0;
    }
    
    int delay (void)
    {
        unsigned int i;
        for (i = 0; i < 6; i++);
           {
               }     
    }
    error: void value not ignored as it ought to be
    number = delay();

    I don't know weather my example is suitable for this reason but I thought it would be best so I just wrote my program. But when I compiled program it's showing error. What's the wrong my idea or program
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    Check the syntax of the for statement.

    Also, if you have a function that doesn't return a value, why are you trying to assign a variable the return from it?

    What do you expect it to do?
     
  3. vead

    vead

    473
    14
    Nov 27, 2011
    I studied about Function with no argument and no Return value and then tried to make my own program
    I want to do like this example https://www.tutorialgateway.org/types-of-functions-in-c/
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    You didn't study it very well.

    Why are you expecting a return value from your function "delay"?
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    right I have done mistake i posted wrong program
    Code:
    #include<stdio.h>
    
    void delay (void);
    
    int main (void)
    {
      delay();
     
       return 0;
    }
    
    void delay (void)
    {
        unsigned int i;
        for (i = 0; i < 6; i++);
           {
               }    
    }
    
    I was trying to assign value of delay function to another variable and then wanted to know the the value of variable like this . but when I do, this I get error
    Code:
    int main (void)
    {
      int number;
      number = delay();
     
      printf(" Print number : %d",number);
     
       return 0;
    }
    
    error: void value not ignored as it ought to be
    number = delay();
     
  6. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    Please see post #2
     
  7. vead

    vead

    473
    14
    Nov 27, 2011
    Function with no argument and no Return value
    Code:
    #include<stdio.h>
    
    void delay (void)
    {
        unsigned int i;
        for (i = 0; i < 3 ; i++)
           {
                printf(" Print number : %d \n ",i);
             }     
    }
    int main (void)
    {
       delay();
       return 0;
    }
    
    Print number : 0
    Print number : 1
    Print number : 2

    Function with argument and no Return value

    Code:
    #include<stdio.h>
    
    void delay (unsigned int number)
    {
        unsigned int i;
        for (i = 0; i < number; i++)
           {
                printf(" Print number : %d \n ",i);
             }     
    }
    int main (void)
    {
       delay(5);
       return 0;
    }
    
    Print number : 0
    Print number : 1
    Print number : 2
    Print number : 3
    Print number : 4

    How to write program with no argument and Return value ?
    How to write program with argument and Return value ?
     
  8. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    Now you seen to have done it.
     
  9. BobK

    BobK

    7,645
    1,662
    Jan 5, 2010
    I cannot even imagine what it is you are not understanding. A function with no return value does not have a return value. So we call it like this:

    void delay();
    ...
    delay();

    If it had a return value, we could assign to to a variable of the same type:

    int delay();
    ...
    int x;
    x = delay();

    You last example did both of these correctly, so what is it you do not understand?

    Bob
     
  10. vead

    vead

    473
    14
    Nov 27, 2011
    Function with return value but no argument

    Code:
    #include<stdio.h>
    
    int delay (void)
    {
        unsigned int i;
        for (i = 0; i < 5; i++)
           {
                printf(" Print number : %d \n ",i);
             }  
         
             return i;
    }
    int main (void)
    {
       int x;
       x = delay();
       printf(" Print delay : %d \n ",x);
       return 0;
    }
    Print number : 0
    Print number : 1
    Print number : 2
    Print number : 3
    Print number : 4

    Print delay : 5

    Look at following program
    Code:
    #include<stdio.h>
    
    int delay (void)
    {
        unsigned int i;
        for (i = 0; i < 5; i++)
           {
                printf(" Print number : %d \n ",i);
             }  
         
             return i;
    }
    int main (void)
    {
     
       delay();
     
       return 0;
    }
    what happen when main function call to delay function, does it return numbers from o to 4 in main function or it return number 5 in main function?

    i think , when we call delay function from main function then it will return 5 in main function.
     
  11. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    Neither, it returns nothing.
     
  12. vead

    vead

    473
    14
    Nov 27, 2011
    I am confuse, can we say that below function return only integer value ( 0,1,2,3,4)
    Code:
    int delay (void)
    {
        unsigned int i;
        for (i = 0; i < 5; i++)
           {
                printf(" Print number : %d \n ",i);
             }
      
             return i;
    }
    so when this function execute it return only integer value. it does not pass any argument , so when we call it from main function it doesn't return anything right ?
     
  13. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,210
    2,695
    Jan 21, 2010
    No, it will return 5.

    The arguments are separate from the return value.
     
  14. vead

    vead

    473
    14
    Nov 27, 2011
    so does below program return only number 4

    Code:
    int delay (void)
    {
        unsigned int i;
        for (i = 0; i < 4; i++)
           {
                printf(" Print number : %d \n ",i);
             }
     
             return i;
    }
    Look at another example. i tried to make program function with return value but no argument
    Code:
    #include<stdio.h>
    int main(void)
    {
        count_value();
       
        return 0;
    }
    
    unsigned int count_value (void)
    {
      unsigned int value = 2;
      if ( value == 2)
        {
          print (" return value %d ",value);
           return 2;
        }    
     
      else
       {
          print (" return value %d ",value);
           return 1;
       }
           
    }
    It show some errors


    warning: implicit declaration of function 'count_value' [-Wimplicit-function-declaration]
    count_value();
    ^~~~~~~~~~~
    hello.c: At top level:
    hello.c:9:14: error: conflicting types for 'count_value'
    unsigned int count_value (void)
    ^~~~~~~~~~~
    hello.c:4:2: note: previous implicit declaration of 'count_value' was here
    count_value();
    ^~~~~~~~~~~
    hello.c: In function 'count_value':
    hello.c:14:4: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]
    print (" return value %d ",value);
    ^~~~~
     
  15. Harald Kapp

    Harald Kapp Moderator Moderator

    9,395
    1,919
    Nov 17, 2011
    When you call count_value within main it has not yet been declared. So the compiler doesn't know this function. Compare with teh code on your post #10, where you declare delay() before calling it within main.
    If you want to write the function after the calling code, you need to first declare the function to make it known to the compiler.

    This is a consecutive error due to the previous one. As the function declaration for count_value was missing when the compiler first encountered the call to count_value within main, the compiler assigned an implicit return type "int" to the function. When it later encounters the code for count_value, the definition as "unsigned int count_value (void)" conflicts with the implicit type int.

    You need to delcare count_value() before calling it to make these errors disappear.
     
  16. vead

    vead

    473
    14
    Nov 27, 2011
    Thanks. Does this program make any sense or it could be better
    Code:
    #include<stdio.h>
    unsigned int count_value(void);
    int main(void)
    {
        count_value();
     
        return 0;
    }
    
    unsigned int count_value (void)
    {
      unsigned int value = 1;
      if ( value == 2)
        {
          printf (" return value %d ",value);
           return value;
        }  
     
      else
       {
          printf (" return value %d ",value);
           return value;
       }
         
    }
    return value 1

    Function with return value and argument
    Code:
    #include<stdio.h>
    
    int delay (unsigned int number);
    
    int main (void)
    {
       delay(4);
       return 0;
    }
    
    int delay (unsigned int number)
    {
        unsigned int i;
      
        for (i = 0; i < number; i++)
           {
                printf(" Print number : %d \n ",i);
           }
      
      
          return i;
    }
    
    Print number : 0
    Print number : 1
    Print number : 2
    Print number : 3

    if you see my I tried to write my own program's
    1. Function with no return value and no argument
    2. Function with no return value and argument
    3. Function with return value and no argument
    4. Function with return value and argument
    I hope my all four program's are correct.

    I just want to make sure my all program's are correct, are they correct ?
     
    Last edited: Jan 22, 2018
  17. BobK

    BobK

    7,645
    1,662
    Jan 5, 2010
    I think I see what your problem is with functions with a return value and no arguments. You are trying to make it return something different each time it is called, but not succeeding. Here it how it can be done:

    int count = 0;

    Code:
    int counter()
    {
        count++;
        return count;
    }
    
    int main()
    {
        while (1)
        {
            printf("%d\n", counter());
        }
    }
    
    output:

    1
    2
    3
    4
    ...

    The trick is that the function "counter()" has no arguments, but it uses a global variable, declared outside the function to decide what to return.

    Bob
     
  18. vead

    vead

    473
    14
    Nov 27, 2011
    I compiled your program

    Code:
    #include<stdio.h>
    
    int count = 0;
    
    int counter(void)
    {
        count++;
        return count;
    }
    
    int main(void)
    {
        while (1)
        {
            printf(" count %d \n", count);
        }
    }
      
    result :The system cannot execute the specified program.

    I think we only print the value of variable not the function, that's why I wrote printf(" count %d \n", count); in place of your statement printf("%d\n", counter());
     
  19. BobK

    BobK

    7,645
    1,662
    Jan 5, 2010
    You can have any expression as an argument in the printf statement.

    You can replace "count" in your printf with "counter()" and it will work correctly.

    The way you have changed it, the function is not even being called.

    Bob
     
  20. vead

    vead

    473
    14
    Nov 27, 2011
    Sorry but it's not working on winGw. what's wrong
    Code:
    #include<stdio.h>
    
    int count = 0;
    
    int counter(void)
    {
        count++;
        return count;
    }
    
    int main(void)
    {
        counter();
     
       while (1)
        {
            printf(" count %d \n", counter());
        }
        return 0;
    }
      
    The system cannot execute the specified program
     
    Last edited: Jan 22, 2018
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-