Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

Function declaration and syntax in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a function in blockchain programming?
A function is a reusable block of code that performs a specific task within a smart contract or blockchain program.
Click to reveal answer
beginner
How do you declare a function in Solidity?
You declare a function using the <code>function</code> keyword, followed by the function name, parentheses for parameters, and curly braces for the function body.<br>Example: <code>function greet() public { }</code>
Click to reveal answer
beginner
What does the public keyword mean in a function declaration?
It means the function can be called both inside and outside the smart contract, making it accessible to anyone interacting with the contract.
Click to reveal answer
beginner
What is the purpose of function parameters?
Parameters let you send information into a function so it can use that data to perform tasks or calculations.
Click to reveal answer
intermediate
How do you specify the return type of a function in Solidity?
You add a <code>returns</code> keyword after the parameter list with the type inside parentheses.<br>Example: <code>function getValue() public view returns (uint)</code>
Click to reveal answer
Which keyword is used to declare a function in Solidity?
Amethod
Bfunction
Cfunc
Ddef
What does the public keyword in a function declaration mean?
AFunction is private
BFunction can only be called internally
CFunction can be called externally and internally
DFunction is deprecated
How do you specify a function returns a value in Solidity?
AUsing <code>returns</code> keyword
BUsing <code>output</code> keyword
CUsing <code>returnType</code> keyword
DNo special keyword needed
Which symbol encloses the function body in Solidity?
A<>
B()
C[]
D{}
What is the purpose of function parameters?
ATo send data into a function
BTo store data permanently
CTo declare variables globally
DTo stop function execution
Explain how to declare a function in Solidity including visibility and return type.
Think about the parts you need to write a complete function.
You got /6 concepts.
    Describe the role of function parameters and how they affect function behavior in blockchain programming.
    Consider how you give information to a function.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the correct way to declare a function named transferFunds in a blockchain smart contract?
      easy
      A. transferFunds function() { }
      B. function transferFunds() { }
      C. func transferFunds() { }
      D. def transferFunds() { }

      Solution

      1. Step 1: Identify the common function declaration keyword

        In many blockchain languages like Solidity, functions start with the keyword function.
      2. Step 2: Check the syntax structure

        The correct syntax is function functionName() { }, so function transferFunds() { } is correct.
      3. Final Answer:

        function transferFunds() { } -> Option B
      4. Quick Check:

        Function declaration uses 'function' keyword [OK]
      Hint: Look for 'function' keyword before name [OK]
      Common Mistakes:
      • Using 'def' instead of 'function'
      • Placing 'function' after the name
      • Using 'func' which is not standard here
      2. Which of the following is the correct syntax to declare a function with one parameter named amount?
      easy
      A. function send(amount) { }
      B. function send amount { }
      C. function send(amount) => { }
      D. function send[amount] { }

      Solution

      1. Step 1: Recognize parameter syntax in function declaration

        Parameters are placed inside parentheses separated by commas if multiple.
      2. Step 2: Validate each option's syntax

        function send(amount) { } uses parentheses correctly: function send(amount) { }. Others misuse brackets, arrows, or omit parentheses.
      3. Final Answer:

        function send(amount) { } -> Option A
      4. Quick Check:

        Parameters go inside parentheses [OK]
      Hint: Parameters always go inside parentheses () [OK]
      Common Mistakes:
      • Omitting parentheses around parameters
      • Using square brackets instead of parentheses
      • Using arrow syntax incorrectly
      3. What will be the output of this function call in a blockchain smart contract?
      function multiply(x, y) { return x * y; }
      multiply(3, 4);
      medium
      A. 7
      B. Error
      C. 34
      D. 12

      Solution

      1. Step 1: Understand the function logic

        The function multiplies two inputs x and y and returns the product.
      2. Step 2: Calculate the result for inputs 3 and 4

        3 multiplied by 4 equals 12.
      3. Final Answer:

        12 -> Option D
      4. Quick Check:

        3 * 4 = 12 [OK]
      Hint: Multiply inputs as given to get output [OK]
      Common Mistakes:
      • Adding instead of multiplying
      • Concatenating numbers as strings
      • Assuming syntax error without checking code
      4. Identify the error in this function declaration:
      function deposit(amount) {
      balance += amount
      }
      medium
      A. Missing semicolon after statement
      B. Function name is invalid
      C. No return statement in function
      D. Missing parentheses around parameter

      Solution

      1. Step 1: Check syntax inside the function body

        Statements inside functions usually require semicolons to end lines.
      2. Step 2: Identify missing semicolon

        The line balance += amount lacks a semicolon at the end.
      3. Final Answer:

        Missing semicolon after statement -> Option A
      4. Quick Check:

        Statements need semicolons [OK]
      Hint: Check for missing semicolons inside function body [OK]
      Common Mistakes:
      • Ignoring semicolon requirements
      • Thinking return is mandatory
      • Assuming function name is wrong
      5. You want to create a function calculateFee that takes a transaction amount and returns 2% fee only if the amount is above 1000. Which is the correct function?
      hard
      A. function calculateFee(amount) { if amount > 1000 then return amount * 0.02 else return 0; }
      B. function calculateFee(amount) { return amount * 0.02 if amount > 1000 else 0; }
      C. function calculateFee(amount) { if (amount > 1000) { return amount * 0.02; } else { return 0; } }
      D. function calculateFee(amount) { if (amount > 1000) return amount * 0.02; }

      Solution

      1. Step 1: Understand conditional return logic

        The function must check if amount is greater than 1000 and return 2% fee; otherwise, return 0.
      2. Step 2: Validate syntax correctness

        function calculateFee(amount) { if (amount > 1000) { return amount * 0.02; } else { return 0; } } uses proper if-else syntax with braces and returns both cases correctly. Others have syntax errors or missing else return.
      3. Final Answer:

        function calculateFee(amount) { if (amount > 1000) { return amount * 0.02; } else { return 0; } } -> Option C
      4. Quick Check:

        Use if-else with braces for conditional returns [OK]
      Hint: Use full if-else with braces for clear returns [OK]
      Common Mistakes:
      • Missing else return causes undefined output
      • Incorrect if syntax without parentheses or braces
      • Using Python-style if-else in blockchain code
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard