Wednesday, March 4, 2015

Post #4 - Functions!

Functions are quite fun (they do require getting used to remembering the prototype and function call, as well as the function itself) because they can drastically reduce the main section of a program to just a few statements, as the function calls do the work instead of the main program, allowing you to focus on other things in the program besides something simple like user input or output display. Functions also can help narrow down to one function's elements or operations instead of looking through the entire program for what caused the glitch.

An example - here is the program from before, except now everything is condensed into 3 functions corresponding to the 3 tasks of the program - (1) prompt for input, (2) display the input, (3) ask user to continue. Everything looks neater and easier to read! ^_^


Of course, for functions to work they need three presences in the program.
  • The function prototype is seen by the compiler before main. It include the datatype the function will return to main (if no type is returned, void is used instead), the name of the function, and the datatypes of the variables the function will be receiving from main. The names do not have to match those within main and can be omitted, since it is the simplified form of the actual function which is added later. If the prototype is omitted, the compiler will not match the function call with the function definition, and will see the function call as undeclared and the program will not work.
  • The function call is the only part of the function appearing in the program. Since its datatypes of its variables and its return type are already defined by the function definition and the prototype, they should not be included in the function call (over-definition). The variables passed to the function must use the names given in main, because the function will use the values in main for computations or to set values for them. Obviously these are necessary, without a function call your function doesn't do anything!
  • The function definition comes either before or after main and is the primary body of the function. Here all the work the function does is clearly defined, and any result to be sent back to main is returned with return. If a result is expected to be returned, main should have a variable ready to store the returned result, or a cout statement displaying it in main. A function of type void can be displayed with cout but should not be assigned to a variable, since it is not a variable type. void is best used for functions that only assign or display values and do not do computations, like Display()or Input().
Following these three guidelines it's easy to write custom functions for a variety of computations. Functions can even be put inside other functions or called using a function (the latter does not work with void functions). In the above example, DisplayInfo could be put inside of UserInfo because they use the same strings as parameters and only differ in handling of the strings - one stores input while the other displays it.

void UserInfo(string& a, string& b, string& c)
{
cout << "Enter your name: ";
getline(cin, a);
cout << "Enter your city: ";
getline(cin, b);
cout << "Enter your country: ";
getline(cin, c);

//Function call to DisplayInfo inside of function UserInfo
DisplayInfo(a, b, c);
}

//DisplayInfo keeps its original code below, but can be omitted in main above since it is called here instead.

A note about & (passing by reference):
If & (ampersand) is placed at the end of the datatype, the variable can be externally modified by the function, a copy of the variable's location is not passed to the function as it is in passing by value (without a &). This shows why each string with & had its contents changed when data was input into each one using UserInfo(), the ampersand allowed data to be input directly into the strings passed from main, and they were then passed by value to DisplayInfo(), which displayed a copy of the strings (since it did not need to modify them). If no ampersand was included in the first function, DisplayInfo() would have been passed blank values, because passing by value does not change the values of the strings in main but the values of a copy of each string, which are held in different locations than those in main. In my opinion, if the values are going to be changed and used in main again, it's better to pass by reference to assign them their new values, and then pass them by value to not change what they were set to by the first function.

No comments:

Post a Comment