trying to wrap my head around functions
So I know a bit about functions, yet for some reason I can't get my head to wrap around them. I understand they're used to split your program up to make it easier to deal with it, however I'm not completely understanding their workings. I have a runtime error with a certain function, not sure why. I wrote some code to experiment different ways of passing a function and what not.
Code:
#include <iostream>
#include <string>
using namespace std;
void function1(void);
void function2(void);
int returnfunc(void);
void str(string prompt);
string strFunc(string); // Problem function
int main()
{
int num1;
int num2;
string strSent;
function1(); // Call function1 in main
function2(); // call function2 in main
num2 = returnfunc(); // num2 will grab the value that returnfunc returns within its function
cout << "The number you passed to main was " << num2 << endl << endl; // shows that nb1 successfuly returns a value and assigns it to the variable 'num2'
str("The prompt is within the () of this function"); // the message determined from prompt is defined here
cout << endl << endl;
cout << "Enter a sentence.\n";
cin.get();
getline(cin, strSent);
// Problem here
strFunc(strSent);
return 0;
}
void function1()
{
cout << "Calling this function in main() will display this stream." << endl << endl;
}
void function2()
{
int n1;
cout << "Enter a number -> ";
cin >> n1;
cout << "The number you entered was \"" << n1 << "\"" << endl << endl;
}
int returnfunc()
{
int nb1;
cout << "Enter a number to pass to main ";
cin >> nb1;
return nb1;
}
void str(string prompt)
{
cout << prompt; // will display whatever prompt you code in the main
}
string strFunc(string strsnt) // problem here
{
cout << strsnt;
}
Perhaps someone can elaborate more on them? Maybe some more practical approaches?