Skip to content

Set default parameters to functions in Solidity

Set default parameters to functions in Solidity

Functions in Solidity are the workhorses of your smart contracts, defining what your contract can do. But what if you want to make certain function parameters optional, with default values if they’re not provided? Solidity doesn’t have built-in support for optional parameters like some other programming languages. However, there are a couple of clever tricks we can use to achieve a similar effect.

Your Function as a Restaurant Order

Think of your function as a restaurant order. There are essential items you always need (required parameters), like the main course. But there might be optional extras (default parameters) you can add, like a side dish or a drink. The goal is to provide flexibility for the customer (user of your function) while ensuring a smooth ordering process (function execution).

Method 1: Function Overloading

This technique involves creating multiple versions of the same function with different numbers of parameters. Here’s how it works:

Define Functions with Different Parameter Lists

Create separate functions with the same name but varying numbers of parameters. The first function will have all the required parameters, while subsequent functions will have the same required parameters followed by optional ones with default values.

function buyProduct(uint256 amount) public payable {
  // Function logic to process the purchase with default settings
}

function buyProduct(uint256 amount, string memory discountCode) public payable {
  // Function logic to process the purchase with a discount code (optional parameter)
}

In this example:

  • buyProduct(uint256 amount) is the required parameter version. Every purchase needs an amount.
  • buyProduct(uint256 amount, string memory discountCode) adds an optional discountCode parameter with a default behavior (presumably no discount applied) if not provided.

Solidity Chooses the Right Function

When someone calls the function, Solidity automatically selects the version with the matching number of arguments provided. This allows users to either specify the optional parameter or leave it out and rely on the default value.

🔥 Check this course out: Build a Semi-Fungible ERC404 Tokens’ Marketplace

Method 2: Using Internal Functions

This approach involves creating an internal helper function with the default behavior and calling it from your main function. Here’s the breakdown:

Internal Function with Default Values

Create an internal function that encapsulates the logic with default values for optional parameters.

function setDefaultDiscount(string memory code) internal pure returns (string memory) {
  return "defaultDiscountCode"; // This is the default discount code
}

In this example, the setDefaultDiscount function is internal (only accessible within the contract) and returns a default discount code.

Main Function with Optional Parameter

Define your main function with an optional parameter for the discount code. If the parameter is not provided, call the internal function to get the default value.

function buyProduct(uint256 amount, string memory discountCode) public payable {
  string memory codeToUse = discountCode.length > 0 ? discountCode : setDefaultDiscount();
  // Function logic to process the purchase using the chosen discount code
}

Here, the buyProduct function:

  • Takes an optional discountCode parameter.
  • Uses a ternary operator to check if discountCode has a value.
  • If discountCode is empty, it calls the setDefaultDiscount function to get the default code.
  • The chosen discount code (provided or default) is then used in the purchase logic.

Choosing the Right Method

Here’s a quick guide to help you decide which method to use:

  • Function Overloading: Use this if you have multiple variations in functionality based on the presence or absence of optional parameters.
  • Internal Functions: Use this if you have a clear default behavior you want to encapsulate and reuse within the contract.

Remember: Solidity doesn’t support true optional parameters, but these techniques provide workarounds to achieve similar results. By understanding these methods, you can create flexible and user-friendly functions within your smart contracts.

Follow us on â€“

🔮Twitter – https://twitter.com/0xmetaschool

🔗LinkedIn – https://www.linkedin.com/company/0xmetaschool/