INTRODUCTION:
Functions
are compound statements that can take input, execute instructions and return
output. With functions, we can define and reuse the functionality in
our programs. A function can have no parameters (I will explain this in just a
bit) , one parameter (f(x)), or multiple parameters (f(x,y,z)).
Parameters are basically values that we want
the function to use. You will understand this concept better as you move
ahead.
Example:
f(x)=x*2
Here, ‘f’ is the function,
‘x’ is the parameter i.e. the value
that the user gives the function to execute the command on, and ‘x*2’ is the function definition i.e. the command
the function will execute.
FUNCTIONS IN PYTHON:
Keyword ‘def’ tells Python that you
are defining/creating a function. After ‘def’ you give a function a name by which it will be
referred to in the rest of the program, just like variables.
A function
is defined in the following way:
def f(x):
return
x*2
Here, ‘def’
is the keyword that tells Python we
are defining a function, ‘x’ is the parameter
that the user passes to the function, ‘return x*2’ is the instruction/function definition that tells Python to multiply ‘x’
by 2 and return the value.
Now, we have
created a function ‘f’ that returns the parameter passed multiplied by 2. But,
how do we use it?? Well, to use a
function simply assign it to a variable, we call this ‘Calling a Function’ or
simply ‘Calling’.
Here’s how
you do it:
a=f(3)
Here, ‘a’ is
the variable that will be storing
the value return by the function, ‘f’ is the function’s name and ‘3’ is the parameter
that we have passed it.
Let’s understand, what will happen now. ‘a’ is the variable through which we have called the function. We have called the function by passing the value ‘3’ as a parameter in place of ‘x’ which we used while defining the function, which means we want the function to use the value ‘3’ for the commands that use ‘x’ as a value. The function we defined will now multiply the value of ‘x’ (i.e. ‘3’ in this instance), by 2 and return it. Now the result that was returned (i.e. ‘6’ in this instance) will be stored in variable ‘a’. To print the result you simply have to print ‘a’.
You can also use multiple parameters as well. To use multiple parameters, define the function in the following way:
def f(x,y,z):
return
(x*y)/z
Note: When you call this function, keep in
mind that you must pass the values in the exact order of the parameters. For
example:
a=f(3,4,2)
Here, ‘3’
will be assigned to ‘x’, ‘4’ will be assigned to ‘y’ and ‘2’ will be assigned
to ‘z’. So, ‘3’ will be multiplied by ‘4’ first
(because of the brackets and the BODMAS rule) and then the product of the two
will be divided by ‘2’.
I hope you
understand the concept of parameters now. If not, feel free to go through the
examples again or ask me personally in the comments section below.
There are
two types of parameters in functions:
Required Parameters: These parameters must be passed by
the user when calling the function for the function to execute. All the
examples above use required parameters.
Optional Parameter: These are the parameters that the
user may or may not pass any value when calling them. These parameters have a
default value in case the user does not pass any value. These default values
are assigned to the parameters when defining the function. An example of such types of parameters is explained
below:
def f(x=2):
return x**2
Here, if the
user does not pass any value for ‘x’ when calling the function, the function
will assume ‘2’ as the value of ‘x’ by default and return the square of ‘2’
i.e. ‘4’.
But if the
user does pass any value for ‘x’, suppose ‘4’ then the function will use that
value instead of the default value and return the square of ‘4’ i.e. ‘16’.
BUILT-IN
FUNCTIONS:
These are the functions that are
already built in Python and are available to anyone who has installed Python. len(),
str(), input() are all examples of built-in functions.
Example:
age= input(“Enter your age”)
int_age=int(age)
if int_age < 21:
print(“You
are young”)
else:
print(“WOW,
You are old!!”)
CONGRATULATIONS !!! You have learnt how to create a function in a program. Trust me this knowledge will help you create programs that are a lot easier to understand and execute. If you have a huge wall of code where each portion performs a certain task, you can divide them into separate functions and then all you have to do is call them, you will not have to write that code again and again. Have fun with it !!! Create some cool programs. Good luck !!!!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T feel free to ask any further queries in the comment section below.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback