why do we use functions
helps us understand the code easier
makes code less repetitive and more organized
makes code reusable
Parameters
The inputs/what goes into a function
Order matters
local variables
defined inside a function/loop and can only be used inside that function/loop
global variables
can be used anywhere in the code
ex: constanants
scope
the area of code that a variable can be used in
why do we need scope
differentiate variables with the same name in different parts of code
(cannot have 2 variables with the same name within the same scope)
What does API stand for
Application Programming Interference
what does api do
Set of tools for buliding programs
Helps two programs connect or communicate with each other
Helps devices connect and connects devices to programs
API examples
spotify, twitter
return value
returns the value when the function is called, does not print value unless println is used
What part of the function is bolded?
function start(){
var numApples = 4;
println("Before: " + numApples);
var twiceAsMany = doubleNumber(numApples);
println("After: " + twiceAsMany);
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
calling the function
What part of the function is bolded?
function start(){
var numApples = 4;
println("Before: " + numApples);
var twiceAsMany = doubleNumber(numApples);
println("After: " + twiceAsMany);
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
defining the function
What part of the function is bolded?
function start(){
var numApples = 4;
println("Before: " + numApples);
var twiceAsMany = doubleNumber(numApples);
println("After: " + twiceAsMany);
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
function body
What part of the function is bolded?
function start(){
var numApples = 4;
println("Before: " + numApples);
var twiceAsMany = doubleNumber(numApples);
println("After: " + twiceAsMany);
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
return value statement
What part of the function is bolded?
function start(){
var numApples = 4;
println("Before: " + numApples);
var twiceAsMany = doubleNumber(numApples);
println("After: " + twiceAsMany);
}
function doubleNumber(x){
var doubledX = 2 * x;
return doubledX;
parameter
syntatic validation
enforces correct syntax on structures fields
ex: 9 digits in SSN, date in correct form
semantic validation
enforces the correctness of the values in the specific business context
ex: start date is before end date