function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler LarrabeeTyler Larrabee 

Trying to understand why line is needed

I'm going through the trailhead course on Apex and I am trying to understand why 'return acct;' is a necessary line in this class.
public class AccountHandler {
	public static Account insertNewAccount(String name)
    {
        Account acct = new Account(Name= name);
        insert acct;
        return acct;
    }
}
Originally I wrote this class without searching the developer community for the answer. The only thing I was missing was 'return acct;'. I'm trying to understand why that line is needed. 
Charan Thumma 15Charan Thumma 15
Hi Tyler,

The return type of the method is Account for this. So it should return an account.

If you use void you dont have to reutn the account
 
public class AccountHandler {
	public void insertNewAccount(String name)
    {
        Account acct = new Account(Name= name);
        insert acct;
    }
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm

Please mark it as best if it is useful.