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
rushi ettam 7rushi ettam 7 

Purpose of return statement

I mentioned my return type as Contact in my method
until you specify that Contact record with DML statement it won't save to database
and if you won't specify systm.debug , we can't see the output in debug log
But if you are not using the DML statment and system.debug
How &&Where can you use the returned values from any method in apex class???????
Jithin Krishnan 2Jithin Krishnan 2
Return statements are used when you want to return a value from a method to the calling method. For example consider the below method which returns a String value:
public String Method1(){
   String a='Sample ';
   return a;
And the below method which calls the Method1() method:
Public void Method2(){
   String b;
   b=Method1();//calling the method
You will get the value 'Sample Code' in the variable 'b' in Method2 from Method1. In some cases, when you don't want to return any value,you can also use just return; without specifying any variable.
But, I didn't understand your confusion abour DML statements and system.Debug and how it relates to return statement. Debug satetments are used for debugging. When you want to see the value of a varible at runtime, you can use debug statements. And DML is used for performing DML operations like insert, delete, update and upsert.
vamshi krishna 255vamshi krishna 255
Thank you Jithin,
But why cant we print System.debug(Method1)/System.debug(Method1()),Since method1 returning string of 'Sample code'