Functions in Python
Functions in Python
Advantages:
•Development made easy and fast
•Avoids repetition
ELEMENTS OF A FUNCTION
def a1(x, y): # function header
print( x+y) # function body
Implicit Explicit
OUTPUT: 5.0
¢ Positional arguments
¢ Default arguments
¢ return 6+4
¢ return a**2
¢ return (a+8/3)/b
¢ return (a+b)/c
OUTPUT
CODE
from math import pi
print("built in value of pi is:", pi) # pi is a built-in function
# Global Scope
pi = 3.15
def outer():
# Enclosing Scope
pi = 3.14159
def inner():
# Local Scope
pi = 3.141
pi=pi * 10
print("inner:", pi) # This will print 'local pi'
inner()
print("outer:", pi) # This will print 'enclosing pi'
outer()
print("global:", pi) # This will print 'global pi'
QUESTION1
Write a function to create the following figure:
**********
* *
* *
**********
QUESTION 2
Display a sequence using function. Eg.
2,5,8,11,14,17,20 (start is 2, end is 20 and step is 3)
QUESTION 3, 4
Write the output of the following:
Global keyword
required for immutable
data type
MODIFYING GLOBAL VARIABLE INSIDE A
FUNCTION