Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
The lesson as a book you can also play. The other ways are under More.
Sign up free to listen. The script is free to read.
Formal parameters are the variables that are declared in the function definition. They act as placeholders that receive values when the function is called. For instance, if we define a function to add two numbers, we can say add(int a, int b). Here, a and b are the formal parameters that are specified in the function declaration. They help the function know what data it will work with.
Imagine you are baking cookies. The recipe calls for ingredients (like flour and sugar) but does not specify quantities. When you decide how many cookies to make, you fill in those ingredient amounts. In this scenario, the recipe is like the function definition, and the ingredient quantities represent the formal parameters.
Actual parameters are the real values that are passed to the function when it is called. Using our previous example, if you call the function add(5, 3), the actual parameters are 5 and 3. These values are inserted into the formal parameters a and b during the execution of the function, allowing the function to perform its task using these specific inputs.
Continuing with the baking analogy, think of the actual parameters as the specific amounts of ingredients you choose when you bake cookies. You might take 2 cups of flour and 1 cup of sugar. These numbers are your actual parameters, which you use to make the cookies based on the recipe.
This section distinguishes between formal parameters defined in a function declaration and actual parameters passed during function calls.
In this section, we explore the difference between formal parameters, which are placeholders in a function definition, and actual parameters that are the real values supplied to a function when it is invoked. Understanding this distinction is essential for effectively using functions in programming.
In programming, particularly within functions, we encounter two types of parameters: formal and actual parameters. Formal parameters are declared in the function signature; they act as variables that accept values when the function is invoked. On the other hand, actual parameters are the real values that are passed to these formal parameters during the function call.
void greet(String name), name is a formal parameter used to receive the input when greet is called.