Maker Pro
Maker Pro

Function and their types

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You didn't study it very well.

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

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
You didn't study it very well.

Why are you expecting a return value from your function "delay"?
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();
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Please see post #2
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Please see post #2
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 ?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Now you seen to have done it.
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
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
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
I cannot even imagine what it is you are not understanding. A function with no return value does not have a return value.

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.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
i think , when we call delay function from main function then it will return 5 in main function.

Neither, it returns nothing.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Neither, it returns nothing.
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 ?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
No, it will return 5.

The arguments are separate from the return value.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
No, it will return 5.

The arguments are separate from the return value.
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);
^~~~~
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,748
Joined
Nov 17, 2011
Messages
13,748
warning: implicit declaration of function 'count_value' [-Wimplicit-function-declaration]
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.

conflicting types for 'count_value'
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.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
You need to delcare count_value() before calling it to make these errors disappear.
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:

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
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
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
I think I see what your problem is with functions with a return value and no arguments.

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.

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());
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
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
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
You can replace "count" in your printf with "counter()" and it will work correctly.
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:
Top