English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have this worksheet to do and i get most of the stuff..except one question.

it asks "what's the difference between foo & foo1? how would u call foo1 and foo2 such that they both print 34?"

void foo()
{
int x=34;
cout< }

int foo1()
{
int x=34;
return x;
}

ok so basically. i don't understand the second question of that question at all. the first part..i understand..one is void and one is int. but they both actually PRINT 34. so...i don't get it. i don't really understand "return" i just don't see a difference because they both print 34. one just has return and one is cout.

2007-03-09 16:14:25 · 3 answers · asked by boom56 1 in Computers & Internet Programming & Design

hmm stuck on the last problem also. it says "declare implement, and call a function 'middle' that prompts the user to enter a string, remove the 1st character if length is even then return the middle character."

ok so far...i have

string middle();
string a;
int main()
{
cout<<"Enter in a string"< getline(cin,a);
if(a.length()%2==0)
a.erase(0,1);
a.length/2=b;
return b;
}

i know the a.length/2 is wrong..but..what i'm trying to figure out is how to get...the middle character to print.

i also still don't get return..what's the difference between return and cout

2007-03-09 16:22:39 · update #1

3 answers

To print void foo() from main type
foo()

To print int foo1() from main type
cout << foo1()

That's the difference.

Second part, to print a middle character.

use substring(starting position, for how many characters)

cout << a.substr(floor(a.length/2)+1, 1)

2007-03-09 23:51:18 · answer #1 · answered by Tasm 6 · 0 0

The difference really ends up being where the "cout" is called. To print "34" in the first function, you would simply call the function:

foo();

To print it in the second function, the code calling "foo1" will need to handle the "cout".

cout<
"foo1" doesn't print the value, it returns it to the line that called "foo1".

2007-03-10 00:23:11 · answer #2 · answered by Jim Maryland 7 · 1 0

"but they both actually PRINT 34"
Nope. Return statements don't print anything. They just give back some value.

int addthreenumbers(int x, int y, int z);

You can then do: int sum = addthreenumbers; sum contains the return value of the function. return doesn't print anything or do anything else. If you have a void function, it doesn't return any value.

2007-03-10 00:42:58 · answer #3 · answered by csanon 6 · 0 0

fedest.com, questions and answers