Best post on Function definition & calling in python -1

What is function , function definition and function calling in python?

Functions

A function is a block or package of organized , reusable lines of code for later execution.
Functions provide modularity to the script by breaking huge codes in small modules.
This helps to avoid repetition and increases re-usability.
Python provides various built-in functions like int(), sqrt(), list(), type() etc.
We can also create own functions in python, this is termed as user defined functions.

Function Definition

In python we can define a function by mentioning function name and statements.
Function definition begins with def keyword followed by function name, parentheses and colon.
We can add parameters in parentheses if required.
Later we can add required statements in code block.

Syntax

def func_name(parameters):
    statement(s)

Here,
def is a keyword used for defining function.
func_name can be any appropriate name given to the function.
parameter is optional.

Function Calling

In python we can call a defined function using its function name and arguments if any.
Function calling involves giving input and it will return a value.
When we call a function we invoke the function to get executed.
We can pass more than one argument in the function.
Function will pass that argument in code block and will return the result called as return value.

Syntax

func_name(arguments)

Here,
func_name will be name given in the function definition.
argument is optional and is mentioned only if parameter is present in definition.

Example of function definition and function calling

""" Author : ITVoyagers (itvoyagers.in)
Date :31st August 2020
Description : Program for showing function definition and function calling.
"""
#function definition
def name_of_fruit(x):
print("name of fruit is ", x)

#function calling
name_of_fruit(“peach”)

 

Output

Output of function definition and function calling
Output of function definition and function calling

Type Conversion functions in Python

Python supports many built-in functions for conversion of values from one datatype to another data type.
Such conversion is also called as coercion.
Python provides type() to check the data type of a value denoted by variable.
All the type conversion functions returns new object representing the converted value.
When functions like int(), str(), list() etc are used to change values to integer, string, list etc, are called as type conversion function

Example of type conversion function in python

""" Author : ITVoyagers (itvoyagers.in)
Date :31st August 2020
Description : Program for type conversion function in python.
"""
>>> a=1 >>> str(a) '1' >>> t=('i','t','c','s') >>> str(t) "('i', 't', 'c', 's')" >>> list(t) ['i', 't', 'c', 's'] >>> set(t) {'i', 't', 's', 'c'} >>> z=54.35 >>> int(z) 54 >>> x=88 >>> float(x) 88.0 >>>

Rules to define function name

  • Function names are case sensitive
  • Function names can be alphanumeric
  • Function name can never begin with number
  • Keywords can never be used as function name
  • Avoid have same name for variable and function
  • Function name should begin with letters (a-z,A-Z) or Underscore (_)
  • No spaces are allowed in function name
  • No special symbols are allowed apart from underscore(_)
  • Function name can be of any length

Points to remember

1. Header is the first line of function definition and it has to end with a colon.
2. After header, rest of the body of function can be defined but that must be in indents.
3. If the parentheses is empty it indicates that the function does not contain arguments.
4. Code block can have another function inside it or even simple print statement as per requirement.

Example of function definition in python

""" Author : ITVoyagers (itvoyagers.in)
Date :31st August 2020
Description : Program to define function in python.
""" #function definition def details(): x=input("Enter your name ") print("name is ", x)

Output

Output of function definition and function calling
Output of function definition and function calling

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......



For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
New posts coming soon.......

1 thought on “Best post on Function definition & calling in python -1”

  1. Yοur method of telling the whole thing in this piece of writing is in fact good, aⅼl
    cɑn without dіfficulty be aware of it, Thanks a lot.

    Reply

Leave a Comment