function or methods in Python.



function or methods in Python.

In this post we are going to learn about functions or methods in Python. Let's start..!

The very first thing that we should know What is function ? 

Function :

Function is very simple concept. Function or methods means a block of code that execute only when we call the function. It simply means to execute the specific block of code or function we need to call it. 

The function can contain many statements of code. The function should have a proper name. We can't use the name of pre-defined or inbuilt functions name. If you write a same name to your function as a name of inbuilt function then that will raise an error. 

Advantages of using Functions.

1] By using functions or methods we can avoid the repetition of same statement. If you are using same statements in your code again and again then you must create a bunch of these statement Simply a function. 

2] With the help of function we can divide our program into a small part. It means if you are working on a big project having thousands line of code then it might be hard to handle whole project in a single part. To make easy to handle we can break our code into a small part called as Function or methods.

3] Write once and use anywhere in program. It simply means you have to find the some statements that you are using again and again in your program and bunch them into a block called as Function and call the function when we want that statements to execute. Simply you have to create the function only once but you can call this function anywhere in program when you need it by using a single word i.e. function name.

4] It helps to solve problems very fast. It means if you have any problem in your code then you have to read all code and solve the problem but if have created functions then you know that where is the actual problem. If you don't have functions in your program and if there is an problem in your code it will not execute whole program because of single problem. But in the case of function the only function will not work that actually have a problem but all other functions without any problem can run properly. 


Now How to use function in Python ? 

Syntax : 

def function_name( ):

      function block


To use functions in Python we need a special keyword that is def . This keyword shows that we are creating a function. After this keyword give a function name. The name of function should not be same as inbuilt function. Give a round brackets ( () ). This round brackets shows that this is a function. After this put a colon (:). And write a function block in proper indentation.

Function name should not be start with any number or special character except underscore ( _ ) . The name of function will only start with a character or a underscore ( _ ). Function name should not have any space between; instead of space we can use underscore. Also function name should not contain any symbol or special character. Every function should have their names different.


Let's create a simple function.

for ex. Suppose we are writing a program to add two numbers. For this simply create two variables take two numbers from user using input() and simply print the addition of that two numbers. 

This is fine but What if we want add two numbers five times , ten times or hundred times anywhere in program. Then you have to create a function. Also you can write same code hundred times but this not a best way to write program. And this repetition we can avoid using function. Simply create a function and write code in the function block. 

[ if you don't know how to use input() then click here to learn more about input() in Python ]



# Function in Python
# creating a function add()
def add():
    # taking 1st number and
    # storing in variable num1
    num1 = int(input("Enter 1st number :: "))
    
    # taking 2nd number and
    # storing in variable num2
    num2 = int(input("Enter 2st number :: "))

    # printing addition of num1 and num2
    print("Addition = ", num1+num2)



This will create a function but this function will not execute. Because functions execute only when you call it. Simply if you want to use function then you have to call it.

for ex. we want to print some text for this we use a print() function means actually we are calling a print function. To use any function we have to call it first. 

Now we will call our add() function.

To call a function write a function name and give a round brackets. Simply write add() in this case.


# Function in Python
# creating a function add()
def add():
    # taking 1st number and
    # storing in variable num1
    num1 = int(input("Enter 1st number :: "))
    
    # taking 2nd number and
    # storing in variable num2
    num2 = int(input("Enter 2nd number :: "))

    # printing addition of num1 and num2
    print("Addition = ", num1+num2)


# calling a function
add()


Now this will call a function add() and execute the statements inside a function block.

When you calls add() function it will find a function having name as add() and will start executing the function block. In this it will take a input from user also print a message that is "Enter 1st number :: " and store the number entered by user in variable num1. Same for second number also; it will take input from user and also print a message that is "Enter 2nd number :: " and store the number entered by user in variable num2. Next it will print a message that is "Addition = " and print the addition of num1 and num2 ( it means addition of values present in variables num1 and num2 )




This how we can use Functions or Methods in Python.


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post