Taurus Car Club of America : Ford Taurus Forum banner

1 - 16 of 16 Posts

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #1
Does anyone know how to label a suroutine in C++? My "textbook" is a weekend crash course book a la "C++ for Dummies" style. Stupid professors.
 

·
Premium Member
Joined
·
5,359 Posts
Ok you know how you have the "main routine" something like this? Just add another outside of it.

void main (int argc, char *argv[]) {

mysubroutine();

}

void mysubroutine() {
cout<<"Check out my subroutine yo!\n";
}

excuse my outdated C skills... this is C but should work fine on a C++ compiler. Note that some compilers want you to put called subroutines before the main routine, or if a sub calls another sub, the sub that is called has to be before the sub that does the calling.
 

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #3
That's for functions. I want to use a subroutine. It's a little different. Thaks for your help, though; and FWIW, your'e right on about your function, except main should be an int, not a void.
 
A

·
Guest
Joined
·
0 Posts
You can int main or void main. If you int main you have to have a return 0; at the end of your program. If you void it, you don't.

I have no idea about the subroutines. I've never used 'em.
 

·
Premium Member
Joined
·
501 Posts
Subroutine as in a "goto" function? Been a few years, but I think it's something like this:

void foobar()
{
if(something happens)
{goto End;}

End:
return 0;
}
 

·
Premium Member
Joined
·
346 Posts
I know I got my C++ book somewheres. I'll see if I can dig it up.
 

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #7
You call it just like you would a function, but the variables are all global - the main function can "see" the variables and operations that occur insibe the subroutine. I just can't remember how to label it.

On a side note: does anyonw know a good C++ dictionary to purchase? It looks like I am going to need one down the road.
 

·
Premium Member
Joined
·
952 Posts
I've take two semesters of C++, got a B in Intro and an A in advanced. Never have I heard someone using a "subroutine" in C++. Maybe we just called it something different, but "goto"s are pretty primitive (like in the days of BASIC). Today if you want a global variable, you just define it outside of main and all of your functions should have access to it. if you define it in main you'll have to pass it in and back out as a parameter, or you can pass it as a pointer if you want to modify it directly so you don't have to pass it back out.


//CPP Program Example
#include <stdio.h>

int MyNum = 0;

void AddMe();

void main ()
{
MyNum = 5;
AddMe();
cout << "Value of MyNum = " << MyNum;
};

void AddMe()
{
MyNum = MyNum + 1;
};
 

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #9
The problem is, Global variables still have to be initialized in each function to work properly (I had serious issues with that problem when I was writing a Runge Kutta program). I don't want to do that, I want the main function to see exactly what's going on in the subroutine. Maybe I'll just use it as a nested for loop.

EDIT: spelling
 

·
Premium Member
Joined
·
952 Posts
If you want main and the functions to have direct access to the same data, just pass a pointer. Pointer syntax is a little tricky to get right the first time, but once you figure it out, you can pass pointers all over and never have to worry about returning a variable.

iirc...

void AddNum (int &);

void main () {
int MyNum = 0;
AddNum (MyNum);
};

void AddNum (int &MyNum)
{
MyNum = MyNum + 5;
};



I think that's right... You're defining the function to take in a pointer as a reference so when you call the function, you pass the variable like you normally would, but the compiler converts it to a reference pointer, which allows you to access the data directly as if the variable was local. My sytanx may be off. It's been almost a year since I've done any of this.
 

·
Premium Member
Joined
·
5,359 Posts
Dan is right. C++ is a bit different from say BASIC, functions are used but I've never seen anyone use something like a subroutine. If you need to pass a lot of variables you can pass an array as well.
 

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #12
I ended up just using a function. Iv'e used functions alot, and I wanted to try something different for fun. Oh, well. I had it running in about ten minutes after I decided to switch it over (had to fix a couple syntax bugs - stupid semicolons). I just used global variables so that I didn't have to worry about return all the necessary values. Thanks for all your help guys, and when I will ask my prof. about the subroutine thing next week when soring break is over with, and let you know.

dan, why is the andpersand sign in your function declaration? I haven't seen that before.
 

·
Premium Member
Joined
·
952 Posts
IIRC, when you define a function at the top of your program, you tell it what types of variables you are going to pass it. The & means you are passing a reference variable. Then, down below, in the real function, you give a local name to the varriable you are passing. In this case, we are passing a reference to a pointer, which I think allows you to use a pointer reference like you would a regular variable, rather then having to deference the pointer to get to the data.

*p = Pointer p
&p = reference for pointer p

-Dan
 

·
Premium Member
Joined
·
952 Posts
You might be able to find an example in some of my old code...

Dan's code
 

·
Registered
Joined
·
695 Posts
It's been a while since I did any programming.

Look up Struct or Classes in the index. That might help you out.
 

·
Premium Member
Joined
·
10,022 Posts
Discussion Starter #16
I put my secondary function before the main function, so I don't have to declare it separately. I also pass values, not variables, so that I don't have to worry about my variable names.
 
1 - 16 of 16 Posts
Top