#WSQ08 Functions Beach

Hey so for this new assigment we had to do Fun With Numbers again, but this time we needed to create a function before we asked the user for the numbers; therefore the structureyou must follow is:

int add (int a, int b)
{
    int s;
    s=a+b;
    return s;
}

That’s the main structure for each function, you must do this for the substraction, division, and integer division, remember to change the letter or name for each function though.

Screenshot7.1.png

Then you can proceed to begin with the main program, where you’d be asking for the numbers who will be used  :

int main ()
{
int a, b, s, r, m, d, i;
cout<< “Give me a integer number” << endl;
cin>>a;
cout<< “Give me second integer number” << endl;
cin>>b;

Screenshot7.2.png

There you can see that we choose our functions created before, and showing that the integers which are going to be use are the ones choosen before; finally we print the answers using “cout” finally ending with the return 0; “

Heres is my code, in case you need it

 

 

WSQ08 ‘WSQ03 Version 2.0’

This ‘On To Functions’ WSQ is kinda weird. The instructions were the same as WSQ03 with only one difference: Separating the operations from the answer’s printing.

This is how WSQ03 looked like. And this is how it changed:

WSQ08code

It still runs the same in case you were wondering.

WSQ08run

This may be a short post, but the reason is that the changes were minimal. Well then, lets talk about a classic. “The Man Who Sold The World” by David Bowie. Not so long ago, this same man passed away. I always knew about this classic; however, I got the interest into it because of Metal Gear Solid V: The Phantom Pain. Even so it is a cover of this song, the lyrics match what ‘Venom Snake’ is going through. David Bowie gave this song a meaning of existentialism  because he portraits his two selves: the image he sold to the world and the his true self negating each other.

The Man who sold the world

#WSQ08 FUNciones.

En este programa es lo mismo que el WSQ03 (realizar operaciones con dos números) pero con funciones, es importante separar las funciones de todo lo demás.

 

for

-MIS CÓDIGOS-

for

LAS FUNCIONES SON IMPORTANTES PORQUE SI ES UN CÓDIGO MUY LARGO PUEDES USAR ESAS FUNCIONES YA DEFINIDAS EN VEZ DE REALIZAR LA MISMA OPERACIÓN OTRA VEZ.

Código:

def resta (num1,num2):
arit=int(num1-num2)
return arit

def mult (num1,num2):
arit=int(num1*num2)
return arit

def diven (num1,num2):
arit=num1//num2
return arit

def divres (num1,num2):
arit=num1%num2
return arit

# main program
print(“Este programa ejecuta las operaciones de resta, multiplicacion y division de dos numeros dados”)

num1 = float(input(“Ingrese un numero: “))
num2 = float(input(“Ingrese otro numero: “))

print(“Los resultados de las operaciones son: “)
print(“resta de “+(str(num1))+ “- ” +(str(num2))+”: “+str(resta(num1,num2)))
print(“producto de ” + (str(num1)) + “* ” + (str(num2)) + “: ” + str(mult(num1,num2)))
print(“Resultado entero de la division de ” + (str(num1)+” “) + “/ ” + (str(num2)) + “: ” + str(diven(num1,num2)))
print(“Residuo de la division de ” + (str(num1)) + “/” + (str(num2)) + “: ” + str(divres(num1,num2)))

 

On to functions

So this has been the easiest task and blogpost so far! They say that if you work hard at the beggining it will help you at the end, and it sure is paying off.

I mentioned all that because this program is exactly the same I had to do on #WSQ03, but now using functions. The cool thing is, when I did #WSQ03 I used functions in order to practice! So basically you´ll see the same program here. BTW, the image above is how this program looks when you run it.

Functions are really cool tools that you can use in order to put your program together in a more organized way. In order to use them, you have to build them first. You usually build them before the code inside main that “calls” the funtion, and before the main, which is a function as well actually.

To call the function, you just have to write the function name with a parenteses. Inside the parenteses you have to write the parameters, which are the values or information that the function is going to work with.

Here you can have a look at my code:

#include <iostream>

using namespace std;

int x, y;

int multiplication(int z, int p){

int operation = z * p;

return operation;
}

int divition (int z, int p){

int operation = z / p;

return operation;

}

int sum (int z, int p){

int operation = z + p;

return operation;

}

int subs (int z, int p){

int operation = z – p;

return operation;

}

int remainder (int z, int p){

int operation = z % p;

return operation;

}

int main(){

cout << “This program will do some basic operations with two numbers. Please provide the first number” << endl;
cin >> x;

code2#WSQ03
code1#WSQ03

Continue reading “On to functions”

On to functions

Welcome back!

Now I bring you a video explaining FUNCTIONS. I expect you can watch it and take notes of every single data that you can need.

Here’s the video:

Basically what I said in the video is that a function is a group of statements that is given a name which can be called from some point of the program. And a parameter can allow passing arguments to the function from the location where it is called from (http://www.cplusplus.com/doc/tutorial/functions/)

Here’s my code:

Haga click para ver el pase de diapositivas.

All the explanation is in the video. It isn’t a difficult topic so practice and you won’t have any problem.

Click here to see the code

This web helps me to understand more about the topic.

See you in my next post!

Top image by: http://chammeleon.tumblr.com/post/82031445772/compila-y-funciona

On To Functions

Esta práctica tuve que escribir una función para cada operación que realizaría el programa, de esta manera solo se pedían los dos números al usuario y automáticamente se muestra el resultado de las operaciones.

Me apoyé en este video para ver como se declara la función, espero les ayude 🙂

https://youtu.be/ZYCTqYvDEI8

Aquí dejo el link de mi código… 

https://www.dropbox.com/s/3tst51k2i5wo808/functions.cpp?dl=0

Screen Shot 2016-02-06 at 11.28.11 AM

Now with functions

#TC1017 #WSQ08

For this programm I had to go back to WSQ03, which just did some basic operations with two numbers, and step it up a notch by implementing functions.

I really like the way functions work, it might be a little easy lo loose track of what the compiler is doing with all of those variables, but once you get the hang of it it becomes very interesting to see all the things you can doy by passing paramaters onto other functions and using their results for something else.

The only problem I had when programming this was, once again, checking syntax of the functions. Everything else was donde smoothly.

functions.proof

Source code:

#include <iostream>
using namespace std;
double primero, segundo;

double suma (double a, double b)
{
int resultado1 = (a+b);
cout << “El resultado de la suma es: ” << resultado1 << endl;
}

double resta1 (double a, double b)
{
double resultado2 = a-b;
cout << “La resta del primer numero menos el segundo es: ” << resultado2 << endl;
}

double resta2 (double a, double b)
{
double resultado3 = b-a;
cout << “La resta del segundo numero menos el primero es: ” << resultado3 << endl;
}
double producto (double a, double b)
{
double resultado4 = a*b;
cout << “El producto de ambos numeros es: ” << resultado4 << endl;
}

double division1 (double a, double b)
{
double resultado5 = a/b;
cout << “El resultado de el primer numero entre el segundo es: ” << resultado5 << endl;
}

double division2 (double a, double b)
{
double resultado6 = b/a;
cout << “El resultado del segundo numero entre el primero es: ” << resultado6 << endl;
}
int main() {
cout << “Ingrese el valor del primer numero :” << endl;
cin >>

Continue reading “Now with functions”