Python Custom Functions


By reading this article your following problems will be answered.

How to define a Python custom functions.

How to call a defined Python custom functions.


Defining custom functions

  • We can define custom functions in Python.
  • The following code shows how to define a function called output_sn.
def output_sn():
  serial_no = 123456
  print(serial_no)
  • To define a custom function, first we need to write ‘def’ followed by desired function name and brackets. 
  • Also make sure to add a colon after the brackets.
  • The we need to write the code that is supposed to run when the relevant function is called, and make sure to indent the whole code that is supposed to run with the function.

Calling custom functions

  • To call a specific function that we defined, we should call the relevant function name followed by brackets. The following code shows how to call the output_sn function that we defined earlier when defining a function.
output_sn()
  • The output of above line will be ‘123456’.
  • We can also define functions with calling previously defined functions as nested functions.
  • That means we can use custom functions inside custom functions, as much as we need.

Function arguments

  • We can also put arguments or parameters into custom functions.

Know more about Python

Python

Working with text and numbers in Python

Variables in Python

Conditionals in Python

Loops in Python

Strings in Python