Review Session 1

Report 1 Downloads 98 Views
Review Session #1 With Sarah Ganci

About Me • Junior • Majors: Computer Science BA, Linguistics BA • Minor: Speech and Hearing Sciences • Hometown: Cary, North Carolina • Interests: hiking, crafts, podcasts, Rick and Morty, The Bachelor

Objectives • Functions • Parameters vs Arguments • Expressions • Syntax reminders

What are functions? • Think of functions like recipes • Functions are reusable instructions • Functions can in some inputs (ingredients) and return values (yummy baked goods)

• We see functions in two contexts: • Definitions • Calls

Function Definitions • Parts of function definitions: • Name: the name of our function • Parameters: place holder variables for expected inputs • Return type: the type of value we want to return from the function • Body: the statements or lines of code that make up the function • This will often include return statements

• We define functions using the following syntax: let = (<parameters>): => { //function body }; let applePie = (apples:string, sugar:string) : string => { return “We combined “ + apples + “ with ” + sugar + “ and now we have apple pie yummm”; };

Parameters • Parameters are place holder variables for future inputs • Parameters are found inside the parenthesis of a function definition • We write parameters in a comma separated list of names and types let = (<parameter1>:, <parameter2>:) : => { //function body };

• Ex: let paulyD = (phrase : string): string => { return “Oh yeah, “ + phrase + “, yeah!”; }; //phrase is a parameter

Return Statements • Return statements return a value to the caller of the function • The returned value’s type must match the specified return type let add = (x: number, y: number, z:number): number => { return x + y + z; };

• As soon we reach a return statement, we exit the function • When we return a value to the place where the function was called, the function call evaluates to that value

Return vs. Print • When we return a value, the function call evaluates to that value add(2,4); //this evaluates to 6

• When we print a value, the value is projected into the display on the browser print(6); //6 will appear in the browser

• They are very different but we often use them together print(add(5,5)); //add returns 10 and then we print that value

Calling functions • To run the code in our functions, we call them • If the function definition has parameters, we must pass values (arguments) into the function call • Arguments must match parameters in type and order • To call functions we use the following syntax: (<arguments>); paulyD(“COMP110”);

Arguments • If a function’s definition has parameters, we need to pass in corresponding arguments when we call the function • Arguments are found in the parenthesis of a function call • We write arguments in a comma separated list of values • Arguments are substituted in for parameters • Ex: (, , ); let hype : string = paulyD(“COMP110”); // “COMP110” is an argument passed into the function paulyD // “COMP110” is substituted for the parameter called phrase // this would return “Oh yeah, COMP110, yeah!”

Parameters vs. Arguments Parameters

Arguments

• Where: function definitions • What: placeholder variables • Ex:

• Where: function calls • What: actual values • Ex:

let quickMaths = (x: number, y: number, z: number): number => {

let total: number= quickMaths(2,2,1); // 2 substitutes in for x // 2 substitutes in for y // 1 substitutes in for z // 2 plus 2 is 4 minus 1 that’s 3 // 3 is returned

return x + y - z; };

Function trace

parameters

return type

let mystery = (x : number, y : number , z : boolean) : number => { if (z){ return x+y; function definition

}else { return x-y; } };

function call arguments

let ex1 : number = mystery(1,2,true);

3

let ex2 : number = mystery(mystery(1,2,true), 3 ,false); 3

0

Hands on: Ice Cream cost calculator • Imagine we are going to an ice cream shop. We want to write a function to calculate the cost of our ice cream cone. • We know: • Each scoop costs $1.50 • Each topping costs $0.50 • Hot fudge is $0.75

• Write a function with the following properties that will calculate this cost • Name: whatever you like J • Parameters: number for scoops, number for toppings, boolean for hot fudge • Return type: should return a number (the total cost)

Solution

Expressions • Evaluate to a single value at runtime • Key question: can it be simplified? • Anywhere you can write an expression, you can substitute any other expression of the same type • Many different types of expressions (not limited to those below) • • • • • •

Literals (number, string, boolean) Variables Concatenations Boolean operators Function calls Any combination of these

Spot the Expressions let sum : number= add(add(1,2),2); if(sum+7>10){ print(“sum is: “ +sum); } let isWednesday : boolean = !false;

Syntax reminders • Bracket alignment • If statements and functions need opening and closing curly braces

• Semicolons • Don’t forget the semicolon at the end of your statements and at the end of function definitions

• Indentation • Won’t actually effect how your code runs, but it makes code more readable