May 14, 2015

Week of 05/03/15 - C++ Part 4: Functions

    From previous C++ experience, I know that functions are series of statements. From experience, the program starts by running the main function. However, if there are multiple functions, the main function is still the only one that is run. This is unless you use a function call in the main function. A function call interrupts the main function by leaving a "bookmark" in the main function and then calls, or executes, the specified function. When that function terminates, the program goes back to the bookmark and continues running the main function. Also, function calls are made using the function name along with a list of parameters enclosed by parenthesis. I wanted to see if you could call functions within functions other than main(), but it doesn't look like you can, according to stackoverflow. Functions also can't be defined inside of other functions. For example, you can't define your function when it's in main(). You have to define it outside of main().

    The main function has an integer type, so in the end it return a number, usually 0. Other functions can have other types, though. We have the void type, where the function returns nothing. There's integer, there's also probably boolean, string, array, and possibly more.

    The website mentions the question "Can my function return multiple values using a return statement?". It says no, but it also says that there are workarounds for that. Probably arrays and parsing them after they are returned.

No comments: