Skip to content

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.

Illustration of some method calls

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 library
using 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 output
WriteLine("Testing method calls");
int delaySeconds;
string userInput;
// Call the Write method - the argument is the text to output
Write("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 window
OpenWindow("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 RandomColor
ClearScreen(RandomColor());
// Call the RefreshScreen method
RefreshScreen();
// Call the Delay method - pass in the number of milliseconds to delay
Delay(delaySeconds * 1000);

Activities

How many method calls and how many arguments does each method have?

  1. WriteLine("Hello World");
  2. OpenWidow("Test", 1920, 1080);
  3. int weight = (start + 10) * 2;
  4. int age = ToInt32(line);
  5. DrawLine(RandomColor(), x1, y1, Rnd(100), Rnd(100));
  6. 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 to Rnd. DrawLine is passed five arguments, RandomColor none, and Rnd is passed one.
  • 6: There are three method calls: Rnd, ToInt32, and ReadLine. ReadLine runs first and is passed no arguments. Both Rnd and ToInt32 are passed one argument. The result of ReadLine is passed to ToInt32. The result of ToInt32 is then passed into Rnd.