CPSC 217 FINAL Exam Questions and answers, Graded A+/ APPROVED EXAM PREDICTION PAPER/
CPSC 217 FINAL Exam Questions and answers, Graded A+ What is the best way to create functions for a program? Give example - -Top down approach, break it down into parts ex. Program to calculate interest, break it into 3 functions 1. Get info 2. Do Calculations 3. Display results What is the result of and how does it work? def displayInstructions ( ): print ("Displaying instructions") displayInstructions ( ) print ("End") - -You created a function called displayIntructions. The body of that function has the print function in it which will have the output of: "Displaying instructions" Then you called on your newly defined function, so this executes the function (ie, its body) and then the last output is: "End" True or false: once a function is executed one time, it can't be called again, even if you try and call the function multiple times. - -False! You can reuse a function as many times as you want/is called What is the difference between a local variable and global variable? - -Local variables are stored within function bodies. Global variables are generally defined at the beginning of the code and can be accessed anytime Why have local variables? - -Since variables are memory locations, they take up a portion of memory of the program. So by creating local variables, the memory is used up temporarily, and then once the function is executed, that memory frees up. When a constant or a variable is in scope, we really mean...Give example - -It is somewhere that it can be used. Ex. a local variable is in scope of the function it is under What is parameter passing? What's often forgotten (2)? - -Passing local variables from one function to another. The number of parameters passed into a function, must equal the number of parameters/variables that are called on. It also needs to match exactly what is called on in the start function. Is this correct: def start ( ): print (num) - -No, no parameter 'num' passed into start, so it can't access that memory location called 'num'. To fix it, must have: def start (num): print (num) What does the return function do? What is often forgotten? Give example - -It communicates a copy of information out of a function (back to caller) as function ends. THE RETURN VALUE MUST BE STORED IN THE START FUNCTION def square (num): result = num * num return (result) def start ( ): aStr = input ("Enter a number: ") num = int(aStr) *result* = square (num) print (result) Difference between parameter passing and return values? - -Parameter passing passes info INTO function before it executes (passes memory location of variable into function) Return values communicate info OUT of a function as the function ends What are some good style of functions that should be implemented? - -1. Function should have a well defined task 2. Have a self-descriptive name 3. No longer than 1 screen 4. Use variable naming conventions (lower case, camel-case, or underscore) Why should you not use global variables? Give an example - -Can accidentally be changed anywhere - since they can be accessed from anywhere, makes code harder to trace num = 1 def fun ( ): *global* num #calls in global num num = 2 #changes global num from 1 to 2 print (num) def start ( ): print (num) #1 fun ( ) print (num) #2, global variable changed after fun ( ) start ( ) What are some common mistakes when calling in functions (4)? - -1. Not defining the
Document information
- Uploaded on
- February 6, 2024
- Number of pages
- 24
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers