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

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/feed-rss2.php on line 8
‘#functions’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Thu, 29 Oct 2015 01:27:21 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ WSQ08 https://kenscourses.com/tc101fall2015/2015/wsq08-32/ Thu, 29 Oct 2015 01:27:21 +0000 http://5nbppkkyj.wordpress.com/?p=105 ]]> 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 called according to the user’s needs.

Screen Shot 2015-10-15 at 7.24.49 PM

This is ontofunctions.py in action!Screen Shot 2015-10-15 at 7.27.30 PM

]]>
https://creativecommons.org/licenses/by/4.0/
Creating C++ functions and calling them. https://kenscourses.com/tc101fall2015/2015/creating-c-functions-and-calling-them/ Wed, 28 Oct 2015 05:02:09 +0000 https://myfreakingcrazythoughts.wordpress.com/?p=147 Continue Reading →]]> 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 a name, and which can be called from some point of the program. The most common syntax to define a function is:

Type name (parameter1, parameter2,) {statements}

Where:

type is the type of value returned by the function.

name is the identifier by which the function can be called.

parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: Int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.

Statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.

 

Types of functions:

System-Supplied Functions:

These are available to anyone who writes a C++ program. This saves the programmer’s time from writing own functions. They are completely debugged, efficient and always produce a precise output. The important contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. It reduces the source code, which saves time of the programmer.

User-Supplied Functions:

C++ language allows additional functions besides the built-in functions called the user-defined function. It allows programmers to define their own functions. The programmer must code the logic of this type. In order to do so, a declaration is needed.

Calling a function:

While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.

This is an example of a code where I created a function and then I called it into my main.

//Function that calculates the area of a rectangle
#include <iostream>
using namespace std;

//Declaration of what the function does
void area(){
int l=0, w=0;
cout<<"Please enter length and width: "<<endl;
cout<<"Length: ";cin>>l;
cout<<"Width: ";cin>>w;
cout<<"The area is: "<< l * w<<endl;
}

int main(){
cout<<"I will calculate the area of a rectangle"<<endl;
area();//Calling the function inside the execution of the program

}

And this is how the program runs:

functions execution

Any doubt please leave it in comments and I will be glad to answer.

-The admin.

]]>
https://creativecommons.org/licenses/by/4.0/
Creating C++ functions and calling them. https://kenscourses.com/tc101fall2015/2015/creating-c-functions-and-calling-them-2/ Wed, 28 Oct 2015 05:02:09 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=147 Continue Reading →]]> 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 a name, and which can be called from some point of the program. The most common syntax to define a function is:

Type name (parameter1, parameter2,) {statements}

Where:

type is the type of value returned by the function.

name is the identifier by which the function can be called.

parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: Int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.

Statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.

 

Types of functions:

System-Supplied Functions:

These are available to anyone who writes a C++ program. This saves the programmer’s time from writing own functions. They are completely debugged, efficient and always produce a precise output. The important contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. It reduces the source code, which saves time of the programmer.

User-Supplied Functions:

C++ language allows additional functions besides the built-in functions called the user-defined function. It allows programmers to define their own functions. The programmer must code the logic of this type. In order to do so, a declaration is needed.

Calling a function:

While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.

This is an example of a code where I created a function and then I called it into my main.

//Function that calculates the area of a rectangle
#include <iostream>
using namespace std;

//Declaration of what the function does
void area(){
int l=0, w=0;
cout<<"Please enter length and width: "<<endl;
cout<<"Length: ";cin>>l;
cout<<"Width: ";cin>>w;
cout<<"The area is: "<< l * w<<endl;
}

int main(){
cout<<"I will calculate the area of a rectangle"<<endl;
area();//Calling the function inside the execution of the program

}

And this is how the program runs:

functions execution

Any doubt please leave it in comments and I will be glad to answer.

-The admin.

]]>
https://creativecommons.org/licenses/by/4.0/
Masteries 11 and 12 – Calling and creating functions https://kenscourses.com/tc101fall2015/2015/masteries-11-and-12-calling-and-creating-functions/ Sat, 24 Oct 2015 14:07:00 +0000 http://kenscourses.com/tc101fall2015/?guid=e37b1c48ffe99a41e413b14ea56d15f4

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


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


]]>
https://creativecommons.org/licenses/by/4.0/
Quiz06 https://kenscourses.com/tc101fall2015/2015/quiz06-20/ Sun, 11 Oct 2015 18:29:02 +0000 http://5nbppkkyj.wordpress.com/?p=110 You can see my code here.

This is my code in action!
Screen Shot 2015-10-11 at 1.23.47 PM

]]>
https://creativecommons.org/licenses/by/4.0/
QUIZ 06 https://kenscourses.com/tc101fall2015/2015/quiz-06/ Fri, 09 Oct 2015 00:19:32 +0000 http://delusionalpieceofdust.wordpress.com/?p=295 ]]> 2376875927_04f3977a62_o

(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:

  1. https://github.com/anagloriaac/QUIZ06/blob/master/q1.cpp

q2 q22

  1. https://github.com/anagloriaac/QUIZ06/blob/master/q2.cpp

q1

]]>
https://creativecommons.org/licenses/by/4.0/
QUIZ 06 https://kenscourses.com/tc101fall2015/2015/quiz-06-15/ Fri, 09 Oct 2015 00:19:32 +0000 https://delusionalpieceofdust.wordpress.com/?p=295 ]]> 2376875927_04f3977a62_o

(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:

  1. https://github.com/anagloriaac/QUIZ06/blob/master/q1.cpp

q2 q22

  1. https://github.com/anagloriaac/QUIZ06/blob/master/q2.cpp

q1

]]>
https://creativecommons.org/licenses/by/4.0/
Quiz #6 – And now, You call it Madness. https://kenscourses.com/tc101fall2015/2015/quiz-6-and-now-you-call-it-madness/ Thu, 08 Oct 2015 15:44:39 +0000 http://andressava.wordpress.com/?p=30 ]]> 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 a long type. There’s a lot of methods you are able to, but the easiest one is with the library <cmath>, where you are able to call a function(wow) called ‘pow(x,y)’, that makes the power of two numbers, that’s it.

And for the second part, you have to make a function called stars where you input the value of the length of a chain of stars, as said previously theres a lot of methods but the easiest one is with a for( ), below i will paste a link to the code for the Quiz #06.

Thanks for reading.

github Code for Quiz #06
https://github.com/AndresSV/TC1017/blob/master/Quiz%20%2306%20.cpp

flickr photo by Craig Sunter
https://www.flickr.com/photos/16210667@N02/13973604274
Creative Commons Public Domain

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ08 – On to functions https://kenscourses.com/tc101fall2015/2015/wsq08-on-to-functions-5/ Tue, 06 Oct 2015 13:36:00 +0000 http://kenscourses.com/tc101fall2015/?guid=ec8699278d0886254f7542cb94408288 This is my WSQ08
This 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 working:

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

]]>
https://creativecommons.org/licenses/by/4.0/
This activity has to have the same result as WSQ03, but with… https://kenscourses.com/tc101fall2015/2015/this-activity-has-to-have-the-same-result-as-wsq03-but-with/ Mon, 28 Sep 2015 17:30:39 +0000 http://bsolisale.tumblr.com/post/130071325015

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.

]]>

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.

]]>
https://creativecommons.org/licenses/by/4.0/