In this Solidity tutorial, we will learn how to create a Solitidy function and we’ll also discuss various Solidity function examples. Additionally, we are going to cover the following list of topics.
- Solidity Function
- Solidity Function Declaration
- Solidity Function Types
- Solidity Function Modifiers
- Solidity Function Syntax
- Solidity Function Example
- Solidity Function Visibility
- Solidity Function Parameters
- Solidity Function Arguments
- Solidity Function Return Array
- Solidity Function Overloading
- Solidity Function Override
Solidity Function
Solidity, like any other modern programming language, has all of the functionality to construct modular code. The modular code includes breaking large programs into smaller, more manageable chunks. And creating functions plays a huge role in this modular approach.
Similar to any other programming language Solidity also supports the use of functions. A function in Solidity is a collection of reusable code that may be accessed at any point in your program. Moreover, with functions, it is no longer necessary to write the same code again and over.
Solidity Function Declaration
Now, to use functions in Solidity first we need to declare it within the smart contract. However, we typically declare functions in Solidity using the function keyword.
After the function keyword, we have to specify a unique name for that function. Additionally, a function can also consist of a list of parameters and an optional return value.
Solidity Function Syntax
Let us discuss the complete syntax of declaring a function in Solidity and the syntax is as follows.
function func_name(params_list) scope returns(return_type) {
// function body
}
In the above syntax,
- First, we need to use the function keyword to declare a function in Solidity.
- Next, we need to specify the name of the function in place of func_name. Moreover, we need to make sure that the function name is unique.
- After the name of the function, we can specify some function parameters if required. For parameters, we need to give a parameter name and type.
- Additionally, we can also specify a return value for the function if required with its scope. And similar to parameters, we need to specify the return type.
Solidity Function Example
Now that we understood how to declare a function in Solidity, let’s use the syntax of the function and create our first Solidity function. And the code for the function is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Sum {
// Calcualting the sum of 2 numbers
function getSum() public pure returns(uint){
uint num_1 = 14;
uint num_2 = 18;
uint sum = num_1 + num_2;
return sum;
}
}
- In the above example, we have created a contract named Sum. Within the contract, we have created a function named getSum without any parameters.
- While declaring the function, we specified the return type as a unit (unsigned integer).
- Within the function, we have taken 2 local variables, num_1 and num_2 as 14 and 18 respectively.
- In the last, we are calculating to sum of these 2 variables and return the final sum.

By adding num_1(14) and num_2(18), we will get 32 as an output.
Read: Solidity String – Complete tutorial
Solidity Function Parameters
In the example of the previous section, we understood how to create a function in Solidity. But, till now, we have only created a function without parameters in Solidity.
So, in this section, we will illustrate how to create a function with parameters in Solidity. Moreover, a specified parameter can be utilized with a function and there can be more than one parameter of a function.
To specify parameters with a function, we can use the following syntax.
function func_name(params_list) scope returns(return_type) {
// function body
}
To specify any parameter, we need to specify the parameter name and the type of the parameter. After this, we can utilize the parameter within that function. Let us discuss this implementation of the Solidity function with parameters using an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Sum {
// Calcualting the sum of 2 numbers
function getSum( uint num_1, uint num_2 ) public pure returns(uint){
uint sum = num_1 + num_2;
return sum;
}
}
- In the above example, first, we created a contract named Sum and within the contract, we created a function named getSum.
- Now, while creating the function, we specified parameters num_1 and num_2 respectively.
- Within the function, we are using these 2 parameters’ values to calculate the sum as a result.

In the above implementation, we are given parameter values of 11 and 12 respectively. In the last, we got 23 as the result.
Solidity Function Visibility
Solidity allows us to regulate the access of a function and how it should interact with one another. This notion is known as Visibility in Solidity.
The visibility of a function can be selected from 4 different categories. This category includes.
- External
- Public
- Internal
- Private
Let us understand the details of each one of them in detail.
Solidity Function Visibility – Public
A public function is a function that is publically available to everyone. It can be accessed from the inside as well as from the outside of the contract. Moreover, when we don’t specify any visibility then by default, the function becomes public visibility.
To set the visibility of a function as public in Solidity, we need to use the public keyword while declaring a function. Here is the complete syntax of creating a public function in Solidity.
function func_name(params_list) public returns(return_type) {
// function body
}
Solidity Function Visibility – Private
By declaring the private visibility of a function, we create a private function in Solidity. This type of function can only be accessed from the same contract where it is declared. Moreover, we cannot inherit a private function in Solidity.
To set the visibility of a function as private in Solidity, we need to use the private keyword while declaring a function. Here is the complete syntax of creating a private function in Solidity.
function func_name(params_list) private returns(return_type) {
// function body
}
Solidity Function Visibility – External
A function with external visibility is an external function in Solidity that can be accessed only from outside of the current contract. An external function can be called by other contracts but it cannot be utilized within the same contract in which it is created.
To set the visibility of a function as external in Solidity, we need to use the external keyword while declaring a function. Here is the complete syntax of creating an external function in Solidity.
function func_name(params_list) external returns(return_type) {
// function body
}
Solidity Function Visibility – Internal
An internal function in Solidity can be called and accessed within the same contract in which it is created. It is quite similar to a private visibility function where we cannot call an internal function from other contracts.
However, a private function cannot be accessed from the derived contracts but an internal function can be called and accessed from the derived contracts in Solidity.
To set the visibility of a function as internal in Solidity, we need to use the internal keyword while declaring a function. Here is the complete syntax of creating an internal function in Solidity.
function func_name(params_list) internal returns(return_type) {
// function body
}
Read: Solidity Array – Detailed Guide
Solidity Function Arguments
In Solidity, to specify the values of the function parameters, we need to pass the arguments to the function. Now, if we call the function and specify the arguments without their name then we need to specify the values in the same order as they are declared.
Let us understand this concept using an example in Solidity. And the code for the example execution is as follows.
Example-1
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Product {
// Calcualting the product of 2 numbers
function getProduct( uint num_1, uint num_2 ) private pure returns(uint, uint, uint){
uint product = num_1 * num_2;
return (num_1, num_2, product);
}
function getResult( uint x, uint y ) public pure returns(uint, uint, uint){
return getProduct(x,y);
}
}
- In the above example, we created a contract named Product. Within the contract, we defined 2 functions.
- First is the getProduct which is a private function, it takes 2 unit values num_1 and num_2 as input and returns the given values and their product as result.
- Second, there is getResult which is a public function and it also takes 2 input values x and y and calls the getProduct function to get the product of given values.
- In the getResult function, we are calling the getProduct function and passing x and y as arguments.

In the above execution, we have given 7 and 9 as input. So, according to our smart contract, the value of num_1 and x is 7, and the value of num_2 and y is 9. As a result, we got 62 as result.
Example-2
However, we can specify an argument while calling a function in any order by using the curly braces. This method is also known as named call where we specify the argument name and its value in curly braces. And with this, the order of the declared parameters does not matter.
Let us understand this concept using an example in Solidity. And the code for the example execution is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Product {
// Calcualting the product of 2 numbers
function getProduct( uint num_1, uint num_2 ) private pure returns(uint, uint, uint){
uint product = num_1 * num_2;
return (num_1, num_2, product);
}
function getResult( uint x, uint y ) public pure returns(uint, uint, uint){
return getProduct({num_1: y, num_2: x});
}
}
In the above code, we just specified parameters using curly braces.

In the execution, we have specified inputs as 7 and 9. But this time, it means the value of num_1 and y is 7, and the value of num_2 and x is 9. As a result, we get 63 as the product.
Read: Solidity Constructor
Solidity Function Overloading
Function overloading is a characteristic of object-oriented programming that enables us to have multiple functions having the same name but different parameters.
So, in Solidity, we can create multiple function definitions with the same name. However, in the definition of the function, the types and numbers of arguments in the parameter list must differ. Moreover, overloading is not possible with function declarations that merely differ in return type.
Let us understand the concept of function overloading in Python using an example in Solidity. And the example is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Product {
function getProduct( uint x, uint y ) private pure returns(uint){
uint product = x * y;
return (product);
}
function getProduct( uint x, uint y, uint z ) private pure returns(uint){
uint product = x * y * z;
return (product);
}
function ProductWithTwoArgs() public pure returns(uint){
return getProduct(6,7);
}
function ProductWithThreeArgs() public pure returns(uint){
return getProduct(6,7,8);
}
}
In the above example, we created a contract named Product. With the contract, we defined 4 functions, and out of these 4 functions are with the same name i.e. getProduct. The first getProduct function calculates the product of 2 numbers and the second getProduct function calculates the product of 3 numbers.

Solidity Function Return Array
In this section, we will discuss how to create a function in Solidity that will return an array. Before starting the code part, we all should have some basic knowledge of Arrays in Solidity.
An array in computer programming is a group of data elements stored in the same memory region at the same time. We can also consider it as a basic data structure where each data element may be accessed simply by its index value.
Now, Solidity supports the use of 2 different types of Array. And we can learn about these detail from the following tutorial – Solidity Array.
Next, let us discuss how to return an array using a function in Solidity. For this task, consider the following example.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract USA_City {
string[] cities = ["Chicago", "Dallas", "Phoenix", "Houston"];
function getCities() public view returns (string[] memory) {
return cities;
}
}
In the above example, first, we created a contract named USA_City. Within the contract, we defined a dynamic-size array named cities. After this, we defined a function named getCities and this function will return the cities array as a result.
The result of the above smart contract is shown below.

Solidity Function Override
Being an object-oriented programming language, Solidity has numerous inheritance capabilities. It allows to inherit from a basic contract and then override one of its functions.
Before Solidity version 0.6, there was no way to tell which functions should be overridden. But with version 0.6 and higher, we can now declare a function with virtual or override keywords and override it whenever required.
Here is the basic syntax of using virtual and override keywords with functions in Solidity.
contract Base {
function sample() virtual public {}
}
contract Derived is Base{
function sample() public override{}
}
- First, in the Base contract, we need to use the virtual keyword with the function that we want to override.
- After this, we need to use the override keyword in the derived contract where we are overriding the function.
Let’s understand the use of the above syntax with the help of an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Base {
function getResult() virtual public returns(uint){
uint product = 5 * 6;
return (product);
}
}
contract Derived is Base{
function getResult() public pure override returns(uint){
uint product = 6 * 7;
return (product);
}
}
In the above example, first, we created a contract name Base and within the contract, we defined a function getResult. The role of getResult is to return the product of 5 and 6.
After this, we created a Derived contract using the Base contract. In this contract, we are overriding the getResult function to return the product of 6 and 7.
Here is the result of the above execution.

In the above execution, we can observe that the result of getResult for contract Base is 30 (5×6). And the result of the getResult function for contract Derived is 42 (6×7).
Solidity Function Modifiers
Function modifiers are a unique concept that aid in modifying the function’s behavior. It is quite useful as it reduces the amount of code that is duplicated. Moreover, if we are testing the same condition across multiple smart contracts, we can reuse the same modifier in several methods.
To declare a modifier in Solidity, we can use the modifier keyword. The whole syntax of creating function modifiers in Solidity is given below.
modifier modifier_name{
//modifier_body
_;
}
- In the above syntax, first, we are using the modifier keyword to declare a modifier in Solidity.
- After this, we can specify the name of the modifier in place of modifier_name. Next, there is the body of the modifier in the curly braces {}.
- In the last, there is the merge wildcard which is represented using the _; symbol. This _; blends the function code with the modifier code.
- Moreover, it is compulsory to use the _; symbol in the body of the modifier. And we can use it anywhere in the modifier definition (starting, middle, ending).
Let us understand the use of modifiers with the help of an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract FuncModifier {
modifier checkValues(uint x, uint y) {
require(x > 0, "Value of x is 0");
require(y > 0, "Value of y is 0");
_;
}
function product(uint x, uint y) public checkValues(x, y)
pure returns(uint) {
return (x*y);
}
}
In the above code, we created a contract named FuncModifier. Within the contract, first, we created a modifier name checkValues. This modifier will check if the value of x and y is greater than 0 or not. And if the values are greater than 0 then the function product will be executed.

Solidity Function Types
In Solidity, a function can be classified into multiple categories based on its visibility and state. Here is the list of all the types of functions in Solidity.
- Public Function
- Private Function
- View Function
- Pure Function
- External Function
- Internal Function
- Payable Function
Let us discuss each of its types of function in detail in Solidity.
Name | Description |
---|---|
Public | A public function can be accessed and called from anywhere. |
Private | A private function can be accessed and called from the contract in which it is created. |
External | An external function can be called or accessed by other contracts but it cannot be utilized within the same contract in which it is created. |
Internal | An internal function in Solidity can be called and accessed within the same. However, it can also be accessed using derived contracts. |
Pure | A pure function in Solidity does not read or modify the data of a contract. Moreover, this type of function does not change the contract state |
Payable | A payable function in Solidity helps to collect or receive ETHER in the smart contract |
View | A view function in Solidity only returns contract data but does not modify the state, |
So, in this Solidity tutorial, we understood how to create a Solitidy function and we also discussed various Solidity function examples. Additionally, we have covered the following list of topics.
- Solidity Function
- Solidity Function Declaration
- Solidity Function Types
- Solidity Function Modifiers
- Solidity Function Syntax
- Solidity Function Example
- Solidity Function Visibility
- Solidity Function Parameters
- Solidity Function Arguments
- Solidity Function Return Array
- Solidity Function Overloading
- Solidity Function Override
I am Bijay, a Microsoft MVP and founder of TSInfo Technologies, a SharePoint development company. Currently focusing on getting expertise on Ethereum, Solidity, Bitcoin, Cryptocurrency, Blockchain, etc. Sharing my expertise and tutorials on Bitcoin and Ethereum related technologies. Read More…