Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-03-2003, 06:22 PM   #1 (permalink)
ecox76
Registered User
 
ecox76's Avatar
 
Join Date: Mar 2003
Location: USA
Posts: 2
ecox76 is on a distinguished road
Send a message via Yahoo to ecox76
Question can someone please explain this to me

why does this code work, but if the main function is before the rest of them, it won't compile? i don't understand why "main" has to be on the bottom! very puzzling.

==============================
Code:
#include <iostream.h>

int one()
{
cout <<"you picked one!" <<endl;

return 0;
}

int two()
{
cout <<"you picked two!" <<endl;

return 0;
}

int three()
{
cout <<"you picked three!" <<endl;

return 0;
}


int main()
{
int pick;

cout <<"Pick 1,2, or 3: " <<endl;
cin >> pick;

        if (pick==1)
                {
                one();
                }
        if (pick==2)
                {
                two();
                }
        if (pick==3)
                {
                three();
                }

return 0;
}
===========================================
ecox76 is offline   Reply With Quote
Old 03-03-2003, 07:59 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
because otherwise you have no reference to them

to solve the problem try this:

Code:
int abc(int x); // declare your func
// obviously, however, this declaration must be above where you call the function ( in this case; main() )
int main(){
    int i;
    i = abc(34);
}

int abc(int x){ //implement your func
  return x + 1;
}
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-03-2003, 08:14 PM   #3 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
functions must either be prototyped or defined above main. main is a function, the first function called, and returned to once a function that is called is complete. once main is done, the program is done.

functions can only see what is above them.

for example, for the code you posted. if you want function three to call function two, it can. but function two cannot call function three. if you prototype the funtions, i believe you could call function three from function two. (not sure though)

however, functions cannot call main.

hope this helps some.

edit:
Code:
#include <iostream>

void test_one();
void test_two();

int main()
{
	test_one();
	
	std::cin.get();
	return 0;
}

void test_one()
{
	std::cout << "Test One";
	std::cout << std::endl;
	test_two();
}

void test_two()
{
	std::cout << "Test Two";
	std::cout << std::endl;
}
this will output:

Test One
Test Two

you can call function two from one if it is prototyped and declared before main.
alpha is offline   Reply With Quote
Old 03-03-2003, 08:52 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Quote:
Originally posted by alpha
however, functions cannot call main.
... what on earth makes you say that?
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-03-2003, 09:03 PM   #5 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
confirmation

alpha got it completely right but seemed unsure. So I'm here to say, yes.

The functions can be prototyped (what I always do) like so:

(for your program they would look like this)

Code:
//include files

#include <iostream.h>

//function prototypes

int one(); //They're just the functions titles copied with 
int two(); // semicolons at the end.
int three();


int main()  //Now main can go up top which makes sense because
{  //its the first function the program goes to.
int pick;

cout <<"Pick 1,2, or 3: " <<endl;
cin >> pick;

        if (pick==1)
                {
                one();
                }
        if (pick==2)
                {
                two();
                }
        if (pick==3)
                {
                three();
                }

return 0;
}


int one()
{
cout <<"you picked one!" <<endl;

return 0;
}

int two()
{
cout <<"you picked two!" <<endl;

return 0;
}

int three()
{
cout <<"you picked three!" <<endl;

return 0;
}
When you do this you can place the main function at the top of the program. I feel this makes more sense as its the first function the program goes into anyway.

The reason the compiler works this way (main on top or prototypes) is because the compiler needs advanced warning about whats going on in the program before it compiles. It needs to allocate (reserve for future use) memory for function calls, variables and other things. It does this so it doesn't run out of memory part way through and crash your computer (among other reasons). When you prototype the top of your program it gives the compiler a concise list of everything your program is going to need before it compiles.

If I'm off on this anyone feel free to correct me.

It also gives you a quick way to look at all your functions. I like prototyping and would recommend it to new C and C++ coders.

Very good question and welcome to code newbie.
saline is offline   Reply With Quote
Old 03-04-2003, 05:06 AM   #6 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
Quote:
Originally posted by abc123
... what on earth makes you say that?
you can call main. but is there a reason to?
alpha is offline   Reply With Quote
Old 03-04-2003, 11:00 AM   #7 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Re: confirmation

Quote:
Originally posted by saline
The reason the compiler works this way (main on top or prototypes) is because the compiler needs advanced warning about whats going on in the program before it compiles. It needs to allocate (reserve for future use) memory for function calls, variables and other things. It does this so it doesn't run out of memory part way through and crash your computer (among other reasons). When you prototype the top of your program it gives the compiler a concise list of everything your program is going to need before it compiles.
the compiler needs to know what kind of parameters to pass to your function and what kind of parameters to expect in return. memory for function variables is dynamically done on the stack.
if your program runs out of memory (overflows the stack), it will crash itself, but should not crash your computer. modern operating systems use virtual memory systems that keep the program memory space separate from kernel memory. this is not true if you're running an archaeic non-memory protected os like DOS or macos <= 9 (and is partially true for halfassed solutions such as win95/98).

as for calling main(), you can definitely do it. however, there are very few valid reasons for doing so.
joe_bruin is offline   Reply With Quote
Old 03-04-2003, 06:46 PM   #8 (permalink)
ecox76
Registered User
 
ecox76's Avatar
 
Join Date: Mar 2003
Location: USA
Posts: 2
ecox76 is on a distinguished road
Send a message via Yahoo to ecox76
Cool

ok, that makes sense. thanks for the replies.
ecox76 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting