• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 24 pages
Exam (elaborations)

CPSC 217 FINAL Exam Questions and answers, Graded A+/ APPROVED EXAM PREDICTION PAPER/

Document preview thumbnail
Preview 3 out of 24 pages

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
$9.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Terryl
3.3
(16)
Sold
135
Followers
39
Items
8419
Last sold
1 week ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions

Whoops! We can’t load your doc right now. Try again or contact support.