1/37
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Parameter
A variable in the method declaration
Argument
The actual value supplied to a method when it is called. Gets copied or passed into the method's parameter.
public void DisplayElement(string symbol)
Type an example of declaring a method with one parameter
DisplayElement(“O“);
Type an example of calling a method with an argument
Single Parameter
A method that accepts one piece of information
Multiple Parameters
A method that accepts two or more pieces of information separated by commas.
public void CreateElement(int atomicNumber, string symbol)
Type an example of a method with multiple parameters
Empty Parameter List
Indicates that a method does not require any input values
Return Value
The information a method sends back after it finishes executing
return
Type the keyword that sends a value back to the caller and immediately ends the method.
Use it when the method returns one primary value.
public int GetAtomicNumber()
Type an example of a method that returns an integer
return AtomicNumber;
Type an example of returning an object's property
public string GetClassification();
Type an example of a method that returns a string
public bool IsMetal()
Type an example of a method that returns a Boolean value
public Element Search(string query)
Type an example of a method that returns an object
public List<Element> GetElements()
Type an example of a method that returns a collection
void
Use the following keyword, when a method performs an action but does not need to send any value back.
Matching Return Type
The value returned by a method must match its declared return type.
Missing Return Statement
A compile-time error that occurs when a non-void method finishes without returning a value.
Passing by Value
The default behavior in C#. A copy of the argument is passed into the method. Changes made inside the method do not affect the original variable.
Passing by Reference
The method receives direct access to the original variable instead of a copy. Changes inside the method affect the original variable
ref
Type the keyword used to pass a variable by reference. Both the caller and the method must use the keyword
out
Type the keyword used when a method needs to return multiple values through its parameters. The method must assign a value before it finishes
params
Type the keyword that allows a method to accept a variable number of arguments of the same type.
Optional Parameter
A parameter that has a default value. The caller may choose to omit it
public void Print(string message = "Hello")
Type an example of an optional parameter
Named Argument
Allows arguments to be supplied by parameter name rather than position
CreateElement(symbol: "O", atomicNumber: 8);
Example of a Named Argument
Positional Argument
Arguments supplied in the same order as the method's parameter list.
Input Parameter
A parameter whose purpose is to provide information to a method
Output Parameter
A parameter whose purpose is to send information back to the caller using “out” keyword
out
Use it when the method that needs to return multiple values
Method Input
The data received through parameters before the method begins its work
Method Output
The value produced after the method completes its work. Usually returned using the ‘return’ keyword.
Parameters make methods reusable. Instead of writing separate methods for Oxygen, Carbon, and Hydrogen, one method can work for all elements by receiving different arguments.
Why do methods use parameters?
Use parameters whenever the method needs information that may change between different method calls
When should a method use parameters?
Constructor Parameters
Also use parameters to initialize newly created objects with their starting values. These parameters are discussed in detail in the Constructors deck
,
Separates multiple parameters inside a method declaration or multiple arguments inside a method call.