Skip to content

How to declare a function with more than one statement? #3128

Answered by dvd101x
0x70b1a5 asked this question in Q&A
Discussion options

You must be logged in to vote

I think there are specific plans to implement functions with multiple statements according to #2701

In the meantime a workaround is that elements of an array can include expressions with assignments.

f(x) = [d = 1, d][end] # evaluate all expressions in the array and return only the value of the last expression.
f(3)                   # 1

You have to be very careful with your naming convention as the expressions will assign values in the scope (there are not function scopes)

d = 5
f(x) = [d = 1, d][end] # evaluate all expressions in the array and return only the value of the last expression.
d                      # 5
f(3)                   # yields 1 but overrides the value of d
d        …

Replies: 2 comments 4 replies

Comment options

dvd101x
Jan 16, 2024
Collaborator Sponsor

You must be logged in to vote
4 replies
@0x70b1a5
Comment options

@dvd101x
Comment options

dvd101x Jan 17, 2024
Collaborator Sponsor

@0x70b1a5
Comment options

@dvd101x
Comment options

dvd101x Jan 17, 2024
Collaborator Sponsor

Answer selected by 0x70b1a5
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants