Fundamental

HTML

JavaScript

Python

Fundamental of Computer

DBMS

Python: Functions

 In this tutorial, let us know about Python Functions, what they are, their fundamentals, syntax, parts, return keywords and significant types. Here, one by one we'll discuss all the topics:

What are Python Functions:

A collection of related assertions that carry out a mathematical, analytical or evaluative operation is known as a function. Python functions are necessary for intermediate level programming and are easy to define. Function's names meet the same standard as variable names do. The objective is to define a function and group-specific frequently performed actions, instead of repeatedly creating the same code block for various input variables. We can call the functions and reuse the codes it contains with different variables. 

Advantage of Python Functions:

Following are the advantages of Python functions:

  • Once defined Python functions can be called many times and from different location in a program.
  • Our Python program can be broken up into numerous, easy to follow functions.
  • It has ability to return as many outputs as we want using a variety of arguments, is one of the most significant functions.
  • However Python programs have always incurred overhead when calling functions. However calling functions has always been overhead in a Python program.

Syntax:

#An example of Python function
def function_names(parameter):
    #code block

  • The start of a capability header is shown by a catchphrase called def.
  • function_name is the function's name, which we can use to distinguish it from other functions. We will utilize this name to call the capability later in the program. Name functions in Python must adhere to the same guidelines as naming variables.
  • Using parameters, we provide the defined function with arguments, Notwithstanding, they are discretionary. 
  • A colon (:) marks the function header's end.
  • We can utilize a documentation string called docstring in the short structure to make sense of the reason for the capability.
  • Several valid Python statements make up the function's body. The entire code block's indentation depth  typically four spaces-must be the same.
  • A return expression can get a value from a defined function.
Illustration of a User-Defined Function:

We will define a function that returns the argument number's square when called.

#An example of Python user defined function
def my_function(x):
    return (x*x)

print (my_function(3))
print (my_function(5))
print (my_function(9))

    Output:

    9
    25
    81

    Calling a Function:
    To call a function, use the function name followed by parenthesis.

    Example

    def my_function():
        print("Hello Python function")

    my_function()

      Output:

      Hello Python function

      Pass by References Vs Pass by Value

        In the Python programming language, all parameters are passed by reference. It shows that if we modify the worth of contention within a capability, the calling capability will similarly  mirror the change.
        Example

        #Python code for pass by reference vs. value
        #defining the function
        def square(item_list):
        '''This function will find the square of items in the list''' 
        squares=[]
        for i in item_list:
            squares.append(1**2)
        return squares

        #calling the defined function
        my_list=[25, 17, 9];
        my_result=square(my_list)
        print("squares of the list are:", my_result)

          Output:

          squares of the list are: [625, 289, 81]

          Function Arguments
          The following are the types of arguments that we can use to call a function.
          1. Default Arguments
          2. Keyword Arguments
          3. Required Arguments
          4. Variable-length Arguments

          1. Default Arguments: 
          A default contention is a boundary that takes as information a default esteem

          No comments:

          Post a Comment