Tyl Programming Language
▶ VARIABLE ASSIGNMENT - PART 4
Variables can be assigned a value from function operations. Say we have a messages list, and we have a function: 'get_first_message', that returns the first message from messages.

Here's a pseudo code of get_first_message:
get_first_message messages »
 first_message

≡ Here comes the code that searches for
≡ the first message in messages parameter
≡ and assigns it to first_message variable

 first_message
get_first_message has the messages parameter, which is the list of messages. The first line: first_message, declares a local variable to hold the message that will be found in messages. The last line: first_message acts as the 'result' statement, meaning that the function will return first_message.

To use get_first_message:
main »
 messages 'message 1' 'message 2' 'message 3' 'message 4' 'message 5'
first_message get_first_message messages

get_first_message messages »
 first_message

≡ Here comes the code that searches for
≡ the first message in messages parameter
≡ and assigns it to first_message variable

 first_message
In main function, first, we declare a local list variable: 'messages'. Then, first_message variable is declared and gets the result of calling get_first_message function with messages parameter. So now, first_message is: 'message 1'.
List variables can be assigned a value from function operations. Using the same messages list, and adding a function: 'get_last_messages' that fills a last_messages parameter with the last messages from messages.

Here's a pseudo code of get_last_messages:
get_last_messages messages last_messages »
≡ Here comes the code that searches for
≡ the last three messages in messages parameter
≡ and adds the messages to the last_messages parameter
get_last_messages has the messages parameter, which is the list of messages. The last_messages parameter will hold the last messages that will be found in messages.

To use get_last_messages:
main »
 messages 'message 1' 'message 2' 'message 3' 'message 4' 'message 5'
last_messages
get_last_messages messages last_messages

get_last_messages messages last_messages »
≡ Here comes the code that searches for
≡ the last three messages in messages parameter
≡ and adds the messages to the last_messages parameter
In main function, first, we declare a local list variable: 'messages'. Then, last_messages variable is declared. get_last_messages function is called with messages and last_messages parameters, and modifies last_messages.
So now, last_messages has: 'message 3', 'message 4' and 'message 5'.


PART 3