Advertisement
agough

Simple Python Function

Mar 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. value_one = 10
  2. value_two = 20
  3. result = value_one + value_two
  4. print "The result is equal to ", result
  5.  
  6. def AddTwoNumbers(first_value, second_value): # function definition called AddTwoNumbers and it takes 2 parameters - first_value and second value
  7.     result = 0  #I declare a variable to hold onto the result of adding the 2 numbers
  8.     result = first_value + second_value  #the 2 numbers that I passed as parameters are added together and the value is assigned to the variable result.
  9.     return result  #finally I return the result to the where I called the function
  10.  
  11.  
  12. myResult = AddTwoNumbers(10,20) # #This will call the function defined above and pass the number 10 and store it in first_value, and store 20 in second_value
  13. print "The result = ", myResult #This will print the result
  14.  
  15. #Now I can call that function over and over again - functions make code reusable if we want to do some work multiple times
  16.  
  17. myResult = AddTwoNumbers(5, 6)
  18. print "The result = ", myResult
  19.  
  20. myResult = AddTwoNumbers(15,98)
  21. print "The result = ", myResult
  22.  
  23. myResult = AddTwoNumbers(6, 9)
  24. print "The result = ", myResult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement