Learn Solidity: Function Calls & Return Types in Solidity

In this post, we will understand the how function calls work & how to specify their return types in Solidity Language.

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.

[pastacode lang=”javascript” manual=”pragma%20solidity%200.4.8%3B%20%0A%0A%2F*%0A*%20%40title%20Learn%20Solidity%3A%20Function%20Calls%20%26%20Return%20Types%20in%20Solidity%0A*%20%40author%20Toshendra%20Sharma%0A*%20%40notice%20Example%20for%20the%20Learn%20Solidity%20Series%20on%20Toshblocks%0A*%2F%0A%0A%2F%2F%20Let’s%20learn%20how%20to%20make%20function%20calls%20in%20Solidity%0A%0A%0A%0Acontract%20FunctionCall%20%7B%0A%0A%09%2F%2F%20Constructor%20calls%20are%20also%20a%20function%20calls%20and%20are%20defined%20like%20this%0A%09%0A%09function%20FunctionCall(uint%20param1)%20%7B%0A%09%20%20%20%2F%2F%20Initialize%20state%20variables%20here%0A%09%7D%0A%0A%09%2F%2F%20you%20can%20create%20a%20contract%20object%20with%20a%20name%20%26%20then%20use%20it%20inside%20the%20function%20calls%20like%20this%0A%09Miner%20m%3B%0A%0A%0A%09function%20setMiner(address%20addr)%20%7B%20%0A%09%09m%20%3D%20Miner(addr)%3B%20%2F%2F%20type%20casted%20the%20addr%20to%20Miner%20type%20and%20stored%20in%20m%0A%09%7D%0A%09%2F%2Ffunction%20setMiner(Miner%20_m)%20%7B%20m%20%3D%20_m%3B%20%7D%20is%20also%20correct%0A%0A%09%2F%2F%20Now%20you%20can%20use%20the%20Miner’s%20function%20which%20is%20info%20to%20sent%20%0A%09%2F%2F%20some%20ether%20with%20optionally%20specifying%20the%20gas%20like%20this%0A%09function%20callMinerInfo()%20%7B%20m.info.value(10).gas(800)()%3B%20%7D%0A%0A%0A%09%2F%2Ffunction%20can%20also%20be%20called%20as%20json%20object%20as%20parameters%0A%09%2F%2F%20below%20function%20can%20be%20called%20by%20using%20the%20json%20object%20as%20shown%20in%20demo%20function%20below%0A%09function%20someFunction(uint%20key%2C%20uint%20value)%20%7B%0A%09%09%2F%2F%20Do%20something%0A%09%7D%0A%0A%09function%20demoFunction()%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20named%20arguments%0A%20%20%20%20%20%20%20%20someFunction(%7Bvalue%3A%202%2C%20key%3A%203%7D)%3B%0A%20%20%20%20%7D%0A%0A%0A%20%20%20%20%2F%2Falso%20note%20that%20variable%20names%20are%20optional%20in%20parameters%20%26%20in%20returns%0A%20%20%20%20function%20someFunction2(uint%20key%2C%20uint)%20returns%20(uint)%7B%20%0A%20%20%20%20%09%2F%2F%20Do%20something%0A%20%20%20%20%09reutrn%20key%3B%0A%20%20%20%20%7D%0A%0A%09%0A%7D%0A%0Acontract%20Miner%7B%0A%0A%09%0A%09%2F%2FThe%20modifier%20payable%20has%20to%20be%20used%20for%20info%2C%20%0A%09%2F%2F%20because%20otherwise%2C%20we%20would%20not%20be%20able%20to%20%0A%09%2F%2F%20send%20Ether%20to%20it%20in%20the%20call%20m.info.value(10).gas(800)().%0A%09function%20info()%20payable%20returns%20(uint%20ret)%20%7B%20return%2042%3B%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

AD-Blockchain-Council-01
Previous: Input and Output Parameters in Solidity Next: Function Modifiers in Solidity