python
Functions
A function is a small machine: you give it something (the parameters), it does some work, and it gives you back an answer with return.
def triple(n):
return n * 3
print(triple(5)) # 15
print(triple(10)) # 30
defmeans "define a function".- The code inside the function is indented (4 spaces). Python uses indentation to know what belongs to the function.
returnsends the answer back to whoever called the function.
return:returnhands a value back to the program so it can keep using it.
Your mission
- Write a function
double(n)that returnsntimes 2. - Write a function
greet(name)that returns the stringHello, NAME!. For examplegreet("Sam")returns"Hello, Sam!".