Easy fruitful & void function,return values,stack diagrams-3

Fruitful functions & void function, return values and stack diagrams

Fruitful function and Void function

Python provides various functions which may or may not return value.
The function which return any value are called as fruitful function.
The function which does not return any value are called as void function.
Fruitful functions means the function that gives result or return values after execution.
Some functions gets executed but doesn’t return any value.
While writing fruitful functions we except a return value and so we must assign it to a variable to hold return value.

In void function you can display a value on screen but cannot return a value.
A void function may or may not have return statement, if void function has return statement then it is written without any expression

Void function without return statement

def fun1():
    print("Python")

Void function with return statement

def fun2():
    print("ITVoyagers")
    return

Void function with parameter without return statement

def fun1(a,b):
    print("sum of a and b", a+b)
fun1(5,2)

In above example main function have passed values to given function and as print statement is used function will not return anything it will just print value.

Fruitful function with parameter and return statement

def fun2(a,b):
    return a+b
print(fun2(5,8))

In above example we have passed two values to the given function which then returns sum of that values to main function using return keyword.

In interactive mode result is displayed directly on screen

>>> import math
>>> math.sqrt(6)
2.449489742783178

In script mode above function does not return a value and becomes of no use.

import math
math.sqrt(6)

When you try to assign a void function to a variable it displays a special value called as None.
None is a legal empty value returned by void function in python.

"""
Author : ITVoyagers (itvoyagers.in)

Date :3rd September 2020

Description : Program to show fruitful and void functions

"""
fruitful and void function
fruitful and void function

"""
Author : ITVoyagers (itvoyagers.in)

Date :3rd September 2020

Description : Program to show void functions returning None type

"""
def add_me(x,y):
    print("sum is",x+y)

p=add_me(4,3)
print(p)

Output

>>> 
sum is 7
None

Return Values

Various built-in functions like math functions produces results or return values.
When a function generates return values it can be assigned to a variable for holding the result.
This stored result can be used further in the script.
To make a function fruitful return keyword is used.
Once a return statement is executed the function terminates without executing immediate statements.
Code that appears after return statement or any place where the flow of execution cannot reach is called as dead code.
Values returned can be a literal(return 3, return 4-2), variable(return a), expression(return (a+b)/c) etc.

"""
Author : ITVoyagers (itvoyagers.in)

Date :3rd September 2020

Description : Program to show return statement

"""
def product(a,b):
    c=a*b
    return (c)
y=product(5,2)
print("product is",y)

Output

>>> 
product is 10

Stack Diagrams

Stack diagrams show the value of each variable, but they also show the function each variable belongs to.
A stack diagram is used to represent the state of a program during a function call.
Stack diagrams are useful to track variables application in code.
Each function is represented by a frame.
A frame is a box with name of a function outside and variables of function inside it.
Frame describes which function is called and variables and parameters which are used during function call are added inside frame.
All parameters and arguments represents their value by using “–> ” and not an assignment operator(=)

Example

def avg_me(x,y):
    s=x+y
    avg=s/2
    print("avg", avg)    

p=4
q=3
avg_me(p,q)

Stack Diagram

Stack diagram

In above example avg_me() is a function and it was called in __main__ so main is represented in top most frame.
Whenever we create a function any variable outside function belongs to main function.
Each parameter refers to the same value as its corresponding argument.
Hence value of x is same as value of p.



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
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment