Tran-Gate
Oct 22 2008, 01:41 AM
I have a few questions on C++ mainly about functions. - Why does int main() have to return a value?
- Why are parameters/arguments used in int main()?
- In the following program, doesn't all of the code need to be executed into the int main() part?
CODE #include <iostream> using namespace std; void box(int length, int width, int height) int main() { box(7, 20, 4); box(5, 4, 3); box(24, 1, 5); return 0; } void box(int length, int width, int height) { cout << "The volume of box is: " << length * width * height << "\n"; } Thanks in advance~!!
Comment/Reply (w/o sign-up)
veerumits
Oct 22 2008, 05:26 AM
This is the programming power that you use there is nothing deep sense just use the programming skill and use logic to complete our taske any how any cost search the required logic and programmed it. in your case C++ compiler facility it must return integer value so thats by here int return.
Comment/Reply (w/o sign-up)
galexcd
Oct 22 2008, 06:19 AM
QUOTE(Tran-Gate @ Oct 21 2008, 06:41 PM)  - Why does int main() have to return a value?
Why do you have to return a value? Because when you declare the main function you wrote int in front of it. That tells the computer that the function you are writing must return an int. Now why do you write an int in front of main? Well because in C++, the computer calls main and expects to receive an integer in return. Void main may work in some compilers but C++ just isn't quite designed to call the function that returns an int. Here is a pretty good explanation why C++ wants an int to be returned rather than nothing. QUOTE(Tran-Gate @ Oct 21 2008, 06:41 PM)  - In the following program, doesn't all of the code need to be executed into the int main() part?
I'm not quite sure what you mean by this. Are you asking why not all the code is in the main function? The code that is outside the main function is the declaration of the function named "box". It is essentially the same as the main function, except it doesn't return anything, and it is not called by the computer when the program is run. When you run your program the computer calls the main function which runs whatever is inside. If nothing calls the box function it will never run, however in the example code you have provided, the main function calls the box function. In fact it calls it three times. Perhaps the question you should be asking first is "What is a function?"
Comment/Reply (w/o sign-up)
Tran-Gate
Oct 23 2008, 03:10 AM
QUOTE I'm not quite sure what you mean by this. Are you asking why not all the code is in the main function? The code that is outside the main function is the declaration of the function named "box". It is essentially the same as the main function, except it doesn't return anything, and it is not called by the computer when the program is run. When you run your program the computer calls the main function which runs whatever is inside. If nothing calls the box function it will never run, however in the example code you have provided, the main function calls the box function. In fact it calls it three times. Perhaps the question you should be asking first is "What is a function?" I meant, I was told that ALL of the code should be executed in the main() function. Well, I don't understand why can't the void box() function be IN the int main() function. Can there only be one function executing at the same time? I am so confused....
Comment/Reply (w/o sign-up)
rpgsearcherz
Oct 23 2008, 03:53 AM
It can all be in main, but it is split to make it easier to read. The reason is that when you get longer programs, it's easier to break it into smaller blocks... Think of it like this... On one hand you could organize your list of continents and countries like this.. 1 - USA 2 - Brazil 3 - Australia 4 - Canada 5 - Africa Or you could organize it like this.. 1)North America A) USA B)Canada 2)South America A)Africa And so on. The second list is easier to read because it's much more organized. The same goes with programs. As you go on you will write longer and longer programs, some going 30k+ lines. This helps to cut down on the hassle of going through one line at a time looking for what went wrong. In the beginning, though, I would just put everything in Main. Hopefully that helps you understand it better.
Comment/Reply (w/o sign-up)
Tran-Gate
Oct 23 2008, 11:28 PM
I see. But what dies the main() function actually do if it is not where all the code is executed?
Comment/Reply (w/o sign-up)
galexcd
Nov 4 2008, 05:40 PM
QUOTE(Tran-Gate @ Oct 23 2008, 04:28 PM)  I see. But what dies the main() function actually do if it is not where all the code is executed? Ok, apparently nobody has ever taught you about functions, so I'll do the best I can to explain it now. I want you to think of a function sort of like it's own program. You program it, and then you can run it. For example, lets say I want a function that prints the word "Hello". Right now don't worry about returning anything. So let's name the function greeter. CODE void greeter(){ cout<<"Hello"; } Now every time you run the function greeter it will print out the word "Hello". So you can run greeter by calling it: CODE greeter(); Now when you are writing in C++ (and most other programming languages), the only thing you can really have in your code other than variable declarations is functions. You can't just create a new c file and write cout<<"Hello" directly in the body. Code like that must be inside a function. So if all of your code is in a function, what is going to run it? Remember a function only runs when it is called by something. If your code consisted only of void greeter(){ ...etc... then nothing would happen if you try to run the code. This is the point of the main function (also called main method). Every time you compile and run your entire c++ program, the first thing it does is it runs the main function. Technically no code has to go into your main method at all, but if you have nothing in it to run, then nothing will happen. You could even put all your code in a different function and have your main method call it. Example: CODE void myprogram(){
[...]Code goes here[...]
}
int main(){ myprogram(); return 0; } Does that help clear it up?
Comment/Reply (w/o sign-up)
Tran-Gate
Nov 5 2008, 12:22 AM
Oh, so main() is what the compiler runs first then it runs the other functions next in order....
Comment/Reply (w/o sign-up)
galexcd
Nov 5 2008, 12:42 AM
QUOTE(Tran-Gate @ Nov 4 2008, 05:22 PM)  Oh, so main() is what the compiler runs first then it runs the other functions next in order.... Well sort of. Main is the function that the compiler runs first, however the other functions are run as they are called. For example in the code below, the main method is called first (as always), and the main method calls two, and function two calls five, and then the main method calls three. Function one and four are never executed. CODE void one(){ cout<<"1..."; }
void two(){ cout<<"2..."; five(); }
void three(){ cout<<"3..."; }
int main(){ two(); three(); }
void four(){ cout<<"4..."; }
void five(){ cout<<"5..."; } The output of the code above would be: QUOTE 2...5...3...
Comment/Reply (w/o sign-up)
Tran-Gate
Nov 5 2008, 12:45 AM
Similar Topics
Keywords : Kinda Noobie Questions Functions Stuff- Various String Functions In C
- (7)
- Difference Between C, C++ And C#?
- And some other questions. (23)
What's the difference between C, C++ and C#? What is more closely related to working with
ASP.NET, C++, C#, or VB? Which comiler is best for C++ and/or C#? Are there any benefits in choosing
VB over C++ or C#? Z^ivjo HR...
Help With Functions In C++
- (6)
Hey guys I could really use some help wiht C++ because My C++ class consists of me three others, and
a ton of people learning HTML. So in other words its basically independent study. He dosnt really
care how you get the assignments done or when you do... just get them done. Well to say the least I
am STRUGLING in this class do to the lack of instruction. Now the latest assignment im sure seems
eazy for many of you but for me... well considering this is my first year of programming im still
quitea a newbie. So the assignment says right a function named Reduce() that...
Functions In Win32
- (2)
Hey, all. Hope someone can make some useful suggestions, as i'm pretty much at a dead end!
I'm writing some code to run in a win32 envrionment, using the API to interface with audio
hardware. At the moment this is very simple, and I'm pretty much just regurgitating micro$oft
tutorials. I'm using the Bloodshed Dev-C++ compiler. My problem is this -- i'm trying to
use the CWaveFile class to provide a simple interface to .wav files. This class is defined in
DSUtil.cpp, and makes use of the 'standard' mmio functions. To use these, I have to #in...
Functions Failing When Embedded In If()
- Just a beserk problem that's plagued me on and off for years (3)
I've had this problem on and off over the years (me just being silly, generally), so I thought I
might share how to fix it. This has come up several time whilst using sockets, actually, so
here's an example. For instance, CODE if(hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)
perror("socket() failure"); exit(1); } What's the problem? The problem is that,
CODE hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP) should be encapsulated within
parentheses. Why? When the call is made, a comparison between the return value and the 0 is ma...
Commonly Asked C/c++ Questions
- (0)
Commonly Asked C/C++ Questions
-------------------------------------------------------------------------------- Here are some
answers to commonly asked C/C++ questions on the forum. If you are new to the forum, please read
this thread as your question may already be answered here. This guide is divided into many sections:
0. Frequently Asked Newbie Questions (how do I pause output, compile my program etc.) 1. Converting
types 2. Formatting Output 3. String manipulation (copy, concatenate, replace etc.) 4. File
Manipulation (File size, dir contents, remove files etc.) ...
Questions
- about c (3)
I want to learn new prog languages, and I chose C. But there are many problems, I don't know
where to begin /unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0"
alt="unsure.gif" /> . Can you tell me some compilers (free if its possible), editors, tutorials,
websites talking about it, hints, opensource programs, or any other thing? Please help a begginer
/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> Thanks...
Looking for kinda, noobie, questions, c,
|
Searching Video's for kinda, noobie, questions, c,
See Also,
|
advertisement
|
|