Quantcast

Jump to content


Photo

User-created Functions - C++


  • Please log in to reply
7 replies to this topic

#1 Supernature

Supernature
  • 87 posts

Posted 14 July 2010 - 09:22 PM

Ok, my instructor is /such/ a d-bag. He basically gives us assignments with little to no information and we basically have to figure things out on our own. This has been pissing me off all day, and it seems so simple yet I don't understand how to do it.

Here's the question:
http://i25.tinypic.c...8.jpg%5B/IMG%5D

I don't understand how to go about doing this. I can't use a cout (which is fucking stupid), and my code is just lacking.
#include <iostream>
using namespace std;

string firstPresident(string first, string last)
{

  string last = "Washington";
  string first = "George";

}

int main ()
{
  firstPresident ();
  cout << first << " " << last << endl;
  return 0;
}

If anyone can help me, I'd appreciate it. Before I kill everything in sight.

Edited by Supernature, 14 July 2010 - 09:26 PM.


#2 ShadowLink64

ShadowLink64
  • 16735 posts


Users Awards

Posted 14 July 2010 - 09:40 PM

Alright, there's some grave errors in your code. :p First off, your function "firstPresident" isn't returning anything. All it's doing right now is taking in "first" and "last" from somewhere, and then reassigning it "Washington" and "George" and then not doing anything with it.

Secondly, with the way you defined your variables, "first" and "last" only exist in the scope of the function firstPresident. Once that function's done, "first" and "last" don't exist anymore (so you're couting something that doesn't exist).

Thirdly, "string" is a special variable type in C++ that doesn't exist without including a library, so you need to include <string>.

In my example I combined it into a single variable (since spaces are allowed in strings).

First, let's rewrite firstPresident to return "George Washington":
string firstPresident()
{
  string name = "George Washington";
  return name;

}


To cout the result of a function,which in our case is the string "George Washington", store the result of the function in some container and display it. You weren't storing the result of firstPresident() anywhere, but below I am storing it in "theName".
int main ()
{
  string theName = firstPresident();
  cout << theName << endl;
  return 0;
}

Put it together:

#include <iostream>
#include <string>
using namespace std;

string firstPresident()
{
  string name = "George Washington";
  return name;

}

int main ()
{
  string theName = firstPresident();
  cout << theName << endl;
  return 0;
}


#3 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 14 July 2010 - 11:34 PM

Ah sl, you basically did it for him :p.

You can do this by the way:
string firstPresident()
{
    return "George Washington";
}


#4 Supernature

Supernature
  • 87 posts

Posted 15 July 2010 - 08:22 AM

Hmm, this makes a lot of sense. I don't have to have any arguments after declaring my function?

#5 Dan

Dan
  • Resident Know-It-All

  • 6382 posts


Users Awards

Posted 16 July 2010 - 05:39 AM

Hmm, this makes a lot of sense. I don't have to have any arguments after declaring my function?


Nope - your method has no parameters so no arguments need to be passed in.

Methods can return a value to the caller. If the return type, the type listed before the method name, is not void, then the method can return the value using the return keyword. A statement with the keyword return followed by a value that matches the return type will return that value to the method caller. The return keyword also stops the execution of the method. If the return type is void, a return statement with no value is still useful to stop the execution of the method. Without the return keyword, the method will stop executing when it reaches the end of the code block. Methods with a non-void return type are required to use the return keyword to return a value.



#6 Faval

Faval
  • 637 posts

Posted 19 July 2010 - 06:41 AM

Only if you wanted to pass it by reference like so.

Void firstpresident(string fname, string lname)
{
fname = "George";
lname = "Washington";
}

int main ()
{
string fname, lname;
firstpresident(fname, lname);
cout << first << " " << last << endl;
return 0;
}

You wouldn't want to do it this way though as it strays from what your instructor wants and to be honest since it's your grade here :)

Edited by Faval, 19 July 2010 - 06:50 AM.


#7 aweroij

aweroij
  • 44 posts

Posted 19 July 2010 - 09:26 AM

Only if you wanted to pass it by reference like so.

#include <iostream>
#include <string>
using namespace std;

v
oid firstpresident(string& fname, string& lname)
{
fname = "George";
lname = "Washington";
}

int main ()
{
string fname, lname;
firstpresident(fname, lname);
cout << fname << " " << lname << endl;
return 0;
}

You wouldn't want to do it this way though as it strays from what your instructor wants and to be honest since it's your grade here :)


Edited by aweroij, 19 July 2010 - 09:27 AM.


#8 Supernature

Supernature
  • 87 posts

Posted 19 July 2010 - 02:23 PM

Thanks guys :)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users