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

6C - Function Within Function - ParamterPassing

This document discusses various ways of implementing functions in Python programs, including: 1. Calling functions within other functions to check if a number is a palindrome. 2. Writing functions to calculate the radius, circumference, and area of a circle given its center point and a point on the circle. 3. Writing a function that returns the quotient and remainder of dividing two numbers. 4. Using default arguments in functions like squaring a number if a power argument is omitted. 5. Implementing functions with arbitrary arguments to sum or print a message to various friends. 6. Using a global variable to track how many times a function has been called. 7. Writing anonymous lambda

Uploaded by

Vijay Udith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

6C - Function Within Function - ParamterPassing

This document discusses various ways of implementing functions in Python programs, including: 1. Calling functions within other functions to check if a number is a palindrome. 2. Writing functions to calculate the radius, circumference, and area of a circle given its center point and a point on the circle. 3. Writing a function that returns the quotient and remainder of dividing two numbers. 4. Using default arguments in functions like squaring a number if a power argument is omitted. 5. Implementing functions with arbitrary arguments to sum or print a message to various friends. 6. Using a global variable to track how many times a function has been called. 7. Writing anonymous lambda

Uploaded by

Vijay Udith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6.

Implementing python programs using Functions

User defined Function

Calling functions within another function:

1. Create a method name reversemethod to reverse an integer passed to it. Create another
function named isPalindrome to check whether the number passed is palindrome or not.
[Note: A number is a palindrome if its reversal is the same as itself]. Write a test program
that prompts the user to enter an integer and reports whether the integer is a palindrome.

2. The following formula gives the distance between two points, (x1, y1) and (x2, y2) in the
Cartesian plane:
Given the center and a point on the circle, you can use this formula to find the radius of
the circle. Write a program that prompts the user to enter the center and a point on the
circle. The program should then output the circle’s radius, diameter, circumference, and
area. Your program must have at least the following functions:
a. distance: This function takes as its parameters four numbers that represent two points
in the plane and returns the distance between them.
b. radius: This function takes as its parameters four numbers that represent the center
and a point on the circle, calls the function distance to find the radius of the circle, and
returns the circle’s radius.
c. circumference: This function takes as its parameter a number that represents the
radius of the circle and returns the circle’s circumference.(If r is the radius, the
circumference is 2πr.)
d. area: This function takes as its parameter a number that represents the radius of the
circle and returns the circle’s area. (If r is the radius, the area is πr2).

Function returning more than one values:

Write a function using that returns the quotient and remainder after dividing two numbers. [Note:
Take care of division by zero]

Default Arguments:
1. Raising a number n to a power p is the same as multiplying n by itself p times. Write a
function called power () that takes an integer for n and an integer value for p, and returns
the result. Use a default argument of 2 for p, so that if this argument is omitted, the
number n will be squared. Write a python program that gets values from the user to test
this function
2. Write a function, mass (density, volume) returns the mass of an object having a density of
‘density’ and a volume of ‘volume’, whereas mass(density)returns the mass having a
density of density and a volume of 1.0 cubic meters. [Mass = Density x Volume]
3. Write a function, repeat ("I'm OK", 10) displays the indicated string 10 times, and repeat
("I'm fine") displays the indicated string 5 times.
4. Write a function to print a count down from n to zero. The default starting value is 7.

Keyword Arguments:
1. Write a function using keyword arguments that returns the maximum of two numbers.
2. Write a function using keyword arguments to calculate the Compound Interest and write
a test function

Arbitrary Arguments:
1. Write a function named helloFriends() to display a message Hello + friendName, to any
number of friends passed to the function
2. Write a function sumUp() to sum up any number of values passed to the function. Test the
function with 3 values and 7 values.
Global Variable:
Write a function that, when you call it, displays a message telling how many times it has been
called: “I have been called 3 times”, for instance. Write a python program that calls this function.
Try implementing this using a global variable to store the count.

Lambda Functions / Anonymous Function


1. Write a Lambda function to find the biggest of 2 numbers
2. Write a Lambda function to calculate the average of 3 numbers.

You might also like