[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

How to create a Function in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570864807553{background-color: #f6f6f6 !important;}”]

A function is defined (created) easily in Python by specifying which name the function should have and if the function should have some inparameters. A function is always terminated with the return command (return a result), or if all operations in the function have been performed.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]Creating a function in Python is very simple. When you define (create) a function, you use the reserved word def followed by the function name. It also specifies whether the function should have some input parameters. Everything that is supposed to be performed when the function is called is coded with indentation. The function ends with the reserved word return which terminates the function and returns a final value. If return is not used, the function runs until the last operation is completed. Additionally, you can also use return without returning any value. Finally, can also add a function description “function documentation” that explains what the function does.[/vc_column_text][vc_empty_space height=”20px”][vc_single_image image=”19489″ img_size=”large” alignment=”center” qode_css_animation=””][vc_empty_space height=”20px”][vc_column_text]

  • A method is a sequence of operations that is performed when called
  • Methods can get input parameters and return values
  • The method is independent of the other code in the program

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][vc_column_text]

How to create a Function in Python? – Syntax

When creating a function, you need to specify the input parameters, and whether the function will return any values. We can design our function in several ways:

 

Syntax:

 

def function_name(input parameter):
    “Function Documentation”
    what the funct should perform
    return value

Let’s take a closer look at the code above. At first, we define our function, set the name we want it to have, and choose the input parameters

 

def function_name (input parameters):

Then, on line 2, we added a Function Documentation where we can explain what the function actually does/purpose

 

   “Function Documentation”

Furthermore, the third line of code in our syntax above is where we write what the function should do, in other words, the operations it should perform.

   what the funct should perform

Finally, we end the function by using the reserved word return and the value that the function should return.

 

   return value

 

Note: all the code that is “inside” the function is written indented.[/vc_column_text][vc_empty_space height=”20px”][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”20px”][vc_column_text]

How to create a function in Python – Example

Let’s take a short and simple example just to illustrate how we can use a function.

 

def my_function(x):                       
    "multiplies input parameter with 3"    
    return 3 * x

So the only thing  the function, that we named my_function, is to take the input parameter and multiply that value by 3. Additionally, the function should after that then return the value

 

If we try our function by sending the value 5 as input parameter

 

result = my_function(5)
print(result)

We will get the result

 

15

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]If you are not sure what the function actually does, you can call the function description with help () that returns the function documentation:

 

help(my_function)

Will print the result

 

Help on function my_function in module __main__:

my_function(x)
    Multiplies the input parameter by 3

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][/vc_column][/vc_row]