• Lessons Home
Topics
Community
  1. Lessons
  2. Apex Syntax Fundamentals
  3. Methods and Functions
  4. Method Parameters

      Method Parameters

      Method Parameters

      Methods become much more useful when you can pass data into them. Parameters are typed inputs declared inside the parentheses of a method declaration. Each parameter has a type and a name, and the method can use the parameter just like a local variable.

      Declaring Parameters

      public static String makeLabel(String name, Integer count) {
          return name + ' (' + count + ')';
      }
      

      This method declares two parameters: a String named name and an Integer named count. Multiple parameters are separated by commas, and each one needs its own type.

      Parameters vs Arguments

      The two words are often mixed up, but they mean different things:

      • A parameter is the variable in the method declaration, such as String name
      • An argument is the actual value you pass when calling the method, such as 'Widget'

      How Values Flow In

      When you call a method, each argument is copied into the matching parameter in order:

      String label = makeLabel('Widget', 3);
      // Inside the method, name is 'Widget' and count is 3
      // label is now 'Widget (3)'
      

      The first argument 'Widget' lands in the first parameter name, and the second argument 3 lands in the second parameter count. The types must match: passing an Integer where a String is expected causes a compile error.

      Why This Matters

      Parameters let one method handle many different inputs. Instead of writing one method per customer name, you write a single method that greets any name you pass in.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In