11 Replies - 328 Views - Last Post: 01 January 2013 - 07:25 AM Rate Topic: -----

#1 Sam_Iam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-December 12

function should return no values

Posted 31 December 2012 - 10:07 PM

Could some one help understand how can I make a function return no value::


#include<stdio.h>
int main()
{
	[color="#0000FF"]int[/color] i =12;
	printf(" value is %d\n, i++");
	printf(" value is %d\n, i++");
	printf(" value is %d\n, i++");
	[color="#0000FF"]return[/color] 0;

}

This post has been edited by jimblumberg: 01 January 2013 - 05:03 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Is This A Good Question/Topic? 0
  • +

Replies To: function should return no values

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: function should return no values

Posted 31 December 2012 - 10:17 PM

Declare it with a void return type.

void foo();

void foo(){

}



Also, we have code tags to handle syntax highlighting: :code:.
Was This Post Helpful? 0
  • +
  • -

#3 Sam_Iam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-December 12

Re: function should return no values

Posted 31 December 2012 - 11:55 PM

you mean like this :::



#include<stdio.h>
int main()
{
	void foo();
	int i =12;
	printf(" value is %d\n, i++");
	printf(" value is %d\n, i++");
	printf(" value is %d\n, i++");
	return 0;

}

This post has been edited by jimblumberg: 01 January 2013 - 05:04 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: function should return no values

Posted 31 December 2012 - 11:57 PM

No. You cannot declare function prototypes inside other functions. Check out JackOfAllTrades' tutorial on functions.

Also, please start using code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

#5 FrostHeart07  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 13
  • Joined: 25-December 12

Re: function should return no values

Posted 01 January 2013 - 04:22 AM

You can use void main() instead of int main(), like this:

#include<stdio.h>
void main()
{
    int i =12;
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");

} 


Also, please practice proper use of indention so people can easily read your codes specially if it's long. :) Usually it's four spaces after the outer braces, like what I've done above.
Was This Post Helpful? -4
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: function should return no values

Posted 01 January 2013 - 05:02 AM

No void main() is incorrect. The C and C++ standard require that main must be defined to return an int and you should return an int from this function, except in very rare cases.
int main()
{
   return(0);
}


Jim
Was This Post Helpful? 0
  • +
  • -

#7 FrostHeart07  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 13
  • Joined: 25-December 12

Re: function should return no values

Posted 01 January 2013 - 06:33 AM

View Postjimblumberg, on 01 January 2013 - 08:02 PM, said:

No void main() is incorrect. The C and C++ standard require that main must be defined to return an int and you should return an int from this function, except in very rare cases.

Jim


Ah... yes. I've almost forgotten about that. Thank you for reminding :). The bad side effects of using turbo C as our first compiler during my first sem of college ugh...
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2018
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: function should return no values

Posted 01 January 2013 - 06:41 AM

View Postmacosxnerd101, on 01 January 2013 - 07:57 AM, said:

No. You cannot declare function prototypes inside other functions.


You can actually (though I don't know why you'd want to). That's why the most vexing parse exists in C++.
Was This Post Helpful? 0
  • +
  • -

#9 NitinJotwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 28-September 12

Re: function should return no values

Posted 01 January 2013 - 07:05 AM

View Postjimblumberg, on 01 January 2013 - 05:02 AM, said:

No void main() is incorrect. The C and C++ standard require that main must be defined to return an int and you should return an int from this function, except in very rare cases.
int main()
{
   return(0);
}


Jim


as far as i know, when i use my gcc, it allows me to use void main() when compiled with gcc, but it won't allows me the same when i compile with g++...please correct me if im wrong
Was This Post Helpful? 0
  • +
  • -

#10 sirgeek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-December 12

Re: function should return no values

Posted 01 January 2013 - 07:22 AM

View PostFrostHeart07, on 01 January 2013 - 04:22 AM, said:

You can use void main() instead of int main(), like this:

#include<stdio.h>
void main()
{
    int i =12;
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");

} 


Also, please practice proper use of indention so people can easily read your codes specially if it's long. :)/> Usually it's four spaces after the outer braces, like what I've done above.


This is VERY illegal and will not be allowed. Declare it above and then use it in int main

#include <stdio.h>

void valueIs()
{
    int i =12;
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");
    printf(" value is %d\n, i++");
}

int main()
{
   valueIs();

   return 0;
}


Was This Post Helpful? 1
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: function should return no values

Posted 01 January 2013 - 07:22 AM

Quote

as far as i know, when i use my gcc, it allows me to use void main() when compiled with gcc, but it won't allows me the same when i compile with g++...please correct me if im wrong

Both gcc and g++ should reject void main(). The C compiler (gcc) will allow main to be defined as main() until you start compiling for C11, because until then C allowed default return arguments.

And even if they did allow the use of void main() they would then be non-standard compliant in regards to the usage of main. The only time main can be defined as returning anything other than an int is in a non-hosted environment.

Jim

This post has been edited by jimblumberg: 01 January 2013 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#12 sirgeek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-December 12

Re: function should return no values

Posted 01 January 2013 - 07:25 AM

View PostNitinJotwani, on 01 January 2013 - 07:05 AM, said:

View Postjimblumberg, on 01 January 2013 - 05:02 AM, said:

No void main() is incorrect. The C and C++ standard require that main must be defined to return an int and you should return an int from this function, except in very rare cases.
int main()
{
   return(0);
}


Jim


as far as i know, when i use my gcc, it allows me to use void main() when compiled with gcc, but it won't allows me the same when i compile with g++...please correct me if im wrong



void main() does work but is illegal and with some compilers, they will not allow you to do it to stop your porgram fucking everything up. Be safe, use int main() :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1