• 13
• 14
• 27
• 9
• 9

# help with functions PLZ!

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

so i have made a hello world with a loop and if statement, now i want to learn how to do functions, keep in mind i am teaching myself from a website. here is my code:
//a small C++ program
#include <iostream>
#include <string>
using namespace std;
int figure(int cbare, int older, int younger);
int main()
{
int cbare;
int alternate;
int older;
int younger;
string name;
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";

getline(cin, name);
while (name == ""){
cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";
getline(cin, name);
}
cin.get();

cout<<"enter you favorite number "<<name<<"\n";
cin>>cbare;
cin.ignore();

figure(cbare, older, younger);

int figure(int cbare, int older, int younger){

if (cbare > 21){
older = cbare - 21;
cout<<"You are older than me by "<<older<<" years\n";

}else if (cbare < 21){
younger = 21 - cbare;
cout<<"I am older than you by "<<younger<<" yeas\n";
}else{
cout<<"You must be 21\n";
}
return;
}
cin.get();
}

this is my error message:
Quote:
 error C2601: 'figure' : local function definitions are illegal
arrrgggg...lol...i understand functions cause i took vb and php, however unfamiliar w/ how c++.net calls them and declares them...what should i be doing?

##### Share on other sites
You've defined your figure function within the main function. This is not allowed in C/C++.
Move it outside main and it should work.

##### Share on other sites
The function declaration should not go inside the main function.

so something like:
int figure ( ... );int main(){}int figure ( ... ){}
As you can see, the decleration of figure goes at the top. YOu want to make sure you have that BEFORE you try to use it or you will get the error message.

Dwiel

##### Share on other sites
that's what i have....
Quote:
 using namespace std;int figure(int cbare, int older, int younger);int main(){ int cbare; int alternate;

......
Quote:
 figure(cbare, older, younger);int figure(int cbare, int older, int younger){ if (cbare > 21){ older = cbare - 21; cout<<"You are older than me by "<

##### Share on other sites
#include <iostream>#include <string>using namespace std;void figure(int cbare, int older, int younger); // Formal definition.int main(){  int cbare;  int alternate;  int older;  int younger;  string name;  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";  cout<<"What is your name?\n";    getline(cin, name);  while (name == ""){	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      getline(cin, name);  }  cin.get();    cout<<"enter you favorite number "<<name<<"\n";  cin>>cbare;  cin.ignore();  figure(cbare, older, younger);  cin.get();  return 0;}void figure(int cbare, int older, int younger) {    if (cbare > 21){	  older = cbare - 21;        cout<<"You are older than me by "<<older<<" years\n";        }else if (cbare < 21){	  younger = 21 - cbare;	  cout<<"I am older than you by "<<younger<<" yeas\n";  }else{	  cout<<"You must be 21\n";  }  return;}

Try that.

A few things. You cannot define a function within a function in C++. You can have the formal function definition, and then the actual function code as you have. Or you could just have the entire figure() code above main, without having to worry about declaring it.

Also, I changed your figure() function to return void. You originally had it return int, but there was no int to return. This would have caused another error.

Also, the C++ standard says main should return an int, 0, upon completion.

##### Share on other sites
Ok - the error message has come as you've defined the figure function within the main function - this is a no-no. You should have:

//headersint figure(int cbare, int older, int younger);int main(){// stuff including calling figure, but not defining it!}int figure(int cbare, int older, int younger){// stuff}

Note that you could also define figure prior to main, meaning that you wouldn't need to forward declare it in this program, thus:

// headersint figure(){// stuff}int main{// stuff}

One other observation: you don't need to have older and younger as parameters to your figure function. They are defined locally (to the function), as opposed to being parameters you are entering to the function. So I would code it like this (with a couple of other small changes):

//a small C++ program#include <iostream>#include <string>using namespace std;// Define figure functionvoid figure(int cbare){    if (cbare > 21){	  int older = cbare - 21;        cout<<"You are older than me by "<<older<<" years\n";        }else if (cbare < 21){	  int younger = 21 - cbare;	  cout<<"I am older than you by "<<younger<<" yeas\n";  }else{	  cout<<"You must be 21\n";  }}int main(){  int cbare;  string name;  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";  cout<<"What is your name?\n";    getline(cin, name);  while (name == ""){	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      getline(cin, name);  }  cin.get();    cout<<"enter you favorite number "<<name<<"\n";  cin>>cbare;  cin.ignore();  figure(cbare);  cin.get();}

Finally - you might get issues with cbare if someone enters a non-int, but you can find that for yourselves.
Of course, someone else will come along now and criticise what I've just written, but ho hum!

HTH,
Jim.

Edit : 5th response! I thought I typed a damn-site quicker than that!

[Edited by - JimPrice on November 25, 2004 3:05:58 PM]

##### Share on other sites
thanks for the advice...just so i understand...i kinda thought i would need to declare my function as something other than in figure(), so by using void(what exactly is that?) it will be able to return my parameters and run the if statement?

##### Share on other sites
Not quite sure what you're asking with the first part of your question - the problem with function was that it was defined inside of main:

int main{    // opening brackets for main// stuffint figure(){    // opening brackets for figure// stuff}    // closing brackets for figure}    // closing brackets for main

So the function definition is physically inside the other function (main) - whereas it should be outside - just check the bracketing on any of the examples given.

Void is used as a function return type to show that nothing is being returned - your figure function is doing some actions, but doesn't need to provide any feedback to the main function, therefore can be defined as void (no return from this function). At the point it's called, the function is being asked to provide its functionality with the parameters you have given it, and do things with it. In your case, it's running some if-checks and outputting some statements, but nothing needs to be fed-back to the main loop. Alternatively, you could do:

//headersint figure(int cbare){  int differenceInAge = cbare - 21;  return differenceInAge;}int main(){// do stuffint ageDiff = figure(cbare);if (ageDiff > 0)     cout<<"You are older than me by "<< ageDiff <<" years\n";// etc.}

The function now only does the age difference calculation, but needs to feed the difference in ages back to the main function. It therefore now has a return type (int).

Hope this is all clear,
Jim.

##### Share on other sites
//a small C++ program#include <iostream>#include <string>using namespace std;void figure(int cbare, int older, int younger);int main(){  int cbare;  int alternate;  int older;  int younger;  string name;  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";  cout<<"What is your name?\n";    getline(cin, name);  while (name == ""){	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      getline(cin, name);  }  cin.get();    cout<<"enter you favorite number "<<name<<"\n";  cin>>cbare;  cin.ignore(); figure(cbare, older, younger);  cin.get();}void figure(int cbare, int older, int younger){    if (cbare > 21){	  older = cbare - 21;        cout<<"You are older than me by "<<older<<" years\n";        }else if (cbare < 21){	  younger = 21 - cbare;	  cout<<"I am older than you by "<<younger<<" yeas\n";  }else{	  cout<<"You must be 21\n";  }  return ;}

now it builds...but when i type in a number it tells me younger is undeclared? why??