Functions PDF
Document Details
![InstrumentalGroup](https://quizgecko.com/images/avatars/avatar-5.webp)
Uploaded by InstrumentalGroup
Mansfield High School
Tags
Summary
This document provides an introduction to programming functions in computer programming. Topics covered include function definition, function call, and examples of returning values with functions using programming language examples.
Full Transcript
Functions Presented by Asia Thought Exercise: Morning Routine Make a list of everything you do in the morning to get ready for the day. Morning Routine Brush Teeth Apply Toothpaste 1) Take a Shower 1) Enter Restroom 1) Locate To...
Functions Presented by Asia Thought Exercise: Morning Routine Make a list of everything you do in the morning to get ready for the day. Morning Routine Brush Teeth Apply Toothpaste 1) Take a Shower 1) Enter Restroom 1) Locate Toothpaste 2) Get Dressed 2) Apply Toothpaste 2) Grab Toothpaste 3) Make Bed 3) Rub Brush on Teeth 3) Uncap Toothpaste 4) Eat Breakfast 4) Spit Out Paste 4) Locate Toothbrush 5) Brush Teeth 5) Rinse Mouth 5) Grab Toothbrush 6) Brush Hair 6) Put Away Items 6) Place toothpaste tube over toothbrush 7) Leave for School 7) Clean Sink 7) Squeeze Review: Data Types 42.56 “text” variable undefined We know that we can store numbers, strings, and other types of data inside variables. Values and Functions Hi I’m Alex let name = "Alex"; sayHello(name); Variables are something. Functions do something Function Definition let introduce = function() { console.log(“I’m a function!”); } Use the function keyword followed by some parentheses and curly braces. Within the curly braces we define the code (or task) of our function. When we create our functions we can assign them to a variable. Function Call sayHello(); When we want to use the function, we call it by using the name of the function followed by parentheses. Why use functions? Functions let us recycle our code. Functions help us avoid writing the same code over and over again. Functions make our code easier to read. Parameters let addTwoNumbers = function(a,b){ console.log(a + b); } parameters Parameters are variables that are declared inside the function so that they can be used inside the function. Arguments arguments addTwoNumbers(3, 5) console.log(3 + 5); Arguments are the values that we pass in to a function. They line up with the parameters from the function definition. Return Values let multiplyTwoNumbers = function(x,y) { return x * y; } Functions can return values, when a function is called this is the value that it evaluates to. Return Ends the Function let multiplyTwoNumbers = function(x,y) { return x * y; console.log(“This will not run”); } When a function reaches a return statement, it exits the function and gives us the return value. Anything after the return statement will not run. Review In addition to storing data inside of variables, we can also store behavior for later use with functions. Functions have to be defined before they can be called. Functions can receive values called arguments and return values as well.