2. Function
• A function is basically a group of code that can be reused
anywhere in the program, which generally saves the
excessive use of memory and decreases the runtime of the
program.
• Creating a function reduces the need of writing the same
code over and over again.
3. Declaring a Function
• In Solidity a function is generally defined by using the function keyword, followed by the name of the
function which is unique and does not match with any of the reserved keywords.
• A function can also have a list of parameters containing the name and data type of the parameter.
• The return value of a function is optional but in solidity, the return type of the function is defined at the
time of declaration.
function function_name(parameter_list) scope returns(return_type) {
// block of code
}
SYNTAX:
4. Calling a function
• A function is called when the user wants to execute that function.
• In Solidity the function is simply invoked by writing the name of the
function where it has to be called.
• Different parameters can be passed to function while calling, multiple
parameters can be passed to a function by separating with a comma.
5. Return keyword
• A return statement is an optional statement provided by solidity. It is the
last statement of the function, used to return the values from the
functions.
• In Solidity, more than one value can be returned from a function. To return
values from the function, the data types of the return values should be
defined at function declaration.
6. pragma solidity ^0.8.0;
// Creating a contract
contract Test {
function return_example() public view returns(uint, uint,
uint){
uint num1 = 10;
uint num2 = 16;
uint sum = num1 + num2;
uint prod = num1 * num2;
uint diff = num2 - num1;
return (sum, prod, diff);
}
}
Functionfun(int a
7. Contract cn{
Uint public a;
Function fun() public pure returns(uint){
Uint public b=1;
Return b;
}
}
8. Pure, view and simple functions
DEFAULT
(NOT SPECIFIED
IN FUNCTION)
VIEW PURE
Ideal For
Transactions that
alter data on the
block chain
Getter functions to
view data on the
block chain
Defined to the
scope of the
function and do
not alter or view
data on the block
chain
Data Access read / write read None
Transaction Type Send Call Call
9. View functions
• View functions are read only functions and do not modify the state of
the block chain.
• In other words if you want to read data from the block chain one can
use view. Getter method are by default view functions
pragma solidity ^0.8.0;
contract viewSample {
//view is specified and function reads data from block
chain
function getBlock() public view returns (uint){
uint blocknumber = block.number;
return blocknumber;
}
}
10. Pure functions
• Pure functions are more restrictive then view functions and do
not modify the state AND do not read the state of the block
chain
11. pragma solidity ^0.8.0;
contract pureSample {
//pure is specified
//this functions is only concerned about the variables
and arithmetic in this function
function getResult() public pure returns(uint sum){
uint a = 5;
uint b = 7;
sum = a + b;
return sum;
}
}