What is the difference between a transaction and a call? In some interfaces, I can interact with contracts via calls or via transactions. What is the difference between the two, and can contracts also do transactions and calls?
A call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain.
It is a read-only operation and will not consume any Ether. It simulates what would happen in a transaction, but discards all the state changes when it is done.
It is synchronous and the return value of the contract function is returned immediately.
Its underlying JSON-RPC is eth_call
A transaction is broadcasted to the network, processed by miners, and if valid, is published on the blockchain.
It is a write-operation that will affect other accounts, update the state of the blockchain, and consume Ether (unless a miner accepts it with a gas price of zero).
It is asynchronous, because it is possible that no miners will include the transaction in a block (for example, the gas price for the transaction may be too low). Since it is asynchronous, the immediate return value of a transaction is always the transaction's hash. To get the "return value" of a transaction to a function, Events need to be used (unless it's Case4 discussed below).
Its web3.js API is web3.eth.sendTransaction and is used if a Solidity function is not marked
Its underlying JSON-RPC is eth_sendTransaction
sendTransaction will be used when a verb is needed, since it is clearer than simply transaction.
Recommendation to Call first, then sendTransaction
Since a sendTransaction costs Ether, it is a good practice to "test the waters" by issuing a call first, before the sendTransaction. This is a free way to debug and estimate if there will be any problems with the sendTransaction, for example if an Out of Gas exception will be encountered.
This "dry-run" usually works well, but in some cases be aware that call is an estimate, for example a contract function that returns the previous blockhash, will return different results based on when the call was performed, and when the transaction is actually mined.
Finally, note that even though a call does not consume any Ether, sometimes it may be necessary to specify the actual gas amount for the call: the default gas for call in clients such as Geth, may still be insufficient and can still lead to Out of Gas.
Case4: can contracts create transactions?
There are 4 cases for how a contract function can be invoked.
Here are the 4 cases, and the first 3 have been covered above:
direct invocation of a contract function, originated with call
direct invocation of a contract function, originated with sendTransaction
contract invocation of a contract function, originated with call
4. contract invocation of a contract function, originated with sendTransaction
Although Case4 sounds like a contract can "also do transactions", because it's within a transaction, the definition of a transaction is data signed by an External Actor. Thus Case4 is not a transaction.
From the Yellow Paper:
Transaction: A piece of data, signed by an External Actor. It represents either a Message or a new Autonomous Object. Transactions are recorded into each block of the blockchain.
Earlier above, it was stated that Events had to be used to get the return value of a contract function, originated with sendTransaction. That pertains and is true for Case2. For Case4, the contract obtains the return value of a contract function directly and can use it. Here's how the Yellow Paper says it in bold:
Message Call: The act of passing a message from one Account to another. If the destination account is associated with non-empty EVM Code, then the VM will be started with the state of said Object and the Message acted upon. If the message sender is an Autonomous Object, then the Call passes any data returned from the VM operation
With these definitions from the Yellow Paper, it can be seen that Case4 is a Message Call within a transaction (thus state changes), there is no nesting of transactions, and that a unit of activity on the Ethereum blockchain is all within a single transaction that may contain multiple Message Calls.
Update: Since calling a function can be ambiguous (is it an eth_call or an eth_sendTransaction?), there is a proposal for eth_simulateTransaction.
The difference between in a call and a transaction is the following:
transactions are created by your client, signed and broadcasted to the network. They will eventually alter the state of the blockchain, for example, by manipulating balances or values in smart contracts.
calls are transactions executed locally on the user's local machine which alone evaluates the result. These are read-only and fast. They can't change the blockchain in any way because they are never sent to the network. Some examples "read-only/dry run/practice".
Calls are useful for debugging smart contracts as they do not cost transaction fees or gas.