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.
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.
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.
=)
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.
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.
#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
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;
}