thank you so much for your help I really appreciated this contract give me error in the line with (length) "ParserError: Expected primary expression. returns candidate.length"

pragma solidity ^0.4.21;

contract Election {

    struct Candidate {
        string name;
        uint voteCount;

    struct Voter {
    bool authorized;
    bool voted;
    uint vote;
    address public owner;
    string public electionName;

    mapping(address => Voter) public voters;
    Candidate[] public candidates;
    uint public totalVotes;

    modifier ownerOnly() {
        require(msg.sender == owner);

    function Election(string _name) public {
        owner = msg.sender;
        electionName = _name;

    function addCandidate(string _name) ownerOnly public {

    function getNumCandidate() public view returns(uint){
        returns candidates.length;

    function authorize(address _person) ownerOnly public{
        voters[_person].authorized = true;

    function vote(uint _voteIndex) public {

        voters[msg.sender].vote = _voteIndex;
        voters[msg.sender].voted = true;

        candidates[_voteIndex].voteCount +=1;
        totalVotes +=1;

    function end() ownerOnly public {

1 Answer 1


When you define a function's returns values, you have to use returns.

functions getNumCandidates() public view returns(uint)

However when you actually return the values, you have to use return.

    return candidates.length;

Hence your code should look like:

function getNumCandidate() public view returns(uint){
    return candidates.length;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.