0% found this document useful (0 votes)
7 views23 pages

001 Functions

The document outlines the fundamentals of writing functions in programming, emphasizing their reusability and modularity. It explains the process of defining and running functions, as well as the use of arguments to accept inputs. Additionally, it highlights the importance of return statements in capturing values from functions.

Uploaded by

markmaksi97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

001 Functions

The document outlines the fundamentals of writing functions in programming, emphasizing their reusability and modularity. It explains the process of defining and running functions, as well as the use of arguments to accept inputs. Additionally, it highlights the importance of return statements in capturing values from functions.

Uploaded by

markmaksi97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

FUNCTIONS

The last "big" topic


GOALS
Write valid functions
Write functions with arguments
Compare function expressions &
function statements
Write a bunch of functions!
FUNCTIONS
Reusable procedures
Functions allow us to write
reusable, modular code
We define a "chunk" of code
that we can then execute at
a later point.
We use them ALL THE TIME
2 STEP PROCESS
DEFINE RUN
DEFINE
function funcName(){
//do something
}
DEFINE
RUN
funcName(); //run once

funcName(); //run again!


RUN
ARGUMENTS
INPUTS
Right now, our simple functions
accept zero inputs. They behave
the same way every time.
NO INPUTS
grumpus()

grumpus()
NO INPUTS
greet() "Hi!"

greet() "Hi!"
ARGUMENTS
We can also write functions that
accept inputs, called arguments
ARGUMENTS
greet('Tim') "Hi Tim!"

greet('Anya') "Hi Anya!"


ONE MORE
avg(20,25) 22.5

avg(3,2,5,6) 4
We've seen this before
No inputs

Arguments!
GREET TAKE 2

"Hi, Arya!" "Hi, Ned!"


2 ARGUMENTS!
"77 is
larger!"

"33 and 33
are equal"
RETURN
Built-in methods return
values when we call them.
We can store those values:
NO RETURN!
Our functions print values out,
but do NOT return anything
FIRST RETURN!
Now we can capture a return
value in a variable!
RETURN
The return statement ends function
execution AND specifies the value to
be returned by that function
PRACTICE

You might also like