forked from inspirezonetech/TeachMePythonLikeIm5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
using-functions.py
56 lines (42 loc) · 1.86 KB
/
using-functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# ------------------------------------------------------------------------------------
# Tutorial: how to declare and call a function
# ------------------------------------------------------------------------------------
# the 'def' keyword is used to declare a function. the structure of a function is:
def a_function():
print('hello world')
# to call a function simply type its name
a_function()
# you can declare functions that accept arguments
def another_function(argument1, argument2):
print(argument1, argument2)
# when calling a function with arguments, pass in the arguments
another_function('hello', 'universe')
# -----------------------------------------------------------------------------------
# Challenge: create a function that takes two integers as arguments and prints the sum.
# call the function and pass the values '1' and '2' to the function
# ------------------------------------------------------------------------------------
# You code here
#
#
#
#
# ...
# ---------------------------------------------------
# More on functions
# ---------------------------------------------------
# Functions can return something with the 'return' keyword.
def biggestNumber(number1, number2):
maxnumber = max(number1, number2) # The max built-in function returns the highest value of all those passed
return maxnumber
# When calling the function, you can use the value returned
oldestPerson = biggestNumber(15, 18)
print("Oldest person:", oldestPerson)
print(biggestNumber(100 * 0, 1 * 2))
# -----------------------------------------------------------------------------------
# Challenge: modify the previous function you created so it returns the value instead.
# create another piece of code that uses the value returned from your function
# ------------------------------------------------------------------------------------
# Your code here
#
#
# ...