Assessment Unit 7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

var smallest; findSmallest(34, 99); function findSmallest(num1, num2) { if(num1 < num2){ smallest = num1; } else { smallest = num2; } }

Watch out for global variables! If a function accesses or updates a variable elsewhere in your program, that function shouldn't be shared as is. Hint: What about using a return?

Takeaways of parameters and return values

- Functions with parameters and return values help us simplify our code - Functions can only return one value at a time A function can have: * No parameters and no return values * Parameters, but no return values * Return values, but no parameters * Parameters and return values

var cakeCalculator = cakeCost(3, "lemon");

After the expression is evaluated, cakeCaculator stores the value 12.

API

Application Program Interface - specifications for how functions in a library behave and can be used

Look at Level 1

Check for Understanding

Takeaways of Libraries

Creating a library: + Build functions + Add documentation Share as a Library + Using a library: + Click "Manage Libraries" + Either choose a classmate's library, or paste in a library code + Call the functions by writing the library name, a dot, the name of the function, and including any arguments for the parameters Testing a library: + Use console.log as the end user to test functions in a library + Check that the output is what you would expect + Read the library code if something does not work correctly, and contact the library owner if something needs to be changed.

Why is it important to use meaningful names for the functions in your library?

It is important to give functions in libraries meaningful names so that other people who may want to use the library can understand and remember, at least roughly, what each function does.

How can we easily share functions between programs?

This is a library - a collection of functions that can be used in many different programs.

Library

a group of functions (procedures) that may be used in creating new programs

function findSmallest(num1, num2){ if(num1 < num2){ return num1; } else { return num2; } }

findSmallest: Given two numbers, finds the smallest - num1 {number} - first number - num2 {number} - second number - return {number} - the smaller of the two numbers

Look @ check for understanding at end of Lesson 7

n/a

Extracting shared features to generalize functionality is known as . . .

procedural abstraction

Argument

the value passed to the parameter

What are the benefits of hiding all of the code for filtering the dataset in a library?

- Simplifies the code - Reduces the chance of bugs (assuming the functions in the library are bug-free) - More efficient

A library should have documentation for the included functions:

- how each function works - a complete list of the parameters - what (if anything) will be returned

The procedure below will be called twice with two numbers provided as arguments to the parameter myNumber. Which of the following pair of numbers will mystery return the same value? PROCEDURE mystery (myNumber) final < -- 0 IF (myNumber > 100) final < -- 100 ELSE IF (myNumber < 0) final < -- 0 ELSE final < -- myNumber return final;

1) 0 & 100 2) 0 & 10 3) 100 & 200 * 4) 30 & 50

Evaluate if the following function is a good candidate to be placed in a library. Why or why not? (Level 5 UNIT 7)

This function is not a good candidate to be placed in a library. The creator of the library is attempting to update the global variables player1Points and player2Points. Because this is being peformed inside of a function, the updated scores for each player won't be retained unless someone creates the global variables player1Points and player2Points in their program. Alternatively, the function may be rewritten using the parameters playerPoints (existing score of each player) and points (additional score to be added) instead: function updateScore(playerPoints, points) { Return playerPoints + points }

Let's quickly review Algorithms:

Up to this point, most of the algorithms you've used you created yourself, or you modified existing code. Notice here how we can build new functions by combining the existing library functions - essentially we are creating new algorithms by using existing algorithms (library functions).

What are the benefits of writing functions that use parameters and return? Try to list at least two.

Writing functions that use parameters and return helps improve code readability and manages complexity by reusing code. Instead of writing two separate functions, it is possible to combine the two by using parameters.

Prompt: What are the benefits of using existing algorithms instead of brand new algorithms?

+ the maximum or minimum of 2 or more numbers + the sum or average of 2 or more numbers + an algorithm that determines if an integer can be evenly divided by another integer + a robot's path through a maze

What makes a good library function? How can you make sure that the end users of your library have what they need in order to use your functions?

top of head

Return

used to return the flow of control to the point where the procedure (also known as a function) was called and to return the value of expression

A library needs a name.

Follow these rules: No spaces Capitalize the first letter This library can now be shared with others. They can use the functions within their own program as long as they follow the rules set forth in the documentation.

what is the MOD

MOD is the remainder that is left after a number is divided by another number 3 % 2 MOD = 1 17 % 5 3 r 2 MOD = 2

The Math library is built into App Lab.

Math.round (5,10) 1) Name of library (math) 2) name of function (round) 3) parameters (5, 10)

What potential problems could come up if I tried to use a function without knowing what it does or how to interact with it?

This would be similar to looking at the front of a function envelope and having to guess: - what the function does - what data type the parameters need - what is returned

Takeaways of parameters and procedural abstraction

Using parameters allows the functions (also called procedures) to be generalized. * Using procedural abstraction helps improve code readability. * Procedural abstraction manages complexity by allowing for code reuse. * For example: the function move(id, direction) could be used to move an element in any direction, rather than writing separate functions for each direction.

Parameter

a variable in a function definition. Used as a placeholder for values that will be passed through the function

Modularity

the subdivision of a computer program into separate subprograms.

when is using MOD useful

* A common usage is to determine if a number is even or odd. If you divide any number by two and there is no remainder, the number is even! * You can use MOD to determine if a number is divisible by another number.

What does it mean to return total?

A return does two things: 1. It stops the flow of the function. If a return is inside of a conditional, if that condition is met the function ends there. 2. It returns a value to the place where the function was called. * A function return value can be stored in a variable.

makeCake - creates a cake * tiers {number} - the layers of the cake * flavor {string} - the flavor of the cake cakeCost - calculates the cost of making a cake * tiers {number} - the layers of the cake * flavor {string} - the flavor of the cake * return {number} - the cost of making the cake avgCake - calculates the most common cake * cakeList {List} - a list of strings of cake names * return {string} - the most common cake

This detailed type of documentation is also known as an: Application Program Interface(API) APIs are specifications for how the functions in a library behave and can be used.

Debugging

the process of finding and fixing problems in code


Set pelajaran terkait

Community Final Back of book questions

View Set

Individual Life Insurance Contract - Provisions and Option

View Set

Chapter 24 Masonry Materials-I (Mortar and Brick)

View Set