Python Define Function


Define Function

How to define an function in Python language ?

The simple way to write a function is to use def keyword, then write de function name, and inside function use the return keyword to print the output of the function.

Examples

Create simple function

	
>>> def test_function():
	return "My Test function"

>>> test_function()
'My Test function'
>>> 

Create function with parameters

>>> def f1(a,b):
	return print("The sum is: ",a + b)

>>> f1(2,3)
The sum is:  5
>>> 

Define function with more parameters

>>> def f2(a,b,c):
	if a == 1:
		return print("B value: ", b)
	if a == 2:
		return print("C value: ", c)

	
>>> f2(1,'OK','NOT OK')
B value:  OK
>>> f2(2,'OK','NOT OK')
C value:  NOT OK
>>>