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 as                                            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.
Previous post:
  • Control structures
          Click here -----> Lesson 2: Control structures
  • Basics of python
          Click here -----> Lesson 1: Basics of python
  • About: Introduction to python course
          Click here -----> About: Introduction to python course




1 comment: