06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 1. INTRODUCTION 20<br />

In [37]: iseven(5)<br />

Out[37]: false<br />

In [38]: isodd(5)<br />

Out[38]: true<br />

Def<strong>in</strong><strong>in</strong>g functions<br />

There are three ways to def<strong>in</strong>e a function. Here is the basic syntax:<br />

In [39]: function squareit(x)<br />

return x^2<br />

end<br />

Out[39]: squareit (generic function <strong>with</strong> 1 method)<br />

In [40]: squareit(3)<br />

Out[40]: 9<br />

There is also a compact form to def<strong>in</strong>e a function, if the body of the function is a<br />

short, simple expression:<br />

In [41]: cubeit(x)=x^3<br />

Out[41]: cubeit (generic function <strong>with</strong> 1 method)<br />

In [42]: cubeit(5)<br />

Out[42]: 125<br />

Functions can be def<strong>in</strong>ed <strong>with</strong>out be<strong>in</strong>g given a name: these are called anonymous<br />

functions:<br />

In [43]: x-> x^3<br />

Out[43]: #5 (generic function <strong>with</strong> 1 method)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!