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
‘#functions’ Articles at TC101 Fall 2015
Introduction to Programming Python and C++

Tag Archives: #functions

WSQ08

In this WSQ I have fun with numbers, again! I’m sure my code can be optimized but I tried to write it as clean i.e. readable, as possible. So yeah, all I did was print some options for the user and ask for two numbers to work with. Finally, I created some functions so they are […]

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 – 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


Quiz06

You can see my code here.
This is my code in action!

QUIZ 06

(The credit of the  image goes to https://flic.kr/p/4C37pr) Welcome to this special post. As you can see, it is a quiz. Here are my codes: https://github.com/anagloriaac/QUIZ06/blob/master/q1.cpp https://github.com/anagloriaac/QUIZ06/blob/master/q2.cpp

QUIZ 06

(The credit of the  image goes to https://flic.kr/p/4C37pr) Welcome to this special post. As you can see, it is a quiz. Here are my codes: https://github.com/anagloriaac/QUIZ06/blob/master/q1.cpp https://github.com/anagloriaac/QUIZ06/blob/master/q2.cpp

Quiz #6 – And now, You call it Madness.

Fun Quiz, a little bit tricky but fun, there’s a lot stress on this one, a lot people failed. But as Ken says it’s #OkToFail. Well back to business, in the first part of the quiz is make a function called “superpower” able to ask for two numbers and return the power of it on […]

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…

This activity has to have the same result as WSQ03, but with…

This activity has to have the same result as WSQ03, but with functions. It was not hard to do this activity, I didn’t have big problems, just some little details. But I did have one which I wasn’t expecting: I used the shortcuts of the operations as the first 3 letters to keep it simple. Product was “pro”, for example, but the problem came with the division, because “div” is used in C++ as a command, so when I tried to run in, Terminal read it as a command instead of a variable. When I realized that, I changed the variable for “divv” so it becomes a variable again. It ran perfectly after that.

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