Saturday, July 18, 2020

Types of Function in python

In python, function can be classified into two types based on who is defining the function i.e. pre-defined function and user-defined function
per-defined function can be further classified into Built-in function and function defined in modules

function%20types.JPG

Pre-defined function

These are the function that are present already defined in the system, they are either inbuit or they are defined in some external module

Built-in function:

We have been using these function, for example print(), int(), float(), input etc. to use them we have to simply call them. example:
In [2]:
nm=input("enter your name")
print("hello ", nm)
enter your namebender
hello  bender
in the above program we have used input() and print() inbuilt function, these are already defined in python so we dont have to define or import them

Function defined in modules

These function are defined in module i.e. seperate program files and not in the program itself. import keyword has be used to get the function in calling program
syntax:
import module_name
module_name.function_name()
OR
import module_name as alias
alias.function_name()

In [4]:
import math
val=math.sqrt(25)
print(val)
5.0
In [5]:
import math as m
val=m.sqrt(64)
print(val)
8.0
here the function sqrt() is neither defined by us or nor present by default as simply calling sqrt() will through an error but this function is defined in the math module and can be imported in this script by importing the math module and calling the function by using dot operator(as dot symbolizes that sqrt belogs to math module.we will look into creating modules later

User-defined function

Now these are the function which are custome created by the user, is define in the same program and is called in the same program itself
In [7]:
def sayHello(name):
    print("Hello ", name)

sayHello("tom")
Hello  tom
Here sayHello is the user defined function. You can read all about user defined function here.
References:
1. computer science for class 12 by sumita arora
Share:

Friday, July 3, 2020

Flow of execution in a function call - python

Flow of execution in a function call:

    To understand the program execution we have to know  how a program is organized in the 
computer's primary memory(RAM). Below is a diagram given of a over simplified memory layout diagram


Heap
Dynamic memory allocation
Stack
Function call and local variable
Static/Global 
Stores Global Variable
Source Code
Stores the program instruction
Operating System
Stores the OS codes

This diagram will be used in our further discussion

    As we know that any function in order to work, has to be called with proper arguments. 
So we are going to look into how a function works, starting from the function call till result generation.  
We will consider a very simple addition program that adds two numbers.

def ad(a,b):
    return a+b

print(ad(10,30))

The above code takes 5 steps to be executed. Underneath we have listed the steps


Visualization
The now flow of execution is from top to bottom, line 1 gets executed but as it contains the def keyword 
and the interpreter gets aware that it is a function declaration, so it jumps over it. This shows that a function 
never gets executed unless called
The execution jumps to line 4, this is the point where the function ad is called inside the print function
 and an global frame is created that stores the function ad and its execution will start 
The execution control jumps to line 1 and the stack frame(memory block) for ad is created along with the
 values that are passed as arguments in the function call
Now the return statement is encountered in line 2
The sum operation is executed and the result is generated
Now the stack frame is destroyed and the value is returned to the calling function. Hence we get the output of 40

Points to remember:
1. Execution of a program starts from the first line of the code, executes one line at a time from top to bottom.
2.The function definition never changes the sequence of execution  of a program but the statements inside the function 
are only executed when the function is called.
3. A function can be called from inside of another function.
4. A function can be declared inside of another function declaration. 
5. This idea is similar to recursion.Don’t read the program from top to bottom but read the execution flow


References: 
2. Pythontutor
3. Computer Science with python Sumita arora
Share: