|
| 1 | +--- |
| 2 | +title: Methods to Use |
| 3 | +--- |
| 4 | + |
| 5 | +At this point the programs we create will include sequences of method calls, with variables that we can use to work with data within our program. |
| 6 | + |
| 7 | +To build programs we will need to have some [methods](/book/part-1-instructions/1-sequence-and-data/1-concepts/02-method) that we can [call](/book/part-1-instructions/1-sequence-and-data/1-concepts/03-method-call). The great this is that [library](/book/part-1-instructions/1-sequence-and-data/1-concepts/10-library) creators will describe the methods that they have created so that we can find and use them. In order to use this documentation we need to know how these methods will be described. |
| 8 | + |
| 9 | +## Method Declarations |
| 10 | + |
| 11 | +Methods are usually communicated using part of their declaration. We will look at building these ourselves in [Part 2](/book/part-2-organised-code/2-organising-code/0-overview), but for now we need to know some basics so that we can start calling methods. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +The above image helps show the components of a method declaration. The key parts are: |
| 16 | + |
| 17 | +- The *name* of the method. Remember, to call the method you use its name. |
| 18 | +- The variables and types of any *arguments* you will need to pass when you call the method. These will be listed as variables after the method name in parentheses. |
| 19 | +- You can also see the type of data that is returned (where `void` means none). |
| 20 | +- You also need to check that the method is `public` and `static`. We can call these directly if you have `using static ClassName;` at the top of your code file. For example, `WriteLine` is in the `System.Console` code, so you can call it if you have `using static System.Console` at the top of the code. |
| 21 | + |
| 22 | +The following code snippets show example method declarations. |
| 23 | + |
| 24 | +In the details of the [Console code](https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-8.0#system-console-writeline(system-string)) in the .NET documentation, there is the following documentation to show one of the `WriteLine` methods. |
| 25 | + |
| 26 | +```cs |
| 27 | +public static void WriteLine (string? value); |
| 28 | +``` |
| 29 | + |
| 30 | +This is available to call directly, as it is `public` and `static`. We can use this in our code using: |
| 31 | + |
| 32 | +```cs |
| 33 | +using static System.Console; |
| 34 | + |
| 35 | +WriteLine("Hello World"); |
| 36 | +``` |
| 37 | + |
| 38 | +The following shows the declaration of the [Open Window](https://splashkit.io/api/windows/#open-window) method from the SplashKit documentation. It is similar to the code above, but also includes the class name so that you know where the method exists. We can call it directly as it is `public` and `static`. |
| 39 | + |
| 40 | +```cs |
| 41 | +public static Window SplashKit.OpenWindow(string caption, int width, int height); |
| 42 | +``` |
| 43 | + |
| 44 | +This is in the `SplashkitSDK.SplashKit` class, so you can call the `OpenWindow` method using the following code. This will pass "This is the caption" to the `caption` data, 800 to the `width`, and 600 to the `height`. This code will return a `Winddow` value, which we can ignore or store in a `Window` variable. |
| 45 | + |
| 46 | +```cs |
| 47 | +using static SplashkitSDK.SplashKit; |
| 48 | + |
| 49 | +OpenWindow("This is the caption", 800, 600); |
| 50 | +``` |
| 51 | + |
| 52 | +For the final example let's look at [Fill Circle](https://splashkit.io/api/graphics/#fill-circle-3) and [Color Red](https://splashkit.io/api/color/#color-red). These have the following declaration on the SplashKit website. |
| 53 | + |
| 54 | +```cs |
| 55 | +public static void SplashKit.FillCircle(Color clr, double x, double y, double radius); |
| 56 | +public static Color SplashKit.ColorRed(); |
| 57 | +``` |
| 58 | + |
| 59 | +We can see from its declaration that we can call these (they are public and static). Their names are `FillCircle` and `ColorRed`. Will `FillCircle` we need to pass it a Color, and three double values. `ColorRed` does not need to be passed anything, and will return a Color value. The following is an example of calling this method. Here we use the result of `ColorRed()` as the value passed into the color for `FillCircle`. |
| 60 | + |
| 61 | +```cs |
| 62 | +using static SplashkitSDK.SplashKit; |
| 63 | + |
| 64 | +OpenWindow("This is the caption", 800, 600); |
| 65 | +ClearScreen(ColorWhite()); |
| 66 | + |
| 67 | +FillCircle(ColorRed(), 400, 300, 100); |
| 68 | +RefreshScreen(); |
| 69 | +``` |
| 70 | + |
| 71 | +In the next pages we will list the different methods that you will use to complete the activities in this chapter both for terminal and graphical programs. |
| 72 | + |
| 73 | +## Other Websites |
| 74 | + |
| 75 | +You can call these methods on the *class* in which they are defined, and we will work up to this over the next few parts. To simplify things, we want to avoid doing this for now and instead call these directly as we have seen here. |
| 76 | + |
| 77 | +When you read other websites, you will often see them calling methods like this: `Console.WriteLine();`. This is the equivalent of calling `WriteLine();` when you have `using static System.Console;` at the top of your code. When you find code like this, you will need to convert it so that you can call the method directly. |
0 commit comments