codescracker


python

Python Functions



« Previous Tutorial Next Tutorial »


A function in python, is a block of code, used to perform a particular action.

Python Function Definition

To define a function in python, follow these rules:

Here is the general form to define a function in python:

def function_name(parameters):
	"function_doc_string"
	function_suite
	return [expression]

Here is an example, takes one string parameter and prints this argument as output:

def printit(strn):
	"This will print the passed string into this function"
	print(strn)
	return

Python Function Calling

To execute a function in python, you have to call it from another function or directly from the python prompt. Here is an example, showing how to call a function in python:

# Python Functions - Call a Function in Python - Example Program

def printit(strn):
    "This will print a passed string inside this function"
    print(strn)
    return;

printit("This is the first call to the user-define function, printit");
printit("This is the second call to the same function, that is, printit");
printit("This is an another call to the same function, printit");

Here is the output produced by the above python program:

python functions

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test