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
Jose Carlos Peñuelas Armenta’s Articles at TC101 Fall 2015, Page 3
Introduction to Programming Python and C++

Masteries 11 and 12 – Calling and creating functions

Functions in Python are very easy to use, they do specific actions with the value you call them. The function substitute a value into a block of code and the returns the calculation (or sometimes they do nothing). For example, you have probably used before the print function:

Print(“Hello world”)
This funtion is a built-in-function that takes an integer or a string (or a combination of them) and prints them for the user. There are a lot of built-in-funtions but we are not going to ge into all of them, the way to recognize a function is to see a word and then a value(or values) between parenthesis.
Name_function(value)
Name_function(value,value1,value2)

Calling a function

In order to work all functions need to be called. You can just write the name of the function with some values inside but that is just going to calculate whatever the function do without responding to the user. If you want to show the calculation to the user you can include the function in a print function or asign the value of the calculation to a variable:
superpower(2,3) #Gives you the value of x raised to the y
nothing happens
print (superpower(2,3))
8
value = superpower(2,3)
print (value)
Keep in mind that if you want that the function takes a value and return it (adquire the value) you need to put the word return inside the function. If you dont do this the function will do nothing and you wont b able to use it for more calculations. 

Creating a function

Now that you know how to call a function you can create one. The stepts are really simple. Fisrt lets see the body of a function:
def function_sum(x,y,z):
     return x+y+z
As you can see to create a function we need to tell Python that we want to make one so we write def, this way Python will know that you are creating a function and not calling it. The next step is to write the name, you can use whatever name you want but keep in mind that if you are in a big project you are going to use a lot of functions so use names easy to remember. I recommend you to name the function with something related to it like:
def root_square(x) #It calculates the root square of a number
def pyramid(x) #It prints a pyramid made of strings
def fibonacci(x) #It prints the value of n in the fibonacci sequence
The next ting to do is to write the parameters that you want the function to take, it can be anything as long as you use them inside the function. It works as a variable but they are not a variable, it just sustitute the values inside the function. Look at the first example and you can see that I used 3 parameters (x, y and z) and later on I used them inside the function to retunr the value of a sum of the 3 parameters. Lets see some examples of the function working:
print (function_sum(1,2,3))
6
print (function_sum(10,4,16))
30
Remember to use the return so that we can use it as the value of the calculation
function_sum(1,2,3) + 10  #6 + 10
nothing 
Remember that we need to print it or asing it to a value. Otherwise the calculation will be useless.
print (funtion_sum(1,2,3) + 10)
16

Notes

-While creating the function remeber to use the : at the end and then put the code inside the function with identation
-Remember that most of the time the function needs the return
-You can use as many parameters as you want
-While calling the function remember to give the value some use, print it or asign it to a variable


WSQ10 – Lists

This is the program working. I took some code from Paul´s blog https://pololarinette.wordpress.com/2015/10/15/wsq10-lists/ because i had some issues understanding the formula of the standart deviation so what i did was o import a library that already included the formula and i used it for the calculation.

If you want to see the code here is the link to Github:
https://github.com/Jocapear/TC1014/blob/master/WSQ10.py

Quiz08

This quiz made me think a little, I had problems first at creating the list and ask the user when to stop but I solved it in the end.The link to my quiz on Github is this:https://github.com/Jocapear/TC1014/blob/master/Quiz08.py

Quiz 07

My quiz is in github on the next links (both activities are in the same file)https://github.com/Jocapear/TC1014/blob/master/Quiz07.py

Quiz 06

Hello here is my code: And here is the second:If you want to see my code on Github i will post the link here:https://github.com/Jocapear/TC1014/blob/master/Quiz06

WSQ09 – Factorial calculator

This WSQ was kind of tricky for me, im kind of new into loops so I need a little bit more of practice with them. I had to look for a factorial number on google because i didn´t know what a factorial number was.

 This is the program working:

If you want to get the code from Github here is the link:
https://github.com/Jocapear/TC1014/blob/master/WSQ09.py

WSQ08 – On to functions

This is my WSQ08This one was really easy, its the basic use fo a function, nothing hard or complex. I used a lot of functions on this excersise but I think that it could be done with only one function. Here is my code: And this is the program work…

Mastery 19 – Use of loops with While

I know how to use loops, its really simple. I will show you a WHILE loop that I used in the WSQ07:This loop makes posible the constant repetition of one statement, it can also work with variables and numbers thats why I put the counter inside. The WHIL…

Masteries 15 and 16 – If and Else

Here I will show you that I know how to use IF and ELSE stamentes.This is the basic form, I put it simple to understand the syntax quickly. Here are some works that I made using IF and ELSEs.If you want to know more check my WSQs.

Mastery 07 – Use of comments

Look now I know how to write comments in Python 3:

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).