Introduction
Welcome to my 20th blog post. Today I learned about 'functions' statements in python. I also learned about built-in functions and user-defined functions in python. Now let's dive deep into the details
So let's get started......
Analogy
Functions
Suppose we want to use some logic in code again and again, and the code logic is say 20 lines, then in this case we don't want to write the same lines of code again and again manually or even copy-paste it. Here what we can do is wrap these lines of code into something called functions and just give a call to that functions whenever we want to reuse those lines of code.
In technical terms, a function is a block of code that performs a specific task whenever it is called. In a large code base, it is advisable to create and use existing functions to make the program easy to read and organized.
Types of functions
There are 2 types of functions viz:
Built-in functions
User-defined functions
Built-in functions:
These functions are defined and pre-coded in python. To get the list and meaning of built-in functions we can google the same. Some examples of built-in functions are as follows:
min(), max(), len(), sum(), type(), range(), dict(), list(), tuple(), set(), print(), etc.
In Built-in functions, we do not need to use 'def 'as a keyword to define them.
User-defined functions:
We can create customized functions to perform specific tasks as per our needs. Such functions are called user-defined functions.
Syntax -
def function_name(parameters):
pass
# Code and Statements
Create a function using the def keyword, followed by a function name, followed by parenthesis (()) and a colon(:).
Any parameters and arguments should be placed within the parentheses.
Rules for naming functions are similar to that of naming variables.
Any statements and other code within the function should be indented.
In case we are writing some functions and we plan to write it fully at some point in time in the future, then in the function we need to write the keyword 'pass'
Calling a function
We call a function by giving the function name, followed by parameters in parenthesis.
Example -
def name(fname, lname):
print("Hello,", fname, lname)
name("Sam", "Wilson")
Output -
Hello, Sam Wilson
Example Program
We wrote a bunch of code as explained by codewithharry in the tutorial. In this dummy program, I created 3 functions. First to calculate the geometric mean of 2 numbers, second to find the greater between 2 numbers and lastly a program that we just want to pass and will write the code in near future.
def calculateGmean(a, b):
mean = (a*b)/(a+b)
print(mean)
def isGreater(a, b):
if(a>b):
print("First number is greater")
else:
print("Second number is greater or equal")
def isLesser(a, b):
pass
a = 9
b = 8
isGreater(a, b)
calculateGmean(a, b)
# gmean1 = (a*b)/(a+b)
# print(gmean1)
c = 8
d = 74
isGreater(c, d)
calculateGmean(c, d)
# gmean2 = (c*d)/(c+d)
# print(gmean2)
Output -
First number is greater
4.235294117647059
Second number is greater or equal
7.219512195121951
Resources Used
You can watch the video of Day#20 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Creating functions can help us reduce our mundane coding tasks and help us to shorten the code base making it more readable and organized.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #20
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.