#WSQ08 – Functions

Foe this assignment we dug into functions.

The homework instructions were to go make to WSQ03 – Fun with numbers and write a function for each calculation.

So the first thing I did was to ask the user for two numbers and set ‘x = userFirstNumber’ and ‘y = userSecondNumber’ in order to make my code look a little neater.

Screen Shot 2016-03-05 at 2.02.05 PM

Then I created the functions that will calculate the difference, product, division and remainder of the numbers.

I made a function for each of the calculations even though I could have made one function for all of them, but I decided to get a bit more practice by making more.

To create a function in Python you have to use the keyword ‘def’ followed by the name of the function and a set of parenthesis with hoe many parameters the function will accept.

Screen Shot 2016-03-05 at 2.09.47 PM

These functions accept two parameters which will be the first and second number given by the user which I passed on when I called the functions.

Screen Shot 2016-03-05 at 2.14.29 PM.png

Finally I printed a string for each of the functions to let the user know which one is which, followed by the call of each function and I passed in the two parameters that each function accepts, which are the two numbers given by the user.

This is what the final result looks like:

Screen Shot 2016-03-05 at 2.18.17 PM

 

CC BY-SA 4.0 #WSQ08 – Functions by kevingmcc is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.