โ†
Previous Module
Deploy Your First Contract

๐Ÿ“ž Calling Smart Contract Functions

Learn how to interact with contracts using view, pure, and payable functions

๐Ÿ”— Smart Contract Function Calls

Functions are the building blocks of smart contracts. Understanding how they call each otherโ€”and the gas costs involvedโ€”is essential for writing efficient, secure code.

๐ŸŽฎ Interactive: Function Call Types Explorer

Explore different types of function calls and their characteristics

๐Ÿ”„
Internal Call
Function call within same contract
Gas Cost: Low (24-100 gas)
Characteristics:
1
Direct JUMP in EVM
2
Same context (storage, msg.sender)
3
No transaction overhead
4
Fastest execution
Code Example:
function a() { b(); } // b() is internal

๐Ÿค” Why Function Calls Matter

โšก
Gas Efficiency

Internal calls cost ~50 gas. External calls cost 2,600+ gas. Choose wisely!

๐Ÿ›ก๏ธ
Security

External calls can fail or be malicious. Always validate and handle errors.

๐Ÿ—๏ธ
Architecture

Call patterns determine contract composability and upgrade strategies.

๐ŸŽฏ
Optimization

Understanding calls helps you write lean, cost-effective smart contracts.

๐Ÿ“‹ What You'll Learn

1
Call Types & Mechanics
Internal, external, delegatecall, staticcallโ€”how each works under the hood
2
Gas Costs & Optimization
Measure real gas consumption and learn techniques to minimize costs
3
Security Patterns
Reentrancy guards, checks-effects-interactions, and safe call practices
4
Real-World Examples
See how DeFi protocols and token contracts use function calls efficiently

๐Ÿ”‘ Key Concepts Preview

internal
Same contract, JUMP opcode, preserves context, lowest gas
external
Different contract, CALL opcode, new context, higher gas
delegatecall
Execute in caller's context, proxy pattern, same gas as external
view / pure
Read-only, STATICCALL, cannot modify state, safe reads
payable
Can receive ETH, special handling, value transfer
reentrancy
Attack vector, checks-effects-interactions, use guards

๐Ÿ’ก Quick Example

contract Calculator {
    // Internal call - just JUMP (cheap)
    function add(uint a, uint b) internal pure returns (uint) {
        return a + b;
    }
    
    // External call - calls add() internally
    function calculate() public pure returns (uint) {
        return add(5, 3);  // Internal: ~50 gas
    }
}

contract User {
    Calculator calc = new Calculator();
    
    function useCalculator() public view returns (uint) {
        // External call - CALL opcode (expensive)
        return calc.calculate();  // External: ~2,600 gas
    }
}
Notice: Same calculation, but external call costs 50x more gas! This module teaches you when to use each type.