Table of Contents
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 """

""" 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

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:
For other advanced python related posts: