0
0
Blockchain / Solidityprogramming~30 mins

Constructor function in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Constructor Function in a Blockchain Smart Contract
📖 Scenario: You are creating a simple blockchain smart contract to store and manage a greeting message. This contract will have a constructor function that sets the initial greeting when the contract is deployed.
🎯 Goal: Build a smart contract with a constructor function that initializes a greeting message. Then, display the greeting message stored in the contract.
📋 What You'll Learn
Create a contract named GreetingContract
Add a public string variable called greeting
Write a constructor function that takes a string parameter called _greeting and sets the greeting variable
Add a function called getGreeting that returns the current greeting
Print the greeting message by calling getGreeting
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains often need constructor functions to set initial data like owner addresses or configuration settings when deployed.
💼 Career
Understanding constructor functions is essential for blockchain developers to create secure and functional smart contracts.
Progress0 / 4 steps
1
Create the contract and greeting variable
Create a contract named GreetingContract and inside it, declare a public string variable called greeting.
Blockchain / Solidity
Need a hint?

Use contract GreetingContract {} to start the contract and declare string public greeting; inside.

2
Add the constructor function
Add a constructor function inside GreetingContract that takes a string parameter called _greeting and sets the greeting variable to _greeting.
Blockchain / Solidity
Need a hint?

Use constructor(string memory _greeting) { greeting = _greeting; } inside the contract.

3
Add a function to get the greeting
Add a public view function called getGreeting that returns the string greeting.
Blockchain / Solidity
Need a hint?

Define function getGreeting() public view returns (string memory) { return greeting; }.

4
Display the greeting message
Write a comment showing how to call getGreeting to display the greeting message stored in the contract.
Blockchain / Solidity
Need a hint?

Write a comment like // Call getGreeting() to display the greeting message stored in the contract.