Learn C++ Language from the Best Tutors
Search in
What is the main difference between returning value of a function and does not return any value of a function in C. Which is correct void main(void), int main(void), int main(int) , or void main(int) and explain the main difference between int main() and int main(void).
Asked by Mahesh Kondawar Last Modified
A function may or may not return a value to the calling function. If the function is returning a value, then you have to mention the data type in the function definition and funtion declaration (like int, float or double etc.). If the funtion not returning any value, then you have to mention the data type as void. If you not mention return type, then compiler take it as int data type.
Similary, a function may have arguments or not. If your functions have arguments, then you have to mention data types of arguments. If you are not passing any arguemtns then you may write void or simply live it blank that is depend upon the compiler. main() is a user defined function, so it follows the same rules.
read lessHi, Thanks for A2A, your 1st question is "What is the main difference between the returning value of a function and does not return any value of a function in C".
the main difference between both types of the function will be its return type as the function which will not return any value will have return type "void" which means nothing wherein function with the return value should have a return type "data type" int, char, struct so ....on..while declaring/defining both !! and one more major difference will be, function with no return type (or can say void return type ) don't need any additional variable to save its return value wherein function with return value may have addition variable or additional check for return value !!
now coming to your 2nd question "Which is the correct void main(void), int main(void), int main(int), or void main(int) and explain the main difference between int main() and int main(void)"
ans: best practice is to use int main(void) if it does not have command line arguments !! now using void main(void) is fine but gives warning in some compilers as the main function expected to return integer value !! now int main(int) is incorrect !!
read lessHi, First we understand what a function is? A function is a peice of code that can be used for code reusability. For example we are creating a calculator. Either we can write everything inside main() or for better reusability we write add, subtract etc different functions. We can call these function whenever the needed without repeating the code. Now coming to your question function may or may not return value that depends on requirement. For example we can print add function output inside the function scope. But sometime we need that output for further calculation than we can return output of add function.
read less1. Both type of function can exactly the same, only difference is that return type function return some value so it can be used in expression. Non return type function doesn't return any value so it can't be used in expression.
2. The main is also a function so it can be either return type or non return type.
Return type main function looks like this...
int main(void) {
return 0;
}
Non return type main function looks like this...
void main(void) {
}
int main(int) {
// This is incorrect...
}
read less
Whenever you put return type as int, your program always return success code to operating system which is integer value. If you use void as return type of main then your program does not return any indication to operating system. Returning int is good programming practice always.
Hope this help.
Regard,
Sam J
www.myVuniversity.com
read lessInt main(void)--- it can be called only with a function call having no parameters.
But int main() can be called by a function with any no.of parameters.
int main(void) returns 0 if the program is successfully executed.
void main(void) does not return anything.
it is a good practice to return some value in any program to know the succesful execution.
read less
View 10 more Answers
Related Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
About C++ Programming Language
Introduction C++ is an excellent programming language and many of the applications are written in C++ language. It has generic, object-oriented & imperative programming features, and also provides facilities for low-level memory manipulation. Successor of C language, it is an OOP (object oriented programming) language...
Read full article >
Learn Hadoop and Big Data
Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...
Read full article >
Make a Career in Mobile Application Programming
Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...
Read full article >
8 Hottest IT Careers of 2014!
Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...
Read full article >
Looking for C++ Language Classes?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for C++ Language Classes are on UrbanPro
The best Tutors for C++ Language Classes are on UrbanPro