Python Functions

Functions in Python

Functions are set of instructions to perform a specific task. Below is the syntax of functions in python.

Syntax
//Syntax:

def function_name([arg1,...,argn]): 
    #statements 
    [return value] 
variable_name = function_name([val1,...,valn])
Notepad

Note: Anything enclosed in [ ] (square bracket) is optional.

Example :

function in python

Tryout :

Execute this code in python playground and observe the below

  • Function call
  • Actual arguments being copied to formal arguments
  • Execution of function body
  • Return from function
CODE/PROGRAM/EXAMPLE
observe1="What‘s happening!!"

	def passport_check(passport_no):
		observe4="actual copied to formal"
		observe5="func. execution starts"
		if(len(passport_no)==8):
			if(passport_no[0]>="A" and passport_no[0]<="Z"):
				status="valid"
			else:
				status="invalid"
		else:
			status= "invalid"
		observe6="func. execution ends"
		return status

	observe2="function with formal arg."
	observe3="calling with actual arg."
	passport_status=passport_check("M9993471")

	print("Passport is",passport_status)
	#observe1,2,3,4,5,6 are temporary variables used to explain this concept

Returning from a function :

Let's see how we can use values returned from a function.

Returning from a function in python
#python_function #Functions_in_Python #function_in_python #Returning_from_a_function_in_python

(New page will open, for Comment)

Not yet commented...