# Creating an Accounting Program in Julia, for Pluto

**URL:** <https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227>\
**Category:** Finance and Economics\
**Tags:** question, dataframes, function, pluto\
**Created:** [June 1, 2021, 8:38pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227 "2021-06-01T20:38:12Z")\
**Posts on this page:** 16\
**Page:** 4

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 10:07pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/61 "2021-06-02T22:07:26Z")

</div>

What do you think is the next thing to work on?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 10:09pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/62 "2021-06-02T22:09:02Z")

</div>

OK, putting it all together and a bit of prettier formatting we have now:

```julia-auto
using DataFrames

ledger=DataFrame(
	date=[],
	chequing=[],
	expenses=[],
	income=[]);

function ledgertransaction(
    date,
    credit_account,
    credit_amount,
    debit_account,
    debit_amount
)
	if length(date) == 0
		return (false,"Error -- no date entered")
	elseif !( credit_account in names(ledger) )
		return (false, "Error -- credit account not found.")
	elseif !( debit_account in names(ledger) )
		return (false, "Error -- debit account not found")
	elseif credit_amount-debit_amount != 0			
		return (false,"Error -- credit and debit must match." )
	else
		default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)

		spec = Dict(
			"date" => date, 
			credit_account => credit_amount, 
			debit_account => debit_amount)
		row = merge(default_values, spec)
		push!(ledger,row)
	
		return (true, "there are no errors")	
	end
end

ledgertransaction("Jan 1, 2000","expenses",100,"chequing",100)

```

You should use these code lines for the further steps.

Well done so far!

Let’s take a short break here: It’s important that you understand those issues. What we have learned so far helps to create code which is easy to read. Not for others but at first for you. You need to have readable code for yourself to always see, whats going on. Doing this lets you focus on the important things in coding: the real problems.

I am not sure, if you enjoy those lessons. Of course you are always free to stop this. It only makes sense if you have some fun going through all this!

Now, we have a single one left:

> [@oheil](#):
>
> `ledger` should be considered as being local.

If `ledger` is local, that means, it is not available currently inside `ledgertransaction`, so the function would produce an error. How can we overcome this?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 10:28pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/63 "2021-06-02T22:28:00Z")

</div>

Hmmmm, the ledger can’t be inside the function,because it would recreate the DataFrame anew, and not save the wanted information.

I’m thinking that if there was a function to create the ledger, that was called inside the transaction function, then the variable ledger would be local. Am I on the right track?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 10:30pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/64 "2021-06-02T22:30:15Z")

</div>

Its, again, easier.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 10:40pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/65 "2021-06-02T22:40:25Z")

</div>

why not doing this?  
`function ledgertransaction( ledger, date, credit_account, credit_amount, debit_account, debit_amount ) `

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 10:49pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/66 "2021-06-02T22:49:28Z")

</div>

Where does this go? do I have to have something inside?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 10:54pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/67 "2021-06-02T22:54:09Z")

</div>

You provide `ledger` as a parameter?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 10:58pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/68 "2021-06-02T22:58:10Z")

</div>

Ask, if there is something unclear!

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 11:02pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/69 "2021-06-02T23:02:25Z")

</div>

I’m not familiar with parameters.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 11:17pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/70 "2021-06-02T23:17:25Z")

</div>

That’s not a question, but anyways. functions have parameters. When you call a function you provide values to the function as parameters. See [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/)  
Well it’s _arguments_, not _parameters_, sorry.

So the idea is just, that we provide `ledger` as an argument to the function.  
Arrays ( and DataFrames) are passed to the function as a reference. So the argument `ledger` is the same as our “global” `ledger` when we pass it as argument to the function.  
Like:

```julia
julia> function test(array)
           array[1]=2
       end
test (generic function with 1 method)

julia> a=[1]
1-element Vector{Int64}:
 1

julia> test(a)
2

julia> a
1-element Vector{Int64}:
 2

```

Array `a` is changed by the function.  
If functions change their arguments the convention is to name the function with ! :

```julia
julia> function test!(array)
           array[1]=2
       end

```

Can you transfer this information into a new `ledgertransaction` function?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 11:32pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/71 "2021-06-02T23:32:36Z")

</div>

```julia
function ledgertransaction( ledger, date, credit_account, credit_amount, debit_account, debit_amount )
	ledger=DataFrame(
	
    	date=[],
    
		#Assets
	
		chequing=[],
    	
		#Expenses
	
		expenses=[],
    	
		#Revenue
	
		income=[]);
end

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 11:37pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/72 "2021-06-02T23:37:36Z")

</div>

You don’t need any `ledger1`.  
What’s your motivation for this?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 11:45pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/73 "2021-06-02T23:45:00Z")

</div>

I was testing things, and I forgot I did that.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 6, 2021, 10:13pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/74 "2021-06-06T22:13:29Z")

</div>

```julia
 function ledgertransaction!(
		    date,
   	 		credit_account,
    		credit_amount,
    		debit_account,
    		debit_amount
)
	
		default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
    		spec = Dict(
					"date" => date, 
				credit_account => credit_amount, 
				debit_account => debit_amount)
    		row = merge(default_values, spec)
	
           push!(ledger,row)
       end

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 6, 2021, 11:12pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/75 "2021-06-06T23:12:58Z")

</div>

Why did you create your own package, to manage decimals?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 8, 2021, 12:22pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/76 "2021-06-08T12:22:36Z")

</div>

We are not yet at the milestone 1.  
A note: A function name which ends with an ! is a convention ([Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention)) and denotes function, which modify one or all of their arguments.

We enforce the globale ledger to be local with:

```julia
local ledger=DataFrame(...

```

The complete current code is than:

```julia
using DataFrames

local ledger=DataFrame(
	date=[],
	chequing=[],
	expenses=[],
	income=[]);

function ledgertransaction(
    date,
    credit_account,
    credit_amount,
    debit_account,
    debit_amount
)
	if length(date) == 0
		return (false,"Error -- no date entered")
	elseif !( credit_account in names(ledger) )
		return (false, "Error -- credit account not found.")
	elseif !( debit_account in names(ledger) )
		return (false, "Error -- debit account not found")
	elseif credit_amount-debit_amount != 0			
		return (false,"Error -- credit and debit must match." )
	else
		default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)

		spec = Dict(
			"date" => date, 
			credit_account => credit_amount, 
			debit_account => debit_amount)
		row = merge(default_values, spec)
		push!(ledger,row)
	
		return (true, "there are no errors")	
	end
end

ledgertransaction("Jan 1, 2000","expenses",100,"chequing",100)

```

Now you should get an error, hopefully in Pluto too.

Your task is now to overcome this error only by changing the definition of the function `ledgertransaction`. Hint: you don’t need to change the code of the function, only it’s arguments (and the name by above convention).

Recommended readings are [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) and [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/) .

[Previous page](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227.md?page=3)
