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

Pages: 12
I am making a tutoral and i have

int
float
double
string
bool
time_t

so far, is that all? I would like to know anything and everything else that can be returned from a function please.
Last edited on
void and any class Object ( custom made or standard like std::vector )
char
short
struct
pointers to any of the above.
ostreams
reference to any of the above?
*ifstreams*

** not sure about that one..

****EDIT****
Don't forget iterators also.
Last edited on
ok aweome thank you, what about time_t? also can i have a small example of passing a values between 2 functions? here is some code. I want to pass string to number function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

using std::endl;
using std::cout;
using std::cin;
using std::string;

int returnInt(int);//Defining and integer type.
string returnString(string);

int main()
{
    //Initialize our variables
    int num = 5;
    string str = "string";

    cout << returnInt(num) << endl;
    cout << returnString(str) << endl;

    cin.get();
    return 0;
}

int returnInt(int my_number)
{
    my_number = 10;

    return my_number;
}

string returnString(string my_string)
{
    my_string = "hello this is a string";

    return my_string;
}


also how would i pass a variable from main to a function.
Last edited on
in a few words, a function can return anything that can be instantiated:
- class
- struct
- built-in type
- pointer to any of the previous
- reference to any of the previous (this lets you use the function as an lvalue)
- const version of the previous.(not sure if this is accurate)
- the memory allocation functions return a void* value.

like i said, basically anything that can be instantiated can be a return value.

you can think of a function like an rvalue variable which value is defined in terms of the arguments.

the return value is used in the expression that called the function then discarded, so if you don't use it, it will be ignored.
I am making a tutoral


Not to sound like a jerk... but how can you expect to teach someone through a tutorial if you don't fully understand the topic yourself?
Because thats how i learn, also if i learn it and write it down then i can go back and look at it myself if i need help. Thats part of the reason im writing these tutorials. Im writing them to help others and to help myself not only learn but so i dont forget. I understand the concept of passing by value and reference i just forget how to pass them to other functions and how to pass them from main. I used to know how to do it but i havent dont it in like a year so i forget.
Last edited on
Okay that's fair. =)
=)
I'm almost done with the functions tutorial all i need is to know how to pass variables to other functions and pass them from main to a function and that should be it. Im making my tutorials long but very informative so even a 3rd grader could understand them. At least thats what i hope.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
void function_2( int& );
void function_3( int& );
const int function_1( int &val )
{
    if( val != 1 )function_2( --val );
    return( val );
}
void function_2( int &val )
{
    function_3( ++val );
}
void function_3( int &val )
{
    function_1( --val );
}

int main()
{
    int apple = 4;
    std::cout << function_1( apple ) << std::endl;
}


Is that what you mean by pass variables to other functions then pass to main( guessing with a return but also the reference does that ).
The const qualifier for function_1's return type is pretty pointless. It's an rvalue, so you can't do function_1(3) = 5; even if the return type wasn't const. It would be better suited for return types of constant references.
yes, but can i pass a variable to main from a function?
Last edited on
Isn't that what you are doing in your original example code?
18
19
    cout << returnInt(num) << endl;
    cout << returnString(str) << endl;


What exactly do you mean by "pass a variable?"
sorry i meant from main to a function.
Last edited on
nevermind i got it, i confused myself but it worked i just did this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::endl;
using std::cout;
using std::cin;

void GetFromMain(int&);

int main()
{
    int send = 300;

    GetFromMain(send);

    cin.get();
    return 0;
}

void GetFromMain(int &num)
{
    cout << num << endl;
}

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

void funky(int var){std::cout << "Before: " << var << std::endl;} //Like what you did
void funky2(int& var){var += 10;} //Like giblit's example

int main(){
   int myVar(5);
   funky(myVar); //Passing a variable (myVar) from main() to funky(int)
   funky2(myVar); //Similar to above but now the original myVar is accessible from within funky2
   std::cout << "After: " << myVar << std::endl;
   return 0;
}


?
ok i think its complete can someone look it over and tell me what you think? tell me if theres any code erros or anything thats not quite right or anything

https://sites.google.com/site/cplusplusforcompletebeginners/home/lesson-one---getting-set-up/lesson-four---functions
Hah, your tutorial is way more detailed than my personal notes. Not bad, man.
I skimmed. I noticed some weirdness:

At the very end of the tutorial:
1
2
3
4
5
6
7
8
9
10
11
12
13
int returnInt(int my_number)
{
    my_number = 10;

    return my_number;
}

string returnString(string my_string)
{
    my_string = "hello this is a string";

    return my_string;
}


There is no reason for these functions to have parameters. The parameters are not used for anything. For example... returnInt could be easily changed to the following to eliminate the need for a parameter:

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


Or better yet:
1
2
3
4
int returnInt()
{
    return 10;
}



If a parameter is not being used for input/output for a function, it should not be there.


EDIT:

You'll notice that removing the parameters also means you can remove the 2 otherwise useless variables in main.

This:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    //Initialize our variables
    int num = 5;
    string str = "string";

    cout << returnInt(num) << endl;
    cout << returnString(str) << endl;

    cin.get();
    return 0;
}


becomes this:

1
2
3
4
5
6
7
8
9
10
int main()
{
    // (no need for variables because we're not using any)

    cout << returnInt() << endl;
    cout << returnString() << endl;

    cin.get();
    return 0;
}
Last edited on
Pages: 12