Skip to content

Method Call

A method call is an instruction that gets the computer to run the code in a method. You use the name of the method to identify which method you want to run. Some methods require data, which you must pass as arguments to the method as part of the method call.

A method calls runs a method, passing in values for the method to use

Method Call — when, why, and how

As you have seen, methods are building blocks that contain instructions that do a task — sort of like mini-programs. A large part of your code will be calling methods to get them to do things you need done, in the order you need them done.

When you are thinking about calling a method, you need to know what it does, what to give it, and what it will give you back. Writing the code to call a method is then simple: you just provide the name, and a list of values for arguments.

The name of a method should help you know what that method does, and the method’s documentation will help you understand what you need to provide the method for it to do its job.

Arguments

When you call a Method, you need to pass a value for each argument. This value can be a literal or calculated value known as an expression.

Overloading

Languages like C# support something called overloading. Overloading lets programmers create multiple methods with the same name but different arguments. Each method with the same name should do the same general action, but provide a different option for the data you can pass in. For example, library might provide two versions of a method to print data to the terminal — one where you can pass in text, and another where you can pass in a number.

Overloading gives programmers even more flexibility in how they ask the computer to do things. If you look at the SplashKit documentation you will see several examples of overloaded methods. So make sure to pay attention to which version of a method you want to call, and the arguments you will need to pass for that version.

Method Calls and Sequence

Method calls are one instruction that gives you control of the program counter. A method call will save the current state of your program, and then set the program counter to the first instruction within the method. The sequence will then progress through the method, and when it ends, the location of the next instruction after the method call will be put into the program counter. In this way, the method is called and then returns to where it was called from. This allows us to maintain our focus on sequence. A method call lets you direct the sequence into the method and then back to your next instruction. As you read, design, or work with code for your program, you can focus on the program’s sequence and not worry about what happens within the methods you call.

In C#

A method call allows you to run the code in a method, getting its instructions to run before control returns to the point where the method was called.

A method call consists of a method’s name followed by an opening parenthesis, zero or more arguments (delimited by commas), a closing parenthesis, then a semicolon.

Examples

Basic Example

The code below contains a C# program with four method calls. Each method call runs the WriteLine method to output text to the terminal.

The method call starts with the method’s name that indicates the method to be called is WriteLine. Following the identifier is a list of values within parenthesis. These are the arguments passed to the method for it to use.

As the WriteLine method comes from the SplashKitSDK.SplashKit, this example program starts with a using directive to tell the compiler to look in the SplashKitSDK.SplashKit library for any methods that are called. Remember that programs run in sequence, so we need to provide this directive before writing any code that calls a method from this library. This is why it is convention to put using directives at the start of a program’s source code.

using static SplashKitSDK.SplashKit;
WriteLine("Count back from 2...");
WriteLine(2);
WriteLine(1);
WriteLine(0);

SplashKit Example

The following code uses three different methods from the SplashKit library. You can see the details for these on the methods page, but the names should give you a good idea of what this does.

using static SplashKitSDK.SplashKit;
DownloadSoundEffect("hello", "https://programmers.guide/resources/code-examples/part-0/hello-world-snippet-saddle-club.ogg", 443);
PlaySoundEffect("hello");
Delay(5000);

All of the arguments in this code are using literal values.

Example using result

Some methods return data, which you can use within any calculated value (expression) within your code. The following code demonstrates the use of this with the ReadLine method. The result of this is being stored in a variable and then used to output a message.

string name;
Write("Please enter your name: ");
name = ReadLine();
WriteLine($"Hello {name}!");

The following code demonstrates the use of the values returned by the RandomColor and Rnd methods.

using static SplashKitSDK.SplashKit;
OpenWindow("Random Color", 800, 600);
ClearScreen(RandomColor());
RefreshScreen();
Delay(1000 * Rnd(10));

Line 4 demonstrates the use of the result from calling RandomColor within the call to ClearScreen. In this case, the value returned from the RandomColor method becomes the value for the argument passed to ClearScreen. As you would expect, this will clear the screen to a random color.

Line 6 demonstrates this again, where the result of 1000 * Rnd(10) is passed to Delay. The easiest way to approach this is to consider each value independently. Rnd(10) will return a random value between 0 and 10 — let us imagine this returns 6. In this case, 1000 * Rnd(10) would become 1000 * 6, as the value returned by Rnd(10) was 6. This is then evaluated, and Delay will be passed the argument 6000, causing it to delay for 6 seconds.

Method Calls Up Close

Review the following images to explore how method calls work within the computer.