Learn Python With Dhawal -7- Chapter


CHAPTER 7

Functions in Python

Function is basically a set of statements that are used to execute a particular task. Now you'd ask me that it is the same as loop right? No, there are a few key differences between a loop and a function.

A function only executes once after it is called but a loop executes until the condition is satisfied. A loop can have a function call inside but not function definition but a function can have a loop inside. A function is only executed if it is called a loop doesn't need to be called. A function has arguments aka the inputs but the loop doesn't have arguments it has its parameters in which it'll run. You can write a function in some another file, program and can link it with your present code and call it to run it but same is not possible with loop unless its inside a function to be called from your own code.

A Function is basically made up of 3 parameters but you can count 4 just for the sake of it if you want.


  1. Function declaration
  2. Function definition
  3. Function Call
  4. Return Value
Function Declaration

It is very much same as the naming a variable. You need to declare your function before you can define it. You can although combine the function declaration and definition together into one.

A Function is defined in python by using the keyword 'def' followed by the function_name that you want to keep followed by the parenthesis which may or may not contain the arguments.

Function Definition

A Function Definition is like writing the main body of a loop with all the statements that you want to execute when the function is been called. We usually combine the Function definition and Function Declaration in one step. 

Syntax:
Some Other Statement
def myFunction():
<tab>Statement 1
<tab>Statement 2
Some Other Statement

Function Call
Unlike a loop , a function won't be executed unless its called for. You can make a function call anywhere inside the program once its well defined.  You can also call functions from other codes that you previously have written or from python source libraries. Source libraries have several predefined functions like print(), etc that you call to execute some function. Some libraries are by default imported while some need to be imported and then created an object of to be used.

In python you need not worry about most of the things as you'd notice in the code examples below.

A function can be called by its name followed by parenthesis, for ex : myFunction(). Here inside parenthesis you can write the name of your input variables which you wanted to pass on to the function if they're mentioned in the function definition.  


Code 1:
Input:

  1. def function():
  2.     x=1
  3.     y=2
  4.     print(x+y)
  5.     
  6. function ()

Output:
3

Here we defined a function in a simple manner and didn't give any arguments as shown in step 1. We have created the variables x and y inside the function hence they can only exist inside the limits of that function as shown in step 2 and 3. To execute the function we simply call function as shown in step 6. Then the function will add the two numbers and print the result as shown in step 4.


Code 2:
Input:

  1. def function2(r):
  2.     p=22/7
  3.     print("The area of circle is ",p*r*r)
  4.     
  5. function2(5)

Output:
The area of circle is  78.57142857142857

This is an example of a function with parameters or arguments as per say. Here we are passing the radius of the circle and print the area of the circle. We created the pie variable by dividing 22/7 to get most accurate possible value. Technically there is one more way to get such commonly used constants and methods to find are and others in python but since we are in learning phase we are using this method for now to find the value of pi constant. As we would move in further deep concepts of the OOP I'd show more elaborate ways of doing things in python but until then keep this in mind.

We name the variable 'r' as we would be taking whatever the input is passed into that variable during the function call. While calling the function we can directly also pass a variable or  just a value like shown in the step 5.


Code 3:

Input:

  1. radius=int(input("Enter the radius "))

  2. a=0
  3. v=0
  4. p=22/7

  5. def circle():
  6.     area=areaOfSphere(radius,p)
  7.     volume=volumeOfSphere(radius,p)
  8.     print("The surface area of the sphere is ",area," and the volume of the Sphere is ",volume)

  9. def areaOfSphere(r,pi):
  10.     a=4*pi*r
  11.     return a

  12. def volumeOfSphere(r,pi):
  13.     v=4/3
  14.     v=v*pi*r*r*r
  15.     return v

  16. circle()

Output:


  1. Enter the radius 5
  2. The surface area of the sphere is  62.857142857142854  and the volume of the Sphere is  523.8095238095237
Here we are doing something huge, we are taking 3 functions to perform our task. One main function is calling two other smaller functions to perform the task. Also as you can see we are also returning the values that we have operated upon to the main function as well as the input is taken into new variables in the main function.

In step 1 we take the input radius from the user, then we find out the pi value and then define the function circle where we are using variable area to get the output whatever function area will be returning. Similarly we use the variable volume that function will return volume of the sphere after performing the operations.

For functions of area and sphere we pass the inputs of pi and radius and then let them calculate using the standard formulas. Once the calculation is done we return the value using the return keyword which will give the value back to the function call. The main function called to execute the whole program. Don't forget that else you'd get a blank output. The next code will give you somewhat idea in 'how to debug a code' aka 'how to find where your code went wrong'



Code 4:
Input:


  1. radius=int(input("Enter the radius "))

  2. a=0
  3. v=0
  4. p=22/7

  5. print("start loop is executed")
  6. def circle():
  7.     print("circle loop is executed")
  8.     area=areaOfSphere(radius,p)
  9.     volume=volumeOfSphere(radius,p)
  10.     print("The surface area of the sphere is ",area," and the volume of the Sphere is ",volume)

  11. def areaOfSphere(r,pi):
  12.     print("area loop is executed")
  13.     a=4*pi*r
  14.     return a

  15. def volumeOfSphere(r,pi):
  16.     print("volume loop is executed")
  17.     v=4/3
  18.     v=v*pi*r*r*r
  19.     return v

  20. circle()
  21. print("whole code is executed")


Output:


  1. Enter the radius 5
  2. start loop is executed
  3. circle loop is executed
  4. area loop is executed
  5. volume loop is executed
  6. The surface area of the sphere is  62.83185307179586  and the volume of the Sphere is  523.5987755982989
  7. whole code is executed
This is the same as previous code but here I have shown simple ways to debug a complex code to find out incase you make a wrong step and your code is compiled without any errors but output isn't what you are expecting. I kept little statements at the places I felt which would be creating trouble hence I get to know when some function is been executed or not. Usually you get errors which you can search up on the internet or solve by referring to the python documentation but at times your code is compiled yet you don't get the answer you might be expecting hence here you need to use several methods to find out the point of fault. You can also print variables in midst of some calculations if some operation isn't performed properly or there's some mistake in the code.



In Next chapter we will learn more about functions and understand them in much better manner.


~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.

0 comments:

Post a Comment

Do comment and share your thoughts.