Sir,
I am not understanding the below code of method. pls explain.
static void PrintNumber(int numberParam)
{
// Modifying the primitive-type parameter
numberParam = 5;
Console.WriteLine("in PrintNumber() method, after " +
"modification, numberParam is: {0}", numberParam);
}
Invocation of the method from Main():
static void Main()
{
int numberArg = 3;
// Copying the value 3 of the argument numberArg to the
// parameter numberParam
PrintNumber(numberArg);
Console.WriteLine("in the Main() method numberArg is: " +
numberArg);
}
The result from the above line is printed below:
in PrintNumber() method, after modification, numberParam is: 5
in the Main() method numberArg is: 3
1. Pls explain what is method declaration, invocation, argument in simple words.
Dharmraj ThakurPosted Jan 29, 2018, 12:53 AM
harish reddyPosted Jan 29, 2018, 12:42 AM
Dharmraj ThakurPosted Jan 29, 2018, 12:32 AM
Just declaration method with its signature without having boday is called method declaration. In your case method declaration is not avaialble
Eg:
void PrintNumber(int numberParam); //this will make sense what is the prototype of any method like what is expecting as argument and what will it return?
2. Method Implementation:
When method having body. Means actual work of method written inside curly brackes....
static void PrintNumber(int numberParam)
{
// Modifying the primitive-type parameter
numberParam = 5;
Console.WriteLine("in PrintNumber() method, after " +
"modification, numberParam is: {0}", numberParam);
}
3. Method invocation:
Just call the readymade method and it will perform as written logic is called method invocation.
PrintNumber(1); // here you have to pass actual values in argument and catch whatever it return (void if no return value)
4. Argument:
Argument is parameters of any method which can have zero, one or more. You can pass any values to the method.
PrintNumber(1); //caller will call with passing actual values
static void PrintNumber(int numberParam) // values will receive here... arguments valriable will hold the values passed from caller.
{}