Method Calls and Arguments
Methods exist for us to use. A method call is a statement in your program code that tells the computer to run a particular method, and wait for it to complete before moving to the next instruction. When you call a method, you use its name and then provide a list of values between parenthesis. These values are called arguments, they give data to the method for it to use in its actions.
Example
The following code demonstrates several method calls. Notice how each is always the name of the method, followed by 0 or more values within parenthesis. When the method returns data, we can store that in a variable or use it to pass as the value to another method.
// Give us access to methods in SplashKit libraryusing static SplashKitSDK.SplashKit;
// Give us access to convert methods from C#using static System.Convert;
// Call the WriteLine method - the argument is the text to outputWriteLine("Testing method calls");
int delaySeconds;string userInput;
// Call the Write method - the argument is the text to outputWrite("How many seconds do you want it to show for? Enter seconds: ");
// Call ReadLine - this needs no arguments.// It will return the string that the use entered.// In this case we store it in userInput.userInput = ReadLine();
// Call ToInt32 - passing it the text from userInput as the argument.// This returns a number which we then store in delaySeconds.delaySeconds = ToInt32(userInput);
// Call the open window method. This has three arguments// - the first is the title of the window. We will make it Test Window// - the second is the width - we want 1280 pixels on each line// - the last is the height - we want 720 lines in the windowOpenWindow("Test Window", 1280, 720);
// Call RandomColor - it needs no data and it returns a color// We then call ClearScreen, and pass it the data returned from RandomColorClearScreen(RandomColor());
// Call the RefreshScreen methodRefreshScreen();
// Call the Delay method - pass in the number of milliseconds to delayDelay(delaySeconds * 1000);
Activities
How many method calls and how many arguments does each method have?
WriteLine("Hello World");
OpenWidow("Test", 1920, 1080);
int weight = (start + 10) * 2;
int age = ToInt32(line);
DrawLine(RandomColor(), x1, y1, Rnd(100), Rnd(100));
int width = Rnd(ToInt32(ReadLine()));
Note: Rnd
is a method that generates a random number.
Answers
- 1: This has one method call to
WriteLine
with a single string argument. - 2: This has one method call to
OpenWindow
with three arguments: a string and two numbers. - 3: This does not have any method calls.
- 4: This has one method call to
ToInt32
, which is passed one argument. - 5: There are four method calls on this line.
DrawLine
,RandomColor
, and two calls toRnd
.DrawLine
is passed five arguments,RandomColor
none, andRnd
is passed one. - 6: There are three method calls:
Rnd
,ToInt32
, andReadLine
.ReadLine
runs first and is passed no arguments. BothRnd
andToInt32
are passed one argument. The result ofReadLine
is passed toToInt32
. The result ofToInt32
is then passed intoRnd
.