You are on page 1of 7
Figure, 00-WHILE St Similar to the WHILE statment, the initialization occurred outside of the actual looping statement. Notice that the body of the loop Is located on top of the condition, From the structure itself, we can easily notice that even during execution, the statements within the bady are executed first. After which the condition is evaluated, checking whether to continue the loop or stop itll, togethor. What does this suggest? In the above example, we cannot easily distinguish the effects ofthis kind of approach. However, ifwe change the value of our countor {to something greator or equal to the constant 9, there we will notice the big difference. Both in WHILE and FOR, the body will not be executed since the condition is evaluated frst which evaluated to true, Butin DO WHILE, the body will be executed regardless ifthe condition evaluates to true later on, By the time the condition Is evaluated, the counter {has already been incremented/ decremented. Controlling Loops Since the start of the module, when dealing with looping statements, everything has boon linear and straight forward. However, this Is not always the case when we implement our solutions, In C++, we have two controlling statements which can change the flow of loops at any part ofthe iteration. ‘These are the BREAK and CONTINUE statements ‘The BREAK statements terminate the execution of the entire loop that Its encased, This is placed anywhere within the body of the loop. Any statements Recall In the provious module, we discussed mainly about arrays. We discussed hhow the computer allocates the appropriate space an application needs to perform its tasks, We alsa identified the syntax of an array, composed by: (3) datatype, (b) variableName, and (o) siz We also discussed the different dimensions for the array, namely: (2) one-dimensional array, {(b) two-dimensional array, (e) throe-dimensional array, and [d)soon, Lastly, we were able to roughly compute the necessary space needed for the application to use its declared variables and arrays. Introduction to Functions/Procedures Code blocks are generally designed to be written inside the int main() code block. This way, we look at our code sequentially, that means we never lef the int main() code block, This s the time to introduce functions. Functions: ‘are a separate code block which performs one specific task. These separate ‘code block can accept inputs from the other code blocks. In addition, they can, algo return back values which can be a result of the actions performed within ‘he function. ‘We are all very famallar with the most famous function in the entire C++, the int wain() code block. The int mzin() function, as we have mentioned several times throughout the course, is the entry point of any C++ application. Itis here where we place the entire logic of our application. Like ‘tho name nain suggests, It isthe main function of the entire C++, Components of Functions [A function is composed of several components which are similar to different ‘programming languages. These components build the fundamentals of a funetion which are necessary to completely differentiate and define the role ‘ofa function from the rest of the other code blocks. Also, these components ‘build up the function's properties and capabilities which are exploited not only by the developer of the application, but also other developers who uses several snippets of the source code. These components are: = ‘hat 0 Fallow? (a) return type (H) function name © parameters (@) body of the function ‘The return type is the data type which the function shall return as its final execution. The return type can be any of the defined data types discussed throughout the course as found in Table 1. pene Int Integer type Short ‘Short Integer type Long Long Integer type Bool Boolean type Float Single lating point type Double Double floating point type Char Character type Void No return value Tile mye Table 1 covers the fundamental data types in C++ with the addition of the void return type, The void return type simply tells the application that the function shall not return any data, You can modify the int main() function to have a void return type. Simply change the int return type to a votd return type and remove the return hin the new void main() funciton. ‘The function name is the alias which will be used to as reference te the function, Its similar to variable names. The only difference is the presence of the parentheses after the function name. ‘The parameter is the component ofa function which enumerates the input values of the function. The parameter Is placod Inside the parentheses of the function, right after the function name. The syntax for the parameter is dateType variableName. A function can support more than one parameter. Should that be the case, the parameters inside the parentheses are delimited byacomma. ‘The body of the function is the group of statements within the code block of the function, Regardless of the design of the fiction, ifthe return type is not void, the last execution statement should be return value,rerurn vartableNameorreturn expression Functions are also called Procedures. Quitk Tips, Variations of Functions/Procedures Functions can be arranged depending on the need of ft and its function. C++ does not discriminate the use of functions, Hence, any function can be defined any number of mes. Void Function without Parameter Figure 1 Youd Funcuon without Parameter Figure 1 shows a source code where a void function printtlsLLohlor1é() is defined, Also notice that aur defined function is placed before the nain() function, The reason behind this approach is to let the function be defined prlor itis called. Let us say that the nain() came before our printHeliovior1d() function. By the time our printHellokor1d() function is called inside the nsin() function, our function is yet to be defined, Hence, the ‘main function will throw an exception stating that the pritil=! lollor!d() is not defined. Void Function with Parameters Figuro 2 shows a source code where a vold function pr-intSun() Is defined ‘with two parameters; two integers 2 and bNotice that within the printSun() function, we simply displayed a text stating that we are adding ‘the two integers in the parameter, We called our printSum() function within ‘the nain() function and we passed the values 91 for parameter sand 23 for parameter b. Quick Tips When passing variables into the parameters and not constant values, the evariables themselves. The implication of voriables pass their values and not! this fs the fmmueabily of the original vartabies, Hence, after the original variables were passed, ther values even when changed Inside another function will never be affected, Int Function with Parameter gure. tt Function with Parameter Figure 3 shows a source code where anint function sum() is defined with two parameters; two Integers a and b, This time, we added a return type of int Hence, the function must return an integer value back to wherever itis called. The only statement within the sun() function is a return statement ‘which retumes the sum between the two integer parameters, in addition, inside the moin() function, we called our sun) function asa supplement to ‘our cout stream. Honce, we displayed right away the value returned by the sum() function, Int Function without Parameter Figure 4 int Paneuen without Paramster Figure 4 shows a source code where an int function getFaveNunber() is defined with no parameter. The function simply returns a constant back to whatever called it. this case, stwas called within the m=in() function. However, in this case, the value returned by the getFavetlunber() function is stored to an Integer » Inside the m2in() funetion.

You might also like