You are on page 1of 8

Module 6: Functions

Module 6: Functions
Module 6: Functions
 Functions
 Local variables ,Global variable
 Default Argument Values
 Returning Values
 Keyword & Positional Arguments
 Arbitrary Argument Lists
Module 6: Conditional statements/Control Structures

 Function
What is a function in Python?
In Python, function is a group of related statements that perform a specific task.
Functions help break our program into smaller and modular chunks. As our program grows larger
and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes code reusable.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Module 6: Conditional statements/Control
Structures
 Function Example
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Example:
def greet(name):
"""This function greets to the person passed in as parameter"""
print("Hello, " + name + ". Good morning!")
Module 6: Conditional statements/Control Structures

 Global and Local Variables in Python


 Global variables are the one that are defined and declared outside a function and we need to use them
inside a function.

# This function uses global variable s


def MyFavLanguage():
print(lang)

# Global scope
lang = "I love Python"
MyFavLanguage()
Module 6: Conditional statements/Control Structures

 Default Argument Values


 Function arguments can have default values in Python.
 We can provide a default value to an argument by using the assignment operator (=). Here is an example. def greet(name, msg
= "Good morning!"):
""" This function greets to the person with the provided message. If message is not provided, it defaults to "Good morning!" """
print("Hello",name + ', ' + msg)
greet("Kate")

greet("Bruce","How do you do?")


Module 6: Conditional statements/Control Structures

 def CalTax(salary):
"""this fucntion is for calcuation of
number"""
AfterTaxSalary =salary - (salary*0.1)
return AfterTaxSalary
salary=100
print("My Earning
",salary)
x=CalTax(salary)
print("Salary After
Tax:",x)
if x>=90:
print("your are elible for charity")
x=x-10
print("your are elible for charity
10")
print("Final Salary",x)
Module 6: Conditional statements/Control Structures

def add(**kwargs):
for key,value in kwargs.items():
print("key=",key)
print("value=", value)
add(a="sd",b="brwer",c="sffds",d="as
doiuo")

You might also like