WSQ05 – On To Functions

--Originally published at BRENDA

This was a simpler task than the ones before because it was based on the program I had already done of Fun with Numbers, but instead of simply doing the operations inside main (), I created a function for each operation: sum, substraction, multiplication, division and residue.  For this I learnt about the Mastery Topic #6 & #7, functions. I learnt how to do it by reading the book Ken provided (http://www.greenteapress.com/thinkcpp/thinkCScpp.pdf) and by the explanation Ken gave in class.

This is an example of how I created and named a function:

int mysum (int x,int y) {
return x+y;
}

And then I just put: cout<< mysum (a,b); inside main ()  to do the operation.

Here is the code:

#include <iostream>
using namespace std;
int mysum (int x,int y) {
return x+y;
}
int mysubstraction (int x,int y){
return x-y;
}
int times (int x,int y){
return x*y;
}
int divide (int x,int y){
return x/y;
}
int left (int x,int y){
return x%y;
}
int main ()
{
int a;
cout <<“Please enter A:”;
cin>>a;
int b;
cout <<“Please enter B:”;
cin>>b;
cout << ” A + B = “;
cout<< mysum (a,b);
cout << “\r\n A – B = “;
cout << mysubstraction (a,b);
cout <<“\r\n A * B = “;
cout << times (a,b);
cout << “\r\n A / B = “;
cout << divide (a,b);
cout <<“\r\nThe remainder of A/B is “;
cout << left (a,b) <<endl;
return 0;
}Captura2


Fun with Numbers and Functions (WSQ05)

--Originally published at TC1017 (Python3) – Titel der Website

Hello Guys,

this is the continuation of the WSQ01 (Fun with numbers).

The task here was, to calculate the numbers of User with functions.

To understand how Functions work on Python3 i found the following video.

 

Which i have not found is how to work with a return value.

 

The Program:

WSQ5 - SourceCode - FunWithNum2.PNG

 

The Result:

WSQ5 - Ergebnis - FunWithNum2.PNG

 

 

Enjoy.


Homework 05

--Originally published at Blog de Esteban

En este programa aprendí a usar las funciones, no sabia bien como funcionaban así que comencé a leer al respecto y a ver videos, ya que le entendí hice mi programa y salió todo bien, pero después se lo enseñé a Ken y me dijo que tenía un error, las funciones solo se usan para devolver valores y yo estaba devolviendo textos con valores, así que modifique mi programa y ya quedó perfecto.

Si quieres descargar mi código aquí dejo el link: https://www.dropbox.com/s/2bcm3sr9t6udu4f/Tarea5.cpp?dl=0

On To Functions

--Originally published at Programming in C++

1
Operations 

Program:

This program is similar to the first one we did, except we use functions.

How does functions work? For functions we write a part of the code outside our main, and the we call for it in the main.

Basically we did each operation in different pieces, and then we call them when needed.

In this specific program there are more code lines in the function version than in the original, but in bigger program it can be useful.

 

 

2
Our main code calling the operations