0% found this document useful (0 votes)
6 views

16+funcs,+func+expressions,+closure,+recursion,+the+stack

Uploaded by

eowug
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

16+funcs,+func+expressions,+closure,+recursion,+the+stack

Uploaded by

eowug
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

functions

funcs, func expressions, closure, returning funcs, recursion, the stack


functions
● functions in go are types
○ functions behave as types in go
○ use like any other type
■ declare them as variables
■ pass functions around just as you'd pass types around
■ pass functions just like any other argument / parameter
● pass them into functions as arguments
● return them from functions
■ declare functions inside other functions
○ similar to JavaScript
func main
the entry point for your program
parameter

argument

parameters & arguments


You need the ()

calling a function
parameters

two params
parameters

two params
return
named return
returns

return multiple
variadic parameters
variadic arguments
parameter name does not have to match argument name
exercise
Write a function which takes an integer and returns two values:
● the integer divided by 2
● whether or not the integer is even (true, false)
For example
● half(1) should return (0, false)
● half(2) should return (1, true).

credit: http://www.golang-book.com/books/intro/7
solution to exercise
exercise
Write a function with one variadic parameter that finds the greatest number in a list of numbers.

credit: http://www.golang-book.com/books/intro/7
solution to exercise
bad coding practice
variable shadowing
bad coding practice
variable shadowing
func expression
setting a variable equal to a function
this is not a func expression
this is our code before using a func expression
this is not a func
expression

this is a func expression

func expression
setting a variable equal to a func the scope of greeting is func main()
interesting to look at greeting’s type
another func expression
setting a variable equal to a func
another func expression
setting a variable equal to a func
closure
my definition: “one thing enclosing another thing”
closure
func main encloses func increment
closure helps us limit the scope of variables that are used by multiple functions
without closure, for two or more funcs to have access to the same variable, that variable would need to be package scope
func main is enclosing increment; increment is enclosing x
not using closure
closure helps us limit the scope of variables that are used by multiple functions
without closure, for two or more funcs to have access to the same variable, that variable would need to be package scope
not using closure closure
returning a func
a func is returned

closure
closure helps us limit the scope of variables that are used by multiple functions
without closure, for two or more funcs to have access to the same variable, that variable would need to be package scope
nextEven & masEven are each holding/enclosing the variable i
a func is returned

returning a func
(not part of func expression)

another func expression


setting a variable equal to a func
a func is returned

interesting to look at greet’s type


callback
passing a func as an argument
func visit takes two arguments

another func
a slice of ints the callback
func visit takes two arguments

another func
a slice of ints the callback

in e
of th
ts
e in
sl ass
p
ic
func visit takes two arguments

another func
a slice of ints the callback

ac nc
in e

llb fu
of th

k
ts

ca a
e in

e in
sl ass

th ss
p

pa
ic
the func passed as an argument
(the callback)
is assigned to the parameter “callback”
and then gets used
wikipedia’s description
another callback
can you explain this code?
“If you’ve done functional programming like Lisp or Haskell, this way of dealing with functions is super common;
it’s an approach to development; you get used to passing functions around. Go allows you to do that [passing
functions around] but it’s not the most common way of writing code. The more normal way you’d write code [for
something like the code above] would just be a simple for loop. For loops are easy to understand.”
~ Caleb Doxsey
recursion
a func that can call itself
The End Result:
● 24
Can you pencil out how the answer, 24, was reached?

recursion
● factorial(4)
○ returns: 4 * factorial(3)
● factorial(3)
○ returns: 3 * factorial(2)
● factorial(2)
○ returns: 2 * factorial(1)
● factorial(1)
○ returns: 1 * factorial(0)
● factorial(0)
○ returns: 1
----------------------------------------------------
returns: 4 * 3 * 2 * 1 * 1
----------------------------------------------------

The End Result:


● 4*3*2*1

recursion
This is called the base case

● You can always use loops to solve any


problem that can be solved with recursion.
● Loops are more performant than recursion.

recursion
defer
run this at the last possible moment
the stack
http://www.golang-book.com/books/intro/7
Review
● func main() {} ● recursion
● calling a function ● defer
● greeting() ● the stack
● parameters vs arguments ○ the order in which functions are called
○ two params
○ variadic
■ …params
■ args...
● returns
○ named returns
○ multiple returns
● variable shadowing
● func expression
○ setting a variable equal to a function
○ greeting := func(){<code here>}
■ greeting’s type is func
● closure
○ one thing enclosing another
○ helps us limit scope of variables
● returning a func
○ functional programming
● callback
○ passing a func as an argument
exercises
bool
Write a program that prints the value of this expression:
(true && false) || (false && true) || !(false && false)
two params
Write a program that calls a function which takes first name and age
then returns a string like this, “John is 27 years old.”
two returns
Write a program that calls a function which takes first name and age
then returns an int and a bool
the int: person’s age * 7 (dog years)
the bool: whether or not the person is old (age > 25)
use those two returns in a sentence like this,
(“John is 140 in dog years and is not old”)
or like this, (“Jane is 280 in dog years and is old”)
named return
Write a program that calls a function which takes age
then returns dogYears int which is age * 7
variadic parameters
Write a program that has variadic parameters
use that function in a program
variadic arguments
Write a program that has variadic parameters
use that function in a program, passing in variadic arguments
func expression
Write a program that uses a func expression
variable type
You wrote a program that uses a func expression
now add a print statement that shows
the type of the variable to which the function is assigned
closure
create a program that uses closure
returning a func
create a func that returns a func
use that func in a program
callback
create a program that uses a callback
(a func is being passed in as an argument)
recursion
The Fibonacci sequence is defined as: fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1)
+ fib(n-2). Write a recursive function which can find fib(n).
defer
create a program that uses defer
review questions
Answer These Questions
● What is variable shadowing?
● What is a func expression?
● What is closure?
● What is a callback?
● How does defer work?
● What is the stack and how does it work?

You might also like