Published on: 28 Oct, 2021

Updated on: 29 Oct, 2021

How Can We Define Variable & Function In Python

Here, we will be exploring how to define function and variable in Python

Any programming language across IT industries is designed to design a code that helps to perform a set of tasks or maybe a specific task on specific data set of data.

Now, the most important thing is how we make these data available to our program at the time of execution to perform operations on it. And the answer to this is "Variable"

What is the Variable?

In laymen term, variables are nothing but a container or placeholder that we use to store data or set of data.

Technically, it is a memory location within our system RAM that helps to store data.

How to define Variable?

Generally, in programming languages whenever we define or declare variable we must always specify the data type first and then the variable name.

Since python is a dynamically typed language, we don't need to explicitly specify the data type. We simply write the variable name and value assigned to it.

name = 'I m variable id = 124

Once we have defined the variable, its time to see how we can operate on these variables. And this is where function comes into the picture.

What is the Function?

In laymen terms, functions are nothing but a verb that perform some action on data/variable which is a noun

Technically, there are multiple definitions for the functions. Some of them are:

A function is a set of instruction that helps to perform some operation on the variable

A function is a block of code that performs a specific operation on a variable

How to define Function?

Defining function in Python is pretty easy and straight forward unlike other programming languages.

        
            def function_name():
                print('I am a function')

            def another_funtion(x, y):
                print('I am a function with parameter')
        
    

Based on function parameters and return type there are different ways to define functions

  • Function without parameter & return type
                    
                        def function_without_parameter_and_return_type():
                            print('I do not have parameter & return type')
                    
                
  • Function without parameter & with return type
                    
                        def function_without_parameter_and_return_type():
                            return 'I have no parameter but I do return'
                    
                
  • Function with parameter & without return type
                    
                        def function_without_parameter_and_return_type(x,y):
                            print('I have parameter but no return type')
                    
                
  • Function with parameter & with return type
                    
                        def function_without_parameter_and_return_type(x,y):
                            z = x + y 
                            return z
                    
                

How to execute function in Python?

Executing pyton function is pretty easy. We have to simply call the name of the function with arguments if needed

  • Calling function w/o parameter
                    
                        def function_without_parameter_and_return_type():
                            return 'I have no parameter but I do return' 
    
                        # Calling function 
                        function_without_parameter_and_return_type()
                    
                
  • Calling function with parameter
                    
                        def add_two_nos(x, y):
                            z = x + y
                            return z
    
                        # Calling function 
                        result = add_two_nos(10, 20)
                        print(result)
                    
                

Share with your love's one!