0% found this document useful (0 votes)
5 views

3 Functions

Uploaded by

jiljungjuk737
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3 Functions

Uploaded by

jiljungjuk737
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Functions

- G. Anushiya Rachel
Assistant Professor, CSE
Shiv Nadar University Chennai
Function Definition
• Header
𝒅𝒆𝒇 𝒇𝒖𝒏𝒄_𝒏𝒂𝒎𝒆 𝒂𝒓𝒈𝟏, 𝒂𝒓𝒈𝟐, … 𝒂𝒓𝒈𝑵 :
• To define a function, start with the keyword 𝑑𝑒𝑓.
• This is followed by the name of function and comma separated list of
identifiers called parameters and terminated with a colon.
• Values passed to the function – arguments
• Body
• Contains the sequence of steps to be executed
• Note: A function must be defined before it is called but can be defined anywhere
in the program.

G. Anushiya Rachel, Shiv Nadar University Chennai 2


Function Definition and Call
• Example of function definition
>>> def mul(x, y):
ans = x * y
return ans
• Function call
>>> mul(2, 4)
8
>>> xy = mul(2, 4)

G. Anushiya Rachel, Shiv Nadar University Chennai 3


Function Call through Variable
• Function can be assigned to a variable and the variable can be used to call the
function
a = input(‘Enter 1, 2 or 3: ’)
def one(): if a == 1:
print(‘one’) call_func = one
def two(): elif a == 2:
print(‘two’) call_func = two
def three(): else:
print(‘three’) call_func = three
call_func()

G. Anushiya Rachel, Shiv Nadar University Chennai 4


Types of Functions
• Value-returning function – called for its return value
• Multiple values returned in a tuple
• Non-value-returning function – called for its side effect
>>> def hello(name):
print(‘Hello ’, name)

>>> hello(‘John’)
Hello John

G. Anushiya Rachel, Shiv Nadar University Chennai 5


Positional and Keyword Arguments
• Positional argument – When calling a function, an argument is assigned to a
particular parameter based on its position in the argument list.

• Keyword argument – When calling a function, the arguments are specified by


parameter name

G. Anushiya Rachel, Shiv Nadar University Chennai 6


Default Argument
• Default argument is one that can be optionally provided.

• The third argument, “term” is optional.


• If omitted, the parameter “term” will take up the default value of 20.
• If a third argument is provided, “term” will take up that value.

G. Anushiya Rachel, Shiv Nadar University Chennai 7


Variable Scope
• A local variable is a variable that is only accessible from within a given function.
Such variables are said to have local scope.

• A global variable is a variable that is defined outside of any function definition.


Such variables are said to have global scope.
• The keyword 𝒈𝒍𝒐𝒃𝒂𝒍 can be used to declare a global variable inside a
function.

G. Anushiya Rachel, Shiv Nadar University Chennai 8


Variable Scope – Example

G. Anushiya Rachel, Shiv Nadar University Chennai 9


Variable Scope – Example

G. Anushiya Rachel, Shiv Nadar University Chennai 10


Variable Scope – Example

G. Anushiya Rachel, Shiv Nadar University Chennai 11


Recursion
• Recursive problem solving – A problem is repeatedly broken down into similar
subproblems, until the subproblems can be directly solved without further
breakdown.
• Recursive function – A function that calls itself
• Function execution instance that calls another execution instance of the same
function

G. Anushiya Rachel, Shiv Nadar University Chennai 12


Example – Factorial
• 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1
• 6! = 6 × 5 × 4 × 3 × 2 × 1
• Therefore, 7! = 7 × 6! = 7 × 6 × 5! etc…
1, 𝑛=0
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙 𝑛 = ቊ
𝑛 ∗ 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙 𝑛 − 1 , 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• Recursive factorial function:
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
G. Anushiya Rachel, Shiv Nadar University Chennai 13
Factorial Recursive Instance Calls

G. Anushiya Rachel, Shiv Nadar University Chennai 14


Programming Exercises
• Write Python programs for the following:
1. Obtain an integer from a user and check if the number is a perfect number
or a happy number or both. Write a function to check if the number is
perfect and another to check if it is happy. (Perfect number – Number
whose divisors, except itself, add up to itself. E.g. 28; Happy number –
number for which the sum of the squares of the digits eventually equals 1.
E.g. 203 is happy since 22 + 02 + 32 = 13; 12 + 32 = 10; 12 + 02 = 1)
2. Write a recursive function to count down from N.
3. Write a recursive function to find the sum of digits of an integer.
4. Write a recursive function to find the sum of the elements of a list.
5. Write a recursive function to delete a digit in a specified position in a
number.

G. Anushiya Rachel, Shiv Nadar University Chennai 15


Programming Exercises
6. Write a menu driven program to perform the following operations on a
given sentence. Write a separate function for each of the following:
• Reverse the sequence of words in the sentence.
• List the characters in the string, without duplicates.
• Find the word count and character count (including spaces).
• Check if the string contains a given substring. If no substring is provided,
check if the string contains “ing”.
• Encode the string by replacing each character in position 𝑖 of the string
by the character at position 𝑖 + 1 and 𝑖 + 2 alternately (if you reach the
end of the string, go back to the beginning).
• Exit

G. Anushiya Rachel, Shiv Nadar University Chennai 16


Programming Exercises
7. Write a function that adds ‘ing’ to the end of a given string, provided that
the string has more than 3 characters. If the string already ends with ‘ing’,
add ‘ly’ instead. If there are less than 3 characters, leave the string as it is.
8. Write a function that identifies if a given string is a valid IP address, using
regular expressions. (An IP address is generally four numbers separated by
dots. Each number can range from 0 to 255.)
9. Write a program to obtain a user’s name, employee ID, PAN, and account
number. Write functions to verify if each of these are valid using regular
expressions. The conditions for validity are as follows:
• Name: Only alphabets (upper or lower case), space, and dot
• Employee ID: 5-digit number starting with 2, followed by any 4 digits
• PAN: Contains 10 characters, where the first 5 are alphabets, followed by 4
digits, followed by 1 alphabet
• Account number: A 10-digit number starting with 120
G. Anushiya Rachel, Shiv Nadar University Chennai 17

You might also like