Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
‘#mastery11’ Articles at TC101 Fall 2015, Page 3
Introduction to Programming Python and C++

Tag Archives: #mastery11

Masteries 11 & 12

On these masteries I’m going to show you what are functions

in C++

,how to create one, and how to call it in your program,

Functions allow you to create “mini programs” inside your code in order to make it shorter (and better).

So, let’s recall what we did on mastery 10We will practice with the same concept, the addition of two numbers, but now we will do it with functions.

The big difference with using functions in your program is that you need to write them before our “basic code” which is the int main (). 

So, what you will need to do first is our “basic code”:

Now, if we would want the program to add two numbers that the user inputs, we could write the code inside int main (), declare the three variables (x being the first number the user inputs, y being the second number, and z being the result), then ask the user for these two variables, and then simply add them like this:

And then if we run the program, it will work like this:

And that just works fine, but it will work and look better if we use functions, and as I told you before, functions are written before int main (). So, do as follows:

  1. Write an int function called addition below “using namespace std;” Inside the parentesys you should declare your variables
  2. Return an operation that adds your two variables.

Your funtion should look something like this;

Now that you have your function, you can call it inside int main (), but you need to declare the variables inside here too. So, do as follows:

  1. Declare your two variables inside int main ()
  2. Ask the user for these two variables.
  3. Call your function using cout.

Your program now should look something like this:

Masteries 11 & 12

Now let’s run and test it:

And that’s about it! You know now how to create a function and how to use it, congrats!

Creating and Calling Functions

     A function is a piece of code that has been written some time before it is being used. Some of these already come with python just as the case of the “Random” function that has been used in the following example:



     These functions can be created by the programmer in the same code in which he/she will be using it.


      To create a function we need to start defining it. To define it we write “def” followed by the name of the function and the parameters that will be used in this between parentheses.


     The next line of code needs to be indented as this is what the function is supposed to do. This function will print the primary colors:



      If we try to run this program, this is what will happen:


       As you can see, this program does not print anything. The reason for this is that we have successfully created a function; however, we never called it. So this program knows that it has to print “Red, Blue and Yellow” at some point, it just doesn´t know when.


      So we need when we want the function to be called. In this case the function will be called if the following condition is true:


Creating and Calling Functions

       Note that the line in which we state the condition is not intended. This means that this like and what is followed by it is no longer part of the function we called “Colors”.


       In this program, the user is asked if he/she wants to know which are the primary colors and it receives an input in the form of a string. If the string it received is “yes”, then the function is called. This is what will happen if the conditions stated below are true:



        This is another example of a created function. This function has an argument called “x”. This argument is the name of a person. The function outputs the text “My name is” followed by the value given to “x“.


        Before calling the function we need to define what will be the value of “x”. After that, we call the function by typing the name of the function followed by parentheses.

      After running it, this is what will happen:


        We can also change what the function will print by changing what is being witten between the parentheses. For example, if instead of typing “name(x)” we type “name(x*4)”.

        The argument this function will be taking as “x” is not “Frida”; it is “Frida”*4, which is “FridaFridaFridaFrida”. This is what will happen:



       Another way to create a function is the one shown below:

        The variable “stars” was defined inside the function; however, it can only be used inside the function. For printing variables that were defined inside the function we need to type “return” followed by the name of the variable at the end of the function.


       This way, the program can use the variable correctly even if it is inside a function:



        As you can see, this program asks the user for a number, then it print the same quantity of “*” that the user stated.


        If we decide to omit the “return” part, the result will be totally different:

Creating C++ functions and calling them.

In this lesson I will show you how to write and call a function in order to   simplify the structure of any program. Creating a function: A function in C++, a function is a group of statements that is given… Continue Reading →

Creating C++ functions and calling them.

In this lesson I will show you how to write and call a function in order to   simplify the structure of any program. Creating a function: A function in C++, a function is a group of statements that is given… Continue Reading →

#Masteries 11 and 12

Mastery 11. Calling C++ functions… Mastery 12. Creating C++ functions… And here my video:

#Masteries 11 and 12

Mastery 11. Calling C++ functions… Mastery 12. Creating C++ functions… And here my video:

Masteries 11 & 12

Masteries 11 & 12

#Mastery11 and #Mastery12

Hey today we are going to learn how to create and call functions in Pythons 3 : Calling Python functions 2. Creating Python functions #Mastery11 and #Mastery12

#Mastery11 and #Mastery12

Hey today we are going to learn how to create and call functions in Pythons 3 : Calling Python functions 2. Creating Python functions #Mastery11 and #Mastery12

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).