# Generate Functions

## Summary

Generative AI can be used to automatically generate JavaScript functions using prompts and code templates, allowing developers to quickly and easily create new functions and features for their software.

## Example: Generating a Function to Calculate the Total Cost of a Bicycle Rental

Suppose you are developing a bicycle rental application and need to create a function to calculate the total cost of a rental based on the rental duration and the rental price per hour. You could use a generative AI tool like Compose.ai, which allows you to create new functions using prompts and code templates.

### Prompt

{% code title="prompt" overflow="wrap" %}

```markdown
generate a JavaScript function to calculate the total cost of a bicycle rental based on the rental duration and rental price per hour, using prompts and the Bicycle App configuration?
```

{% endcode %}

### Response

{% code overflow="wrap" %}

```javascript
// Generated function to calculate the total cost of a bicycle rental
function calculateTotalCost(duration, rentalPrice) {
  const rentalDurationInHours = duration / 60; // Convert duration from minutes to hours
  const totalCost = rentalDurationInHours * rentalPrice; // Calculate total cost based on rental duration and price per hour
  return totalCost;
}
```

{% endcode %}

You can use this generated function in your bicycle rental application to calculate the total cost of a rental based on the rental duration and rental price per hour.

### Discussion

The use of prompts and code templates to generate JavaScript functions can greatly improve the efficiency and speed of the software development process. By automating the creation of new functions and features, developers can save time and effort, and focus on other aspects of their software.

One potential benefit of using generative AI to generate JavaScript functions is that it can help ensure code quality and consistency. By using a standard set of prompts and templates, developers can create functions that follow best practices and are consistent with the rest of their codebase.

However, you should always consider optimizations in regards AI generated code.&#x20;

Overall, the use of generative AI in software development can be a valuable tool for generating JavaScript functions and other code. However, developers should carefully evaluate the generated code and make any necessary adjustments to ensure that it meets their specific needs and requirements.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gdf.ai/gdf-full-stack-engineering/subject-knowledge-areas/generation/generate-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
