Functions in Python basics

Functions In python:
  • Set of instructions to perform a specific task.
  • Below is the syntax of functions in python:
                   def function_name([arg1,....,arg n]):
                          statement 1
                          ........
                          statement n
                          [return value]
                   variable = function_name([value1,......,value n])
Example:

                   def Sum(a,b):
                          result = a+b
                          return result
                   answer = Sum(5,2)
                   print(answer) 
  • where:
    • def Sum(a,b) - Function signature
    • Sum - Function name
    • a,b -Function parameters: takes values from passed from the function call. also called Formal arguments.
    • result = a+b - Function body / Statements inside the function.
    • return result - Returns the result to the calling block.
    • answer - Assigning the return value to a variable
    • Sum(5,2) - Function call
    • 5,2 - values passed to the function. known as actual arguments.

1 comment:

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...