Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. print("***** FUNCTION DEFINITION *****")
  2.  
  3. # define a function name hello_world
  4. def hello_world():
  5. print("Hello, World!")
  6.  
  7. # call the function 5 times
  8. for i in range(5):
  9. hello_world()
  10.  
  11. # ---------------- OUTPUT ----------------
  12. # ***** FUNCTION DEFINITION *****
  13. # Hello, World!
  14. # Hello, World!
  15. # Hello, World!
  16. # Hello, World!
  17. # Hello, World!
  18.  
  19.  
  20. print("***** FUNCTION WITH PARAMETERS AND ARG *****")
  21.  
  22. # x is a parameter
  23. def foo(x):
  24. print("x = %d" % x)
  25.  
  26. # pass 5 to the function parameter. Here 5 is an arg.
  27. foo(5)
  28.  
  29. # ---------------- OUTPUT ----------------
  30. # ***** FUNCTION WITH PARAMETERS AND ARG *****
  31. # x = 5
  32.  
  33.  
  34. print("***** FUNCTION WITH RETURN *****")
  35.  
  36. # function that return sum or 2 numbers
  37. def sum_two_numbers(a, b):
  38. return a + b
  39.  
  40. c = sum_two_numbers(3, 12)
  41. print("c = %d" % c)
  42.  
  43. # ---------------- OUTPUT ----------------
  44. # ***** FUNCTION WITH RETURN *****
  45. # c = 15
  46.  
  47.  
  48. print("***** FUNCTION WITH DEFAULT PARAMETER *****")
  49.  
  50. def multiply_by(a, b=2):
  51. return a * b
  52.  
  53. print(multiply_by(3, 47))
  54.  
  55. # single arg can be passed
  56. print(multiply_by(3))
  57.  
  58. # ---------------- OUTPUT ----------------
  59. # ***** FUNCTION WITH DEFAULT PARAMETER *****
  60. # 141
  61. # 6
Add Comment
Please, Sign In to add comment