Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Builtin functions

A function is a named section of code that works something out and returns a value that can be used elsewhere in the code.

Builtin functions are tools within a programming language that you don't have to write from scratch: they just work out of the box.

Top tips:

  • There are loads more builtin functions for VB.NET and python than the ones listed here. 
    This website only shows the builtin functions defined in pseudocode. 
Skill
Description
VB.NET
Python
C#
Length of a string
    

A string is some text such as "Hello"

The length of a string means how many characters (including spaces, numbers and punctuation) there are in that string

This example createa a string called name and sets it to "Bob". It counts how many characters are in that string and stores the answer (3) into a variable called l

# create string variable name = "Bob" # count characters and store length l = len(name)
// create string variable string name = "Bob"; // count characters and store length int l = name.Length;
Choose a random number
   

Computers can't think for themselves - they can only follow instructions., so getting them to choose a number that is truely random is not easy. Thankfully, most programming languages have a function that will choose a number for you that appears to be chosen at random.

This example generates a random integer between 0 and 10 and saves it into a variable called r

# do this once, at the start of your code import random ' store a random number between 0 and 10 r = random.randint(0, 10)
// do this once, at the start of your code Random rnd = new Random(); // store a random number between 0 and 10 int r = rnd.Next(0, 10);
Displaying text
    

Most programs need to output text to the user. 

This example displays the text "Hello" to the screen

# display text print("Hello")
// display text Console.WriteLine("Hello");
Get user input
    

Most programs need to get the user to type in data that controls how the program behaves.

This program asks the user for their name and saves the result into a string variable called name.

# Prompt the user to enter some text name = input("Enter your name")
// Prompt the user to enter some text Console.WriteLine("Enter your name"); string name = Console.ReadLine();
Casting (converting) to a string
     

Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.

This example converts the integer number 1000 to a string containing "1000"

# create an integer variable int_number = 1000 # cast the integer to a string str_number = str(int_number)
// create an integer variable int intNumber = 1000; // cast the integer to a string string strNumber = intNumber.toString();
Casting (converting) to an integer
     

Often it's useful to round a decimal number (real data type) to a whole number (integer data type).

It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer

This example converts the string "1000" to the integer 1000

# create a string variable containing an integer str_number = "1000" # cast the string to an integer int_number = int(str_number)
// create a string variable containing an integer string strNumber = "1000"; // cast the string to an integer int intNumber = int.Parse(strNumber);
Casting (converting) to a real
     

Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)

This example converts the string "1.1234" to the real value 1.1234

# Create a string variable containing a real value str_number = "1.1234" # Cast the string to a real float_number = float(str_number)
// Create a string variable containing a real value strNumber = "1.1234"; // Cast the string to a real floatNumber = double.Parse(strNumber);

Loading...