15 Levels Of Writing Python Functions
<h1>1) Basic functions with 0 arguments</h1>
<pre>
def hello():
print('hello')
print('world')
hello()
# hello
# world</pre>
<p>The absolute most basic function you can write — a function that takes in 0 arguments, but does only one uncustomizable thing.</p>
<h1>2) Functions that take in 1 argument</h1>
<pre>
def add10(n):
return n + 10
print(add10(4)) # 14
print(add10(5)) # 15</pre>
<p>Here, our function takes in <em>one and only one </em>argument, and adds 10 to it.</p>
<h1>3) Functions that take in 2 or more arguments</h1>
<pre>
def add(a, b):
return a + b
print(add(10, 4)) # 14
print(add(100, 5)) # 105</pre>
<pre>
def average(a, b, c):
return (a+b+c)/3
print(average(1,2,3)) # 2.0
print(average(3,4,6)) # 4.333333333</pre>
<p>We can make functions take in as many <em>arguments </em>as we want them to.</p>
<h1>4) Functions with default/optional parameters</h1>
<p>We can make our functions take in default parameters, also known as optional parameters.</p>
<pre>
def greet(name, greeting='hello'):
print(greeting, name)
greet('tim', 'hola') # hola tim
greet('tim') # hello tim</pre>
<p>Here, <code>greeting</code> is a default parameter/optional parameter.</p>
<ul>
<li>If we don’t pass in anything, <code>greeting</code> defaults to <code>'hello'</code></li>
<li>If we pass in something, <code>greeting</code> takes that value</li>
</ul>
<p><a href="https://levelup.gitconnected.com/15-levels-of-writing-python-functions-77f48c3a4f8b">Read More</a> </p>