Function to reduce code duplication?

Function to reduce code duplication?

Hi,

In my calendar view, I have the event title set as the 'Calendar Name' field.

I run the following code when a record is added, but also when it is updated. This creates a useful event title for me in the calendar view.

  1. brand_lookup  =  Brands  [ID == input.Brand];
  2. type_lookup  =  Types  [ID == input.Type];

  3. if (type_lookup.Type  ==  "Special Event")
  4. {
  5.     input.Calendar_Name = input.Description + " - Special Event";
  6. }
  7. else
  8. {
  9.     input.Calendar_Name = brand_lookup.Brand + " - " + type_lookup.Type;
  10. }

Is it possible to set this code as a function, so I don't have to repeat the code twice. I have tried, but it says that the variables are undefined.