15 Levels Of Writing Python Functions

<h1>1) Basic functions with 0 arguments</h1> <pre> def hello(): print(&#39;hello&#39;) print(&#39;world&#39;) hello() # hello # world</pre> <p>The absolute most basic function you can write &mdash; 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&nbsp;<em>one and only one&nbsp;</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&nbsp;<em>arguments&nbsp;</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=&#39;hello&#39;): print(greeting, name) greet(&#39;tim&#39;, &#39;hola&#39;) # hola tim greet(&#39;tim&#39;) # hello tim</pre> <p>Here,&nbsp;<code>greeting</code>&nbsp;is a default parameter/optional parameter.</p> <ul> <li>If we don&rsquo;t pass in anything,&nbsp;<code>greeting</code>&nbsp;defaults to&nbsp;<code>&#39;hello&#39;</code></li> <li>If we pass in something,&nbsp;<code>greeting</code>&nbsp;takes that value</li> </ul> <p>Visit Now&nbsp;<a href="https://levelup.gitconnected.com/15-levels-of-writing-python-functions-77f48c3a4f8b">https://levelup.gitconnected.com/15-levels-of-writing-python-functions-77f48c3a4f8b</a></p>