What are all of the different types a function can return?

Pages: 12
Jul 6, 2013 at 9:14pm
Ok I changed it. How does it look now?

As for

1
2
3
4
int returnInt()
{
    return 10;
}


I dont want to show them a hard coded variable, what if the variable needed to be changed? using a name is better because the contents of a variable name can be changed, but I will show them your way too.
Last edited on Jul 6, 2013 at 9:20pm
Jul 6, 2013 at 9:21pm
You should show them something semi-useful of each data type maybe?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

const int add( const int &x , const int &y ) //I suggest using reference so you don't create a copy
{
    return( x + y );
}

int main()
{
    int x , y;
    std::cout << "Please enter two values." << std::endl;
    std::cin >> x >> y;
    std::cout << x << " + " << y << " = " << add( x , y ) << std::endl;
}
Last edited on Jul 6, 2013 at 9:21pm
Jul 6, 2013 at 9:32pm
I dont want to show them a hard coded variable, what if the variable needed to be changed?


Err.... this:
1
2
3
4
5
int returnInt()
{
    int foo = 10;
    return foo;
}


... is just as hardcoded as this:
1
2
3
4
int returnInt()
{
    return 10;
}


And both are of equal difficulty to change later. You just change the "10" to something else.


giblit wrote:
I suggest using reference so you don't create a copy


For complex types like string, I'd agree. But passing basic data types like ints by reference instead of by value is more likely to worsen performance rather than improve it.

Copying the reference/pointer to x is just as expensive as copying x by value. Only now you also have to dereference a pointer/reference in the function to access the data which is slower.
Jul 6, 2013 at 9:33pm
Ah I see thanks.
Jul 6, 2013 at 9:36pm
"Err.... this:"

1
2
3
4
5
int returnInt()
{
    int foo = 10;
    return foo;
}


but what if I wanted to return a random value while using the program? the user has no access to the source so what i meant was what if someone did something like this:

1
2
3
4
5
6
7
8
9
void function()
{
    int number = 0;

    cout << "Enter a random number" << endl;
    cin >> number;

    return number;
}


thats what i meant. Also i indented all of the code so it would be easier to read.
Jul 6, 2013 at 9:37pm
Well clearly that function is totally different and therefore needed to be rewritten regardless. And yeah since you're returning a variable value you need a variable.

But if you're just returning a fixed value you don't need to assign it to a variable first because it's fixed.
Last edited on Jul 6, 2013 at 9:39pm
Jul 6, 2013 at 9:45pm
"But if you're just returning a fixed value you don't need to assign it to a variable first because it's fixed."

Yes I made a point of both a fixed value and one that will change in the tutorial so they will know.
Last edited on Jul 6, 2013 at 9:45pm
Jul 6, 2013 at 9:54pm
Ok i changed the ending part, i think its good now. How does it all look now? That should be it. Now i can move on to arrays
Last edited on Jul 6, 2013 at 9:55pm
Jul 7, 2013 at 1:36am
I don't understand the purpose of the `passing variables to other functions and from main' part, just generalize from the previous section.

Also, as soon as you introduce prototypes you can show how to work with several source files.
Jul 7, 2013 at 3:25pm
I want to show them how to do everything, I dont want any generalizing, or anything. I dont want to assume anyone knows how to do anything, thats how the people who taught me thought and i ended up paying for it in the end, i should know all of this stuff by now and tons more but i dont because people just assume you should be able to figure it out but I dont work like that so i want to make this tutorial for everyone.
Topic archived. No new replies allowed.
Pages: 12