Python Function Example

Chapter: Python Last Updated: 14-09-2018 16:04:28 UTC

Program:

            /* ............... START ............... */
                
# Python Function Program Examples

def printText(content):
  print(content)


printText("Python")
printText("Function")
printText("Hai")


# Python Default Parameter Value
def printMyAge(age ="29"):
  print("My age is :"+age)

printMyAge("20")
printMyAge()
printMyAge("18")

# Python Function to Return value
def return_function(x):
  return 10 * x

print(return_function(7))
print(return_function(6))
print(return_function(5))
                /* ............... END ............... */
        

Output

Python
Function
Hai
My age is :20
My age is :29
My age is :18
70
60
50

Notes:

  • In Python function is a block of code which only runs when it is called.
  • Python Function Syntax :" def nameofFuntion(parameter) : "
  • In the program first function printText will just print the text which is passed in the function.
  • Second function printMyAge we can pass default as (age ="29"). If no arguments are passed in the function it will take the default value, in this case it is 29.
  • Last function def return_function(x) shows how to return a value in Python. In the fuction it will return the value by multliplying with 10. You can refer the function return_function(x) for more clarification.

Tags

Python Main Function Example, Function Definition in Python

Similar Programs Chapter Last Updated
Python Variables Python 21-09-2018
Python String Contains Python 15-09-2018
String In Python Python 22-09-2018
Python Switch Statement Python 20-09-2018
Python If Statement Python 14-09-2018
Area Of Triangle In Python Python 14-09-2018
Python Square Root Python 22-09-2018
Add Two Numbers In Python Python 13-09-2018
Python Comments Python 22-09-2018
Python Hello World Python 14-09-2018

1