Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
pragma solidity 0.4.8;
/*
* @title Learn Solidity: Function Calls & Return Types in Solidity
* @author Toshendra Sharma
* @notice Example for the Learn Solidity Series on Toshblocks
*/
// Let's learn how to make function calls in Solidity
contract FunctionCall {
// Constructor calls are also a function calls and are defined like this
function FunctionCall(uint param1) {
// Initialize state variables here
}
// you can create a contract object with a name & then use it inside the function calls like this
Miner m;
function setMiner(address addr) {
m = Miner(addr); // type casted the addr to Miner type and stored in m
}
//function setMiner(Miner _m) { m = _m; } is also correct
// Now you can use the Miner's function which is info to sent
// some ether with optionally specifying the gas like this
function callMinerInfo() { m.info.value(10).gas(800)(); }
//function can also be called as json object as parameters
// below function can be called by using the json object as shown in demo function below
function someFunction(uint key, uint value) {
// Do something
}
function demoFunction() {
// named arguments
someFunction({value: 2, key: 3});
}
//also note that variable names are optional in parameters & in returns
function someFunction2(uint key, uint) returns (uint){
// Do something
reutrn key;
}
}
contract Miner{
//The modifier payable has to be used for info,
// because otherwise, we would not be able to
// send Ether to it in the call m.info.value(10).gas(800)().
function info() payable returns (uint ret) { return 42; }
}