Code

Procedures and functions in 1C: differences, operation and features

Procedures and Functions in 1C: Differences, Operation, and Features

Python Basics: A Free Course for All Levels Hands-on projects and tutor interaction Find out what skills you can develop in our course

Learn more

In the 1C: Enterprise system, almost every configuration is built on the basis of procedures and functions. These elements make the code clear, correct, and facilitate further work with it. Although they have a similar structure, it can be difficult to know when to use a procedure versus a function. In this article, we will take a closer look at their purpose, differences, and basic usage guidelines.

Contents

  • Procedures and functions are important programming elements that perform specific tasks within a code.

    A procedure is a block of code that performs a specific task but does not return a value. It is used to perform operations, such as displaying information, changing the state of variables, or performing a sequence of actions. Procedures can take parameters, allowing data to be passed into them and used to perform work.

    A function, on the other hand, is also a block of code, but with a key difference - it always returns a value. Functions are used to calculate data and provide results that can be used in other parts of a program. Like procedures, functions can take arguments, which allows data to be passed in for processing.

    Thus, the main difference between these two constructs is that functions return a result, while procedures simply perform actions. Both tools help organize your code, making it more structured and understandable.

  • Procedures and functions have various parameters that determine their behavior and interaction with the external environment. The main types of parameters are:

    1. **Input Parameters** — these are values ​​that are passed to a procedure or function for processing. They can be required or optional, depending on whether they need to be specified when calling.

    2. **Output Parameters** — are used to return values ​​from a procedure or function. They allow the results of calculations to be passed back to the calling code.

    3. **Parameters by Reference and by Value** — parameters can be passed either by value, which means a copy of the data is created, or by reference, which allows the original data to be modified directly.

    4. **Variable Number of Parameters** — Some functions can take a variable number of arguments, which provides greater flexibility in their use.

    These parameters play a key role in determining how functions and procedures interact with data and how they can be used in different contexts.

  • Working with parameters is an important aspect that requires attention and understanding. First, you need to determine what parameters will be used in your task. These can be numeric values, text strings, or other data types, depending on the context.

    Next, you should determine how the parameters will be passed to your program or function. There are several methods for passing them: command-line arguments, configuration files, or environment variables. Each of these methods has its advantages and disadvantages, and the choice depends on the specifics of the task.

    Once the parameters and the method for passing them have been determined, it is necessary to verify their correctness. Validating data at the receiving stage will help avoid errors in the future. For example, if a numeric value is expected, it's worth checking that the input actually matches that type.

    It's also important to consider how parameters will be used during program execution. They can affect execution logic or be used to customize application behavior. Keep in mind that changes to parameters can produce different results, and this is worth testing.

    Finally, don't forget to document the use of parameters. This will help other developers (or yourself in the future) quickly understand how your program works and what settings can be changed.

  • Using procedures and functions in 1C:Enterprise configurations is an important part of development, allowing you to structure your code and make it easier to maintain. Procedures are sets of instructions that are executed to achieve a specific task, while functions, in addition to performing actions, return a value.

    To begin using procedures and functions, you first need to define them in a module. Modules can be either shared or local, depending on where they will be used. Once you define a procedure or function, you can call it anywhere in your code, which promotes reuse and reduces duplication.

    Procedures are typically used to perform actions that do not require returning values, such as event handling or performing certain operations. Functions, on the other hand, are most often used to perform calculations and return a result that can be used in other parts of the program.

    It is important to remember the parameters that can be passed to procedures and functions. This allows them to be more flexible and versatile, as you can use the same procedures and functions with different input data.

    Once a procedure or function is defined and called, you can easily change its logic without affecting the rest of the code, which greatly simplifies the process of maintaining and modifying the system. Thus, the correct use of these tools in 1C: Enterprise allows you to create more efficient and understandable solutions.

Definition and differences between procedures and functions

A procedure in 1C is a piece of code that performs certain actions but does not produce the opposite result. These constructs are used when an action needs to be performed: for example, saving information in a directory, changing an object, or displaying a notification to the user. They function as a command calling for a specific task to be performed.

In 1C, a function is a fragment of code that not only performs certain operations but also returns the final result. Typically, such functions are used to perform calculations and obtain values, such as an amount or date.

For example, a procedure can create a sales document and register it in a database, while a function can calculate the total amount for this document and provide the result.

In the 1C language, there are differences in the way procedures and functions are written, but their general structure has much in common.

Using procedures and functions allows you to divide a program into smaller components, which improves the readability and ease of maintenance of the code. Common algorithms can be isolated into separate procedures and functions, which helps to avoid redundancy. Instead of repeatedly writing the same actions in different parts of the program, it is enough to simply call the appropriate procedure or function. If changes to the algorithm need to be made, they can be done in just one place.

Using procedures and functions improves code readability. When the names of these elements clearly reflect their purpose, it becomes easier to understand what exactly is happening in the program. This, in turn, facilitates its further maintenance.

Read also:

Guide to working with value tables in "1C" for novice users.

A procedure declaration begins with the Procedure keyword, followed by its name and parentheses containing the parameters. The required actions are specified within the procedure body, and the end of the procedure is indicated by the EndProcedure keyword.

The procedure call process is performed as follows:

This procedure executes the code it contains, but does not produce any results.

For example:

A function is defined in a similar way, but using the Function keyword. The required calculations are performed within the function, and the Return operator is used to return the result. If this operator is not provided, the function will return the Undefined value. Functions can be used both in expressions and in assignment operations.

A function can be called as follows:

For example:

In the 1C: Enterprise 8 module, the order of procedures and functions is not important. You can call any procedure or function in the current module from another procedure or function, regardless of their location in the code, even if the called procedure is located lower in the text.

In the declaration of a function or procedure, located after the parentheses, you can include the word "Export" if you want to make it accessible from other modules. Within this procedure or function, you are allowed to call other procedures and functions, both from the current module and from third-party modules.

A semicolon is not required at the end of a procedure or function declaration. However, it can be used after the last procedure or function in a module, and this will not cause an error. However, if you subsequently decide to add new procedures or functions to the module, this may generate an error message.

Aspects of defining parameters in procedures and functions

When calling a function or procedure, it is possible to pass parameters. These parameters allow you to use a variety of input data: a function will generate a result based on the provided values, while a procedure will determine the appropriate algorithm for performing actions.

You can specify a default value in the description, which will be used if no other specification is made. To do this, specify the value to the right of the parameter name using an equals sign. If this value is not specified when the parameter is called, the system will automatically apply the default value.

Parameters with default values ​​are considered optional. Parameters without default values ​​are considered mandatory. They must be specified during the function call, otherwise an error will occur or Undefined will be passed as a value, which often leads to code failures.

Read also:

Programming in "1C: Enterprise": a guide for beginners.

By default, all arguments are passed by reference, which means that in a function or procedure The variable itself is passed, not a duplicate of it. If changes are made to a parameter within a procedure or function, this will also be reflected in the original variable, since we are dealing with the same area of ​​memory.

Example with a procedure:

In this case, the variable MyNumber has undergone changes, since the argument was passed by reference.

Example with a function:

In this function, not only does the NewResult value return, but the MyNumber variable is also directly modified, since the parameter was passed by reference.

To pass a parameter by value, you must explicitly use the Value keyword before the parameter name when declaring the function or procedure. This will ensure that a copy of the value is passed to the procedure, leaving the original variable unchanged.

Example with a procedure:

Thus, the value of the MyNumber variable did not change, since only its copy was modified during the procedure's execution.

Example with a function:

Some handlers have a special variable called Failure. Initially, its value is set to False. If during the execution of the handler it is assigned the value True, this will lead to the termination of the procedure.

Example:

In this case, the document is not processed, since the Refusal parameter is set to True.

Read also:

The 1C language includes a type system, libraries, development environment and aspects of code aesthetics, and also reflects the level of professionalism of the developers.

Methods for Effective Parameter Management

Parameter names should clearly indicate their purpose and be related to the relevant area. This approach promotes better understanding of the code without the need for additional explanations.

Parameters should be placed in a reasonable order: the main (or general) ones should be listed first, followed by the clarifying ones.

Incorrect:

Correct:

Optional parameters should be placed after those that are required.

Example:

Avoid adding too many parameters to a function. It is considered optimal to use no more than seven arguments, with only three of them allowed to have default values. Otherwise, function calls become complex and difficult to understand.

An optimal solution to extensive lists of parameters would be to combine them into structures or to reconsider the functioning of the function.

Unacceptable pattern: excessive number of characteristics.

In this case, there are nine parameters, four of which are optional. This can lead to confusion when using them.

An example that can be given: parameters are combined into a single structure.

In this case, the code becomes more understandable: it is easy to trace which parameter relates to what, and the order of the properties does not matter. When adding new parameters, changes to the calling code will be minimal.

Efficient use of procedures and functions in 1C: Enterprise configurations

Procedures and functions are the key elements on which the logic of 1C applications is based. They help organize the code, minimize its repetition, and control business processes.

Procedures are used when information processing is required, such as document processing or calculation results. They are also used to modify the interface, including hiding or showing form elements and displaying user messages. Furthermore, procedures help control the sequence of actions within business logic.

For example:

Functions are used when calculations need to be performed and a specific result must be obtained, whether it be a sum, quantity, date, or string. In addition, they allow you to isolate certain logical operations into separate modules, which ensures the possibility of their repeated use.

In sections related to objects (such as Document, Directory, Register):

  • document preparation;
  • information verification during data entry;
  • Definition of regulatory parameters.

In form modules:

  • response to user actions in the interface (for example, clicking a button or changing parameters);
  • calling server functions to interact with the database;
  • Filling table cells and controls.

In common modules:

  • Saving universal functions that can be used by different objects, for example, functions for rounding amounts or processing dates.

In order to see the list of functions and procedures available in the current module, just click on the icon with a magnifying glass, marked with the inscription Proc, or use the key combination Ctrl + Alt + P. After this, a navigation window will appear, where you can select the procedure or function you are interested in and go to its source code.

Screenshot: "1C: Enterprise" / Skillbox Media

Elements are displayed in that The order in which they are placed in the module. To sort the list alphabetically, simply activate the Sort option.

In addition to functions and procedures, each module has a list of standard handlers, such as OnOpen(), OnClose(), and OnWriteOnServer(). The set of these handlers may vary depending on the element type. If you double-click on the desired handler, a template for the corresponding procedure designed to handle the event will be automatically created in the module.

For more exciting information about code, join our Telegram channel. We look forward to seeing you among our subscribers!

Also read:

  • Based on the 1C: Enterprise system, this guide is intended for those who are just beginning to get acquainted with programming in this environment. We will cover the key points necessary for a successful start in application development.

    First of all, it is worth noting that 1C: Enterprise is not just a platform, but an entire ecosystem that allows you to create and manage various business applications. To begin, you will need to install the appropriate software on your computer. It is important to familiarize yourself with the interface and basic functions, as this will facilitate further work.

    After installation, we recommend studying the built-in reference books and documentation, which will help you better understand the structure and capabilities of the platform. It is important to pay attention to the 1C programming language, called 1C:Language. It has its own features and syntax that you need to master for writing scripts and processing data.

    Start with simple tasks: create new objects, work with forms, define and use event handlers. In addition to practical exercises, it is worth considering training materials and courses that will help deepen your knowledge.

    Don't forget about the 1C developer community, where you can ask questions and get advice from more experienced colleagues. Developing applications in 1C: Enterprise may seem difficult at first, but with practice and persistence, you can master all the basic aspects and create effective business solutions.

  • “Ordinary programmers don’t write code in Russian”: four annoying myths about 1C.
  • “You don’t need any special talents to work with 1C, but patience will be very useful”: the story of Andrey Lisyura.