{"id":"po91OMr3OdQ","title":"Lesson 4: Functions in Daml","description":"In this lesson, we'll cover the basics of writing functions and testing them. Functions in Daml capture the logical aspect of a workflow that we want to model in a smart contract.  Testing is an integral part of writing templates, and we'll write a script that calls the function and passes the required number of parameters. We'll use the debug function to print the return value, and the dollar sign following the debug function is a shorthand notation. We'll also provide an example of adding two numbers of type int and test it using a debug statement. If you're interested in learning more about Daml, this tutorial is a great place to start.\n\nFor a better learning experience, sign up and enroll in this free course at: https://daml.talentlms.com/plus/catalog/courses/141","channel_name":"Digital Asset Training","published_at":"2025-02-19T00:00:00.000Z","thumbnail_url":"https://i.ytimg.com/vi/po91OMr3OdQ/hqdefault.jpg","transcript_text":"functions in Dam will capture The Logical aspect of a workflow that we want to model in a smart contract in this lesson we'll start with writing simple functions introduce a few notations and then write a simple test script to test out the functions let's start with creating a file called functions. daml the first line in this file we'll write is module functions where there are a few rules for a module this statement declares the module and must have the same name as the file name it has to be the first statement and all module names must start with a capital letter to declare a function we first provide its signature a function signature has the function's name and must start with a lowercase letter in this example the name of the function is increment then there are data types separated by Arrow notations the data types listed first in the signature are the data types of the input arguments you can have multiple input arguments the last data type of a signature is always the expected result daml has strong type checking so you'll get an error if the data type of the actual results doesn't match the expected data type in the signature in this example the signature of the increment function means that it takes one argument of type int and produces a result of type int for example we say that this function increment Maps an INT value to another int value so the right arrow can be read as Maps 2 the next line contains the actual definition or implementation of the function so if we call the increment function with a value n it'll be mapped to n + one notice that unlike imperative languages where we often pass arguments inside of parentheses the arguments and daml functions are listed next to the function name and separated by a space let's look at how we'll test this function in daml testing is an integral part to writing templates to write a test we start by writing a script which requires importing the dl. script Library into this module then we declare a test script a script in daml is also a type of function that captures an action written inside a du block a do block gives us an easy way to run multiple Expressions we'll only use one in this example but it's a good practice to follow for test scripts here in the Declaration we first give the name of the script test functions followed by a colon notice that there are no data types mentioned here which means there are no input arguments the empty parenthesis indicate that the script returns nothing then we Define the function by writing its name followed by equal sign and then script followed by a do block and inside this do block we have just one statement which is a call to the dbug function debug is a function that prints a value to the script results we use use this function for testing and debugging purposes the dollar sign following debug is a short hand notation or syntactic sugar you can think of it as parentheses that enclose what needs to be printed we want to print the result of the increment function when we pass three to it so the dollar sign first gets the result of the increment function and then passes it to the debug function which get printed to the script results as four so let's try this all in daml studio Studio we'll start by creating a new project with the daml SDK installed open a terminal where you'd like to start the project and enter daml new functions then change directory and open the files in the folder in Visual Studio code with daml Studio the daml SDK provides some boilerplate code and if we look in the daml folder and open the main. daml file you'll notice a few things here that we've already discussed there are functions and do blocks here with script tags as well for now we'll delete all this except the module main where line up the top now we can write our increment function just like before we'll start with the function signature notice that we get some squiggly lines here from the daml SDK linter saying this signature lacks an accompany binding well let's do that with the function declaration that's better now we we write the script to test it ideally we would write our test code in a separate module but to keep it simple we'll write it in the same file for now again we get the signature lack and accompany binding error but also an error saying script is not in scope remember we need to import dam. script and we need to write our script function declaration so here's the test functions script declaration and then the definition as test functions equals script do to write the next statement inside this block we need to indent Expressions that are in the DU block daml is a whit space sensitive language so the scope of whatever we write is based on how deep it is indented inside a block so we'll indent and write the debug statement as debug dollar sign increment 3 once everything compiles in the background you'll see a clickable statement saying script results click on it and the script will run and you'll see the result in another tab so this is how we write and test functions in daml remember how we mentioned the dollar sign is replacing the use of parenthesis Let's test that by writing another expression with debug that will take the function of increment 4 in parenthesis and just to practice some more let's write one more function this time we'll add two numbers of type int we'll start with the function signature add followed by a colon this function will take two integers as arguments and map them to a value of type int so we need to write int Arrow int Arrow int now the function's definition here we say add XY = x + y to test this we simply add another debug statement debug dollar sign add 33 our script updates the trace section and we see the results one last thing before we move on the function signature you provided is to some extent redundant because daml can infer the type of arguments and return value for the function definition you provided so if you remove the signature for increment you'll see that the compiler automatically fills in the inferred type we can do the same for the signature for ADD but it comes up slightly more cryptic in this example the ad function takes a where a is a type of additive that is something which can be added additive is a class in daml and you'll learn more about classes such as additive and multiplicative in the future for now try writing some functions to multiply two numbers or maybe compute a complex expression let's review what we covered functions are declared inside a module a function requires a signature and a definition the signature specifies the the function name and the data types of its arguments and return values are separated by right arrows the function definition specifies the actual implementation to test a function we can write a script that calls the function and passes the required number of parameters and we print the return value by calling the debug function and passing the function to it with the dollar sign notation or parenthesis for","transcript_synced_at":"2026-10-26T04:02:45.996Z","segments":[{"start_seconds":0,"text":"functions in Dam will capture The Logical aspect of a workflow that we want to model in a smart contract in this lesson we'll start with writing","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=0s"},{"start_seconds":8,"text":"simple functions introduce a few notations and then write a simple test script to test out the functions let's start with creating a file called functions.","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=8s"},{"start_seconds":18,"text":"daml the first line in this file we'll write is module functions where there are a few rules for a module","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=18s"},{"start_seconds":27,"text":"this statement declares the module and must have the same name as the file name it has to be the first statement and all","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=27s"},{"start_seconds":34,"text":"module names must start with a capital letter to declare a function we first provide its signature a function","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=34s"},{"start_seconds":41,"text":"signature has the function's name and must start with a lowercase letter in this example the name of the function is increment then there are data types","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=41s"},{"start_seconds":51,"text":"separated by Arrow notations the data types listed first in the signature are the data types of the input arguments you can have multiple","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=51s"},{"start_seconds":59,"text":"input arguments the last data type of a signature is always the expected result daml has strong type checking so you'll","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=59s"},{"start_seconds":68,"text":"get an error if the data type of the actual results doesn't match the expected data type in the signature in this example the signature of the","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=68s"},{"start_seconds":75,"text":"increment function means that it takes one argument of type int and produces a result of type int for example we say that this","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=75s"},{"start_seconds":84,"text":"function increment Maps an INT value to another int value so the right arrow can be read as Maps 2 the next line contains","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=84s"},{"start_seconds":93,"text":"the actual definition or implementation of the function so if we call the increment function with a value n it'll be mapped to n + one notice that unlike","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=93s"},{"start_seconds":103,"text":"imperative languages where we often pass arguments inside of parentheses the arguments and daml functions are listed next to the function name and separated","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=103s"},{"start_seconds":111,"text":"by a space let's look at how we'll test this function in daml testing is an integral","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=111s"},{"start_seconds":119,"text":"part to writing templates to write a test we start by writing a script which requires importing the dl. script","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=119s"},{"start_seconds":125,"text":"Library into this module then we declare a test script a script in daml is also a type of function that captures an action","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=125s"},{"start_seconds":134,"text":"written inside a du block a do block gives us an easy way to run multiple Expressions we'll only use one in this example but it's a good practice to","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=134s"},{"start_seconds":142,"text":"follow for test scripts here in the Declaration we first give the name of the script test functions followed by a colon notice","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=142s"},{"start_seconds":152,"text":"that there are no data types mentioned here which means there are no input arguments the empty parenthesis indicate that the script returns","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=152s"},{"start_seconds":160,"text":"nothing then we Define the function by writing its name followed by equal sign and then script followed by a do block","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=160s"},{"start_seconds":169,"text":"and inside this do block we have just one statement which is a call to the dbug function debug is a function that prints","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=169s"},{"start_seconds":177,"text":"a value to the script results we use use this function for testing and debugging","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=177s"},{"start_seconds":184,"text":"purposes the dollar sign following debug is a short hand notation or syntactic sugar you can think of it as parentheses","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=184s"},{"start_seconds":191,"text":"that enclose what needs to be printed we want to print the result of the increment function when we pass three to it so the dollar sign first gets the","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=191s"},{"start_seconds":200,"text":"result of the increment function and then passes it to the debug function which get printed to the script results as","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=200s"},{"start_seconds":207,"text":"four so let's try this all in daml studio Studio we'll start by creating a new project with the daml SDK installed open","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=207s"},{"start_seconds":215,"text":"a terminal where you'd like to start the project and enter daml new functions then change","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=215s"},{"start_seconds":222,"text":"directory and open the files in the folder in Visual Studio code with daml Studio the daml SDK provides some","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=222s"},{"start_seconds":231,"text":"boilerplate code and if we look in the daml folder and open the main. daml file you'll notice a few things here that we've already discussed there are","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=231s"},{"start_seconds":239,"text":"functions and do blocks here with script tags as well for now we'll delete all this except the module main where line","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=239s"},{"start_seconds":247,"text":"up the top now we can write our increment function just like before we'll start with the function","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=247s"},{"start_seconds":255,"text":"signature notice that we get some squiggly lines here from the daml SDK linter saying this signature lacks an accompany binding well let's do that","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=255s"},{"start_seconds":264,"text":"with the function declaration that's better now we we write the script to test it ideally we would write our test","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=264s"},{"start_seconds":272,"text":"code in a separate module but to keep it simple we'll write it in the same file for now again we get the signature lack and","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=272s"},{"start_seconds":280,"text":"accompany binding error but also an error saying script is not in scope remember we need to import dam. script","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=280s"},{"start_seconds":288,"text":"and we need to write our script function declaration so here's the test functions script declaration and then the","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=288s"},{"start_seconds":295,"text":"definition as test functions equals script do to write the next statement inside this block we need to indent Expressions that","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=295s"},{"start_seconds":304,"text":"are in the DU block daml is a whit space sensitive language so the scope of whatever we write is based on how deep","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=304s"},{"start_seconds":310,"text":"it is indented inside a block so we'll indent and write the debug statement as debug dollar sign","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=310s"},{"start_seconds":318,"text":"increment 3 once everything compiles in the background you'll see a clickable statement saying script","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=318s"},{"start_seconds":326,"text":"results click on it and the script will run and you'll see the result in another tab so this is how we write and test functions in","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=326s"},{"start_seconds":333,"text":"daml remember how we mentioned the dollar sign is replacing the use of parenthesis Let's test that by writing another expression with debug that will","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=333s"},{"start_seconds":341,"text":"take the function of increment 4 in parenthesis and just to practice some more let's write one more function this","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=341s"},{"start_seconds":349,"text":"time we'll add two numbers of type int we'll start with the function signature add followed by a colon this","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=349s"},{"start_seconds":357,"text":"function will take two integers as arguments and map them to a value of type int so we need to write int Arrow","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=357s"},{"start_seconds":366,"text":"int Arrow int now the function's definition here we say add XY = x + y to","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=366s"},{"start_seconds":376,"text":"test this we simply add another debug statement debug dollar sign add","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=376s"},{"start_seconds":384,"text":"33 our script updates the trace section and we see the results one last thing before we move on","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=384s"},{"start_seconds":391,"text":"the function signature you provided is to some extent redundant because daml can infer the type of arguments and return value for the function definition","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=391s"},{"start_seconds":399,"text":"you provided so if you remove the signature for increment you'll see that the compiler automatically fills in the inferred","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=399s"},{"start_seconds":407,"text":"type we can do the same for the signature for ADD but it comes up slightly more cryptic in this example the ad function","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=407s"},{"start_seconds":416,"text":"takes a where a is a type of additive that is something which can be added additive is a class in daml and","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=416s"},{"start_seconds":424,"text":"you'll learn more about classes such as additive and multiplicative in the future for now try writing some functions to multiply two numbers or","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=424s"},{"start_seconds":433,"text":"maybe compute a complex expression let's review what we","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=433s"},{"start_seconds":440,"text":"covered functions are declared inside a module a function requires a signature and a","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=440s"},{"start_seconds":447,"text":"definition the signature specifies the the function name and the data types of its arguments and return values are separated by right","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=447s"},{"start_seconds":455,"text":"arrows the function definition specifies the actual implementation to test a function we can write a script that calls the function","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=455s"},{"start_seconds":463,"text":"and passes the required number of parameters and we print the return value by calling the debug function and passing the function to it with the","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=463s"},{"start_seconds":471,"text":"dollar sign notation or parenthesis","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=471s"},{"start_seconds":498,"text":"for","url":"https://www.youtube.com/watch?v=po91OMr3OdQ&t=498s"}],"youtube_url":"https://youtube.com/watch?v=po91OMr3OdQ","url":"https://ccpedia.xyz/videos/po91OMr3OdQ"}