Hi
I'm an outright beginner and new to this programming world.
Why are void main() and int main() called functions? Are their functions similar to that of mathematical functions? Is there a difference between void main() and int main ()? I'm in search of an easy explanation which could fit my very limited knowledge. Thanks for your help and time.
11 replies to this topic
#1
Posted 02 April 2011 - 11:03 AM
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
|
|
|
#2
Posted 02 April 2011 - 12:42 PM
Yes, they're called because functions in a way they're like those in math, returning values. Except those which are of type void. Perhaps a better explanation can be found here.
Also, seeing you're signature, I'd suggest you switch your IDE, since Dev-C++ is not updated anymore. I'd recommend Code::Blocks or Visual Studio (I think Express version can be downloaded for free), or a nice text editor with syntax highlighting and MinGW.
Also, seeing you're signature, I'd suggest you switch your IDE, since Dev-C++ is not updated anymore. I'd recommend Code::Blocks or Visual Studio (I think Express version can be downloaded for free), or a nice text editor with syntax highlighting and MinGW.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#3
Posted 02 April 2011 - 11:21 PM
They're kind of like math functions as Flying Dutchman said. Basically you're listing the return type before "main". Example:
This will not return a value for the main function. However, in this case, you don't need to return a value because you listed it as void.
Hope this helps.
void main() {
} This will not return a value for the main function. However, in this case, you don't need to return a value because you listed it as void.
int main() {
return 0;
}
The function must return the value you originally listed it as ("int" is short for integer). In this case, you would have returned 0, which is an integer.Hope this helps.
#4
Posted 03 April 2011 - 12:18 AM
*edit begin*
If you are a brand new programmer then some stuff I mentioned below might go over your head if you don't understand the very basics. I assumed that you did, but I'm not so sure now so I'll explain quickly the very basics in this edit.
So let me explain the very basics first. main() is a function just like any other that you will be able to create. The difference with main() however is that it is the starting point of your program. This means that whatever you put inside main() is going to be what your program executes and outputs from beginning to end.
In the examples below I use a custom made function called Addition(int x, int y). If you were to never actually use this function inside main() then it would never be used in your program. It would be compiled and ready to use, but never actually used. You can think of Addition(int x, int y) below as a helper function. Helper functions are written outside of main(), but once written can be "called" inside main or heck can be called from any other function.
So in summary you can create as many functions as you want, but the actual program starts in main() while the other functions just sit there waiting to be used as helpers to manipulate data.
*edit end*
When you start programming more you'll see that functions have return types and values. The return type is the type of data that is going to be passed to the calling function when that function it called completes. You have your basic types like int (integer which is basically a whole number), double which is a number with a decimal point, strings which are text, char which is a single character, bool which is true or false, void which means it returns nothing, and different variations of those to accommodate different ranges. The return value is the value that the function gives to other functions that call that function. The value will have to match the type that the function expects to have returned. For instance if your function has an int return type then it expects the function to return a whole number, if it has a double return type it expects the function to return a number with a decimal point, etc. You will eventually get into where you can create your own data types and functions can even return values of those custom data types!
Example:
In this example the function is told that the return type is going to be an int because of the int keyword in front of "Addition". The int x and int y within parenthesis are called parameters, these are the types that the function can receive to do some operation with. This particular function can receive two integer values at which point it will take those values of the integers that are passed and copy them into the x and y variable. These copies of the received variables are then used inside the function body in some way. In this example they are added together and the result is returned. So when this function is called somewhere in our program, let's say inside main(), main() receives the result of the function to be used in whatever way it wants to use it (as shown below the Addition function). main() declares an int called sum then sets sum equal to the returned value of the Addition(int,int) function, which is 8.
There's a little crash course on functions. If you have any other questions feel free to ask.
If you are a brand new programmer then some stuff I mentioned below might go over your head if you don't understand the very basics. I assumed that you did, but I'm not so sure now so I'll explain quickly the very basics in this edit.
So let me explain the very basics first. main() is a function just like any other that you will be able to create. The difference with main() however is that it is the starting point of your program. This means that whatever you put inside main() is going to be what your program executes and outputs from beginning to end.
In the examples below I use a custom made function called Addition(int x, int y). If you were to never actually use this function inside main() then it would never be used in your program. It would be compiled and ready to use, but never actually used. You can think of Addition(int x, int y) below as a helper function. Helper functions are written outside of main(), but once written can be "called" inside main or heck can be called from any other function.
So in summary you can create as many functions as you want, but the actual program starts in main() while the other functions just sit there waiting to be used as helpers to manipulate data.
*edit end*
When you start programming more you'll see that functions have return types and values. The return type is the type of data that is going to be passed to the calling function when that function it called completes. You have your basic types like int (integer which is basically a whole number), double which is a number with a decimal point, strings which are text, char which is a single character, bool which is true or false, void which means it returns nothing, and different variations of those to accommodate different ranges. The return value is the value that the function gives to other functions that call that function. The value will have to match the type that the function expects to have returned. For instance if your function has an int return type then it expects the function to return a whole number, if it has a double return type it expects the function to return a number with a decimal point, etc. You will eventually get into where you can create your own data types and functions can even return values of those custom data types!
Example:
In this example the function is told that the return type is going to be an int because of the int keyword in front of "Addition". The int x and int y within parenthesis are called parameters, these are the types that the function can receive to do some operation with. This particular function can receive two integer values at which point it will take those values of the integers that are passed and copy them into the x and y variable. These copies of the received variables are then used inside the function body in some way. In this example they are added together and the result is returned. So when this function is called somewhere in our program, let's say inside main(), main() receives the result of the function to be used in whatever way it wants to use it (as shown below the Addition function). main() declares an int called sum then sets sum equal to the returned value of the Addition(int,int) function, which is 8.
int Addition(int x, int y)
{
return x+y;
}
void main()
{
int sum;
sum = Addition(5, 3);
}
Now if we were to do the same exact thing except change the return type of Addition(int x, int y) to void it would not return any value at all to its calling function and including return x+y would give you an error. So the example below shows the difference of using void. void does an operation on x and y, but it doesn't return anything for main() [the calling function] to receive. This would give an error as well because sum cannot be set equal to a non int. You would get some error about no valid right operand for = sign. This void Addition function really doesn't do anything useful and is just meant to show #1 that voids don't return a value, and #2 calling functions (main in this case) don't receive any values if no values are returned by a function it calls.
void Addition(int x, int y)
{
x + y;
}
void main()
{
int sum;
sum = Addition(5, 3);
}
Now here is a little extra info that might be helpful. I know when I was new to programming it took me awhile to know what was going on with parameters. As I said, when a function receives parameters it copies the value of whatever is passed to the function into those parameter variables then those parameter variables do the operations. What this means is that your original variable is not changed when it is passed to a function as shown below.
void IncreaseVariable(int x)
{
x = x + 10;
}
void main()
{
int myVariable = 0;
IncreaseVariable(myVariable);
IncreaseVariable(myVariable);
IncreaseVariable(myVariable);
}
What does myVariable equal after 3 calls to the IncreaseVariable function? 30? No it equals 0 still. Remember the function is only working on a copy of the variable each time it is called, the actual variable passed to the function is not changed. What this is called is "passing a variable by value". There is a way to actually make a function update a variable's value. The variable has to be "passed by reference" instead of by value. Below shows how to pass by reference.
void IncreaseVariable(int &x)
{
x = x + 10;
}
void main()
{
int myVariable = 0;
IncreaseVariable(myVariable);
IncreaseVariable(myVariable);
IncreaseVariable(myVariable);
}
Now when the three IncreaseVariable calls complete the value of myVariable will be 30. By adding a & symbol in the IncreaseVariable function in front of the parameter variable you are telling the function to handle the argument that is passed to it differently. Instead of copying the variable passed to it into its parameter variable then manipulating that copy you are passing it a reference to the memory address of the variable being passed to the function. As a result, when the function manipulates the value of the variable passed to the function it is actually accessing the original variable's location in memory and updating that value at that memory location.There's a little crash course on functions. If you have any other questions feel free to ask.
Edited by Zer033, 03 April 2011 - 12:51 AM.
#5
Posted 03 April 2011 - 03:34 AM
@Zer033, excellent post, but you assumed OP's coding in C++ due to usage of reference operator. In C it would've had to be done with pointers.
@jackson6612, whether you're coding in C or C++, use int main() and return 0 if main didn't encounter any problems. (me and my friends have this saying that void main is false teachings :) )
@jackson6612, whether you're coding in C or C++, use int main() and return 0 if main didn't encounter any problems. (me and my friends have this saying that void main is false teachings :) )
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#6
Posted 03 April 2011 - 04:04 AM
C and C++ are part of a family of languages called "Procedural languages". The idea is that each language is composed of procedures and functions, where a procedure does processing, and a function does processing and returns a value. "does processing" can mean a variety of things, like modifying global variables, producing output, asking for input, etc.
In C and C++, everything is inside a function. If there is no return value (a procedure), the return type is void.
In C and C++, everything is inside a function. If there is no return value (a procedure), the return type is void.
#7
Posted 03 April 2011 - 05:35 AM
void main() returns no value. int main() returns an integer value. Functions in imperative languages like C are not the same as mathematical functions, but they are similar in that they take input (parameters) and produce output (return values). Are you reading a book on C? I would suggest C in a Nutshell, from O'Reilly Media.
Programming is a journey, not a destination.
#8
Posted 03 April 2011 - 01:55 PM
I genuinely thank you guys, Flying Durchman, Jrb, Zer033, for helping me with this.
Zer033, I have to say your reply was a good one. I would read it several times and then ask some follow-on questions.
Regards
Jackson
PS: I'm coding (rather trying to) in C++.
Zer033, I have to say your reply was a good one. I would read it several times and then ask some follow-on questions.
Regards
Jackson
PS: I'm coding (rather trying to) in C++.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#9
Posted 04 April 2011 - 01:22 PM
Hi again
Some short questions. Please don't forget I'm an outright beginner. So, if you think answers to any of the questions could be too complex for me to understand then you simply tell me that. Thanks.
If I want to use the function "sqrt()" then I would have to include "#include <cmath>" header file which makes reference to some library file. Correct? "int main()" is also a function, but what kind of function is it? A library function?
Is void main() a non-standard function in C++?
Please guide me on these things. Thanks.
Some short questions. Please don't forget I'm an outright beginner. So, if you think answers to any of the questions could be too complex for me to understand then you simply tell me that. Thanks.
If I want to use the function "sqrt()" then I would have to include "#include <cmath>" header file which makes reference to some library file. Correct? "int main()" is also a function, but what kind of function is it? A library function?
Is void main() a non-standard function in C++?
Please guide me on these things. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#10
Posted 04 April 2011 - 03:29 PM
"main" is a C standard function that a linker will link to as the first function to begin the actual program you've written. It won't truly be the first thing in your executable to execute, but it's the first thing in your program to execute, if that makes sense...
You seem to understand why header files need to be included, but you should also remember how header files actually work in C and C++. The header files are simply copy/pasted by the preprocessor into the file at the location the #include statement is found. This can be both powerful and dangerous, be careful of what you put in a header file (such as using namespace statements)! A header file usually has what are called "Function Prototypes" and Classes within them. Function Prototypes are a special kind of syntax that defines how a function is called, but not how it's implemented. This means the compiler will see the function call as legitimate, and will leave a hook for the linker to attach an actual implementation to.
For example, consider this header file:
The header itself does not make a reference to the library, your linker has to find the library on it's own when it 'links' the object files (non-executable binary files) into an executable file, that you can actually run. This is done automatically by your compiler, which is then hidden away from your IDE, so that may be a bit fuzzy and I apologize. What this means in practicality is that the compiler must know where the header files are, and the linker must know where the libraries and object files are. If you use a different IDE (one that works on Linux), I can tell you how to make sure that is the case with your IDE, but I know very little about Dev-C++ except how old it is, as Flying Dutchman had mentioned.
You seem to understand why header files need to be included, but you should also remember how header files actually work in C and C++. The header files are simply copy/pasted by the preprocessor into the file at the location the #include statement is found. This can be both powerful and dangerous, be careful of what you put in a header file (such as using namespace statements)! A header file usually has what are called "Function Prototypes" and Classes within them. Function Prototypes are a special kind of syntax that defines how a function is called, but not how it's implemented. This means the compiler will see the function call as legitimate, and will leave a hook for the linker to attach an actual implementation to.
For example, consider this header file:
#if !defined(MY_HEADER_H_INCLUDED) #define MY_HEADER_H_INCLUDED int my_function(int first, int second); #endif // MY_HEADER_H_INCLUDEDYou'll notice the function isn't implemented, instead it's just the function header. The function is defined later in a different source file! Now if you do this:
#include "my_header.h"
#include <iostream>
int main(void)
{
std::cout << my_function(5, 10) << std::endl;
}
Your program will compile, but it will not finish linking. This is only because my_function hasn't been implemented. You can implement it pretty easily.#include "my_header.h"
int my_function(int first, int second)
{
return first + second;
}
Now, try and compile both the files at the same time (however you'd do that in Dev-C++), and it'll compile successfully! This is how you make multi-file programs. Writing static and dynamic libraries are outside of the scope that I'm trying to teach you, but this should give you an idea of how and why header files work.The header itself does not make a reference to the library, your linker has to find the library on it's own when it 'links' the object files (non-executable binary files) into an executable file, that you can actually run. This is done automatically by your compiler, which is then hidden away from your IDE, so that may be a bit fuzzy and I apologize. What this means in practicality is that the compiler must know where the header files are, and the linker must know where the libraries and object files are. If you use a different IDE (one that works on Linux), I can tell you how to make sure that is the case with your IDE, but I know very little about Dev-C++ except how old it is, as Flying Dutchman had mentioned.
Wow I changed my sig!
#11
Posted 04 April 2011 - 04:34 PM
I believe there is a good word to explain 'main()', and the word is entry point. What does entry point mean? It is the point to which something enters. It's like when you enter a house, the entry point is the first room you encounter. Likewise, the the entry point in an application, is what which the hardware, or the execution of the software, first enters.
As with void.
"nothingness: the state of nonexistence" - wordnetweb.princeton.edu/perl/webwn
So names, what things are called, are often descriptive.
As with void.
"nothingness: the state of nonexistence" - wordnetweb.princeton.edu/perl/webwn
So names, what things are called, are often descriptive.
#12
Posted 10 April 2011 - 08:17 PM
Actually there is so much differnce between void main and int main. When we write Void to any function it means that it doesn't have any return type. But when we write int main() it means that it have some return type.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









