Think In Functions: Building Solid & Reliable Functions That Lasts

Think In Functions: Building Solid & Reliable Functions That Lasts

User defined functions are a fundamental aspect of programming in Python, and they allow you to create reusable blocks of code that can be called multiple times with different arguments. In this blog post, we will explore the basics of defining and using functions in Python, as well as some examples of how they can be used to solve common problems. 

To define a function in Python, you use the def keyword, followed by the name of the function and a set of parentheses containing any parameters that the function takes. For example, consider the following function definition:




def greet(name):
print("Hello, " + name + "!")


This function takes a single parameter called name, and it prints out a greeting message using that parameter. To call this function, you simply use its name followed by a set of parentheses containing the arguments you want to pass to it. For example:




greet("Alice")
greet("Bob")

Output



"Hello, Alice!"
"Hello, Bob!"


Functions can also return a value using the return keyword. 

For example, consider the following function that calculates the factorial of a given number:




def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

result = factorial(5)

Output



120


The above function uses recursion to calculate the factorial of a given number n.
If n is 0, it returns 1.

Otherwise, it returns n multiplied by the factorial of n-1. 

To use this function, you can assign its return value to a variable.




Functions can also take multiple parameters. 

For example, consider the following function that calculates the area of a rectangle
To call this function, you pass it two arguments, the length and width of the rectangle:




def rectangle_area(length, width):
return length * width

area = rectangle_area(10, 20)

Output



200


Functions can also have default values for their parameters. 

This allows you to specify a default value for a parameter in the function definition, and then use the function without passing that parameter if you want to use the default value. 

For example:




def greet(name, greeting="Hello"):
print(greeting + ", " + name + "!")

greet("Alice")
greet("Bob", "Hi")

Output



"Hello, Alice!"
"Hi, Bob!"


In the example above, the greet function takes two parameters: name and greeting. The greeting parameter has a default value of "Hello", so if you call the function with only one argument, it will use the default value for the greeting parameter. 

Another useful feature of functions in Python is that they can accept an arbitrary number of arguments. 

To do this, you use the *args syntax in the function definition, like so:




def average(*args):
total = 0
count = 0
for arg in args:
total += arg
count += 1
return total / count

average(1, 2, 3)

Output



2.0


Reference Books


Here are the books I’ve used as references for writing this article,
please feel free to read them If you don’t want your knowledge to be
limited to this article alone.