Python Functions
Function Definition: Function is a group of statements which performs a specific task. It is also known as a Sub-routine or a Procedure or a Method.
In most programming languages two types of functions available:
- Library Functions (also known as System defined function, for example print(), input(), etc.)
- User defined functions (written by programmers, for example calculate(), find_area(), etc.)
Let's learn how Python Functions works.
Defining a Python Function
Following simple rules are followed while defining Python Functions:- Function blocks begin with the def keyword; followed by the function name and parentheses ( ).
- Any number of parameters or arguments can be placed within function parentheses.
- In the beginning of functions, writing a short comments about a function is one of the standard practice.
- Function code begins with a colon(:) and has indentation.
- return statement at the end indicates end of function.
Syntax of Python Function
"function comments"
function logic / code
return
By default, arguments or parameters have a positional behavior and you need to write in the same order as they defined.
Python Function Example
The following python function prints line with a specified character as an parameter.
"This function prints a line of given character"
for i in range(0,15)
print (chr)
return
print_line('*')
print("")
print(" Welcome to Python Practicals ")
print_line('=')
Output of program
* * * * * * * * * * * * * * *
Welcome to Python Practicals
= = = = = = = = = = = = = = =
* * * * *