Wednesday, 8 May 2024

PlantUML: define function and return the value

A function name should starts with $ and all the argument names should start with $.

 

Unlike procedures, a function can return a value.

 

A function can be called from other function and can be called from other procedure.

 

functions.txt

@startuml

!function $sum($a, $b)
	!return $a + $b
!endfunction

!function $sub($a, $b)
	!return $a - $b
!endfunction


!$a = 20
!$b = 5

sys1 -> sys2 : Sum of $a and $b is $sum(20, 5)
sys1 -> sys2 : Sub of $a and $b is $sub(20, 5)

@enduml

Above snippet generate below diagram.


 


If a function has one liner body, then we can use below shorthand notation.

 

sys1 -> sys2 : Sum of $a and $b is $sum(20, 5)

sys1 -> sys2 : Sub of $a and $b is $sub(20, 5)

 

 

functionShortForm.txt

@startuml

!function $sum($a, $b) !return $a + $b

!function $sub($a, $b) !return $a - $b

!$a = 20
!$b = 5

sys1 -> sys2 : Sum of $a and $b is $sum(20, 5)
sys1 -> sys2 : Sub of $a and $b is $sub(20, 5)

@enduml

@startuml

!function $sum($a, $b) !return $a + $b

!function $sub($a, $b) !return $a - $b

!$a = 20
!$b = 5

sys1 -> sys2 : Sum of $a and $b is $sum(20, 5)
sys1 -> sys2 : Sub of $a and $b is $sub(20, 5)

@enduml

Previous                                                    Next                                                    Home

No comments:

Post a Comment