Python Exam functions Questions with complete Solutions
Functions - ## A function is a sequence of instructions with a name. ## When a function is called, the instructions are executed. ## When the execution of the instructions is complete, the function may return a value to the calling program. Function round - price = round(6.8275, 2) ## The function round is called with arguments 6.8275 ## and 2. ## round returns the number 6.83, which becomes the ## value of price. ## The computations within round are hidden from the ## calling program. Function Definition - def cubeVolume(sideLength) : # function header volume = sideLength**3 # function body return volume # function body ## name of function: cubeVolume ## Name of parameter variable: sideLength ## def and return are reserved words ## return exits the function and returns the result Calling a Function from Within a Function - def main() : result = cubeVolume(2) print("A cube with side length 2 has volume" , result) def cubeVolume(sideLength) : volume = sideLength**3 return volume ## The definition of cubeVolume is not required when main is defined main() ## The definition of cubeVolume is required when main is called Branches of a Function - ## A branch of a function consists of a sequence of instructions that are carried out when the function is evaluated ## This function has two branches, one for sideLength < 0 and ## one for sideLength ≥ 0. def cubeVolume(sideLength) : if sideLength < 0 : return 0 else : return sideLength**
Written for
- Institution
- Programming for python language..
- Course
- Programming for python language..
Document information
- Uploaded on
- June 8, 2024
- Number of pages
- 3
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
python exam functions questions with complete sol