MASTERY11MASTERY12

(The credit of the background images goes to https://flic.kr/p/aCdfY2 and https://flic.kr/p/bk27BD )

Hey there again, welcome to the merge of Mastery #11 and Mastery #12.

In this post, I’m going to show you how to create and call a function.

Fist of all, let me tell you that a function is “A named sequence of statements that performs some useful function. Functions may or may not take parameters, and may or may not produce a result” (Allen, 2009).

The structure of a function is:

type NAME (parameters) {

Statements

}

You can use functions of any type, but always remember to declare the type before the name of the function. As for the name, it can be named like you wish; however, it can’t be the name of a variable of C++.

Here’s the example video:

As you can see, I made I mistake. I wrote “return 0; ” inside the void function and you can’t do that because void are types that return no values, so be very careful. Also, in the int function, you can observe that the return value is “s” instead of “0”, that is because in this case, we want a value to be returned.

Also, remember to create the function before the int main and call the function inside the main!!!

Reference:

Allen B., D. (2009). How to Think Like a Computer Scientist.  Retrieved from http://greenteapress.com/thinkcpp/

CC BY 4.0 MASTERIES #11 & #12: Calling and creating Functions by Ana Gloria Angulo is licensed under a Creative Commons Attribution 4.0 International License.