Mastery11 and Mastery12

12.Creating Python functions.

In order to call a function we have to create it first, in python there are basic functions like sum or division. However we can actually create our own.

first we have to write “def”, in order to define our function. Now we have to write the name of our function, in this case I’m calling it sum_numbers, because we’re going to sum these numbers. Now we write in parenthesis the variables that we want to use. Then we’ll write the action that our function will do, this time is adding x plus y. Finally, we have to write “return answer” so the function give us the answer of the addition that we did there.

11.Calling Python functions

Now to call this function, we have to give them the value of variables, in this case num1 and num 2. Now we have to write the function, sum_numbers, but instead of having x and y, the actual value; num1 and num2. So now the function will make the addition of num1 and num2, the answer to this addition will be the value of the_sum. Now we only have to print “the_sum” in order to actually see the solution of  num1 + num2.

CC BY 4.0 Mastery11 and Mastery12 by Alejandro Abreu is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.