0% found this document useful (0 votes)
3 views

20201219 Computing Programming Technique Computing

The document discusses programming techniques, focusing on subroutines, procedures, and functions, explaining their definitions, syntax, and advantages. It also covers the concepts of validation, authentication, and the classification of programming languages, including machine code, assembly language, and high-level languages, along with their respective advantages and translation methods. Additionally, it highlights the structured programming approach and the importance of variable scope in programming.

Uploaded by

Harrison Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

20201219 Computing Programming Technique Computing

The document discusses programming techniques, focusing on subroutines, procedures, and functions, explaining their definitions, syntax, and advantages. It also covers the concepts of validation, authentication, and the classification of programming languages, including machine code, assembly language, and high-level languages, along with their respective advantages and translation methods. Additionally, it highlights the structured programming approach and the importance of variable scope in programming.

Uploaded by

Harrison Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Technique

Subroutines, procedures and functions


Functions and procedures are two different types of subroutine.
A function definition is used to define the steps within the function.
A function call tells the program to branch to the function, execute it and come back to the next statement in the
main program.

Here is the syntax to define a function:


Static int/string/bool nameofsubroutine(parameters)
If you want to create a function to add up two marks for a test you might create a subroutine like this:
Static int markaddition(int mark1, int mark2)
{
Int totalmark;
Totalmark = mark1 + mark2;
Return totalmark
}
You would call the function in the main subroutine like this:
Markaddition(2,5)
If you wanted to put what the subroutine returns in a variable you might do this:
Totalmark = Markaddition(2,5)

Procedures are another type of subroutine that do not return a value to the main program. They may be useful to
break up a program into modules.

Here is the syntax to define a procedure:


Static void nameofsubroutine
If you want to create a procedure to print out text e.g. a picture of a square you might create a subroutine like this:
Static void square
{
Console.Writeline(“here is a square”)
Console.Writeline(“****”)
Console.Writeline(“****”)
Console.Writeline(“****”)
Console.Writeline(“****”)
}
Advantages of subroutines:
• Each subroutine can be tested separately to make sure it works correctly
• Many programmers can work on a large program at the same time, cutting down development time
• Some subroutines can be re-used in other programs
• Program maintenance is easier – just the affected subroutines need to be modified
The “scope” of a variable, constant, procedure or function defines the parts of the program in which it is recognised
and can be used.
E.g.
Variables var1 and var2 are global variables and can be seen anywhere in the program
Variables a, b and c can only be seen and used inside func1
Variables x, y and z can only be seen and used inside func2

Local variables only exist while the subroutine is executing. They are only accessible within
the subroutine.
Constants follow the same rules as variables. A constant declared in the main program can be seen and used, but
not changed, in any function

Advantage of local variables


• Local variables keep a subroutine self-contained, so it can be used in any program without variable names
conflicting with those used in the calling program

Global variables are variables that can be seen and used anywhere in the program.

The structured programming approach:


A structured programming approach is one which has these characteristics:
1. It uses subroutines to break down the program into manageable chunks of code
2. It uses only the constructs of sequence selection iteration and recursion
Advantages of a structured programming approach:
 It is easy to understand
 It is easy to debug
 It is easy to maintain
Validation and authentication
Data validation routines can ensure that data entered is of the right type - for example, an integer, Validation
cannot ensure that the user has not entered a wrong value, or made a spelling mistake in a name.

There are many different types of validation check including:

static bool rangecheck(int minimum, int maximum, int num)


{
if (num < minimum || num > maximum) return false;
else return true;
}

static bool typecheck(string x)


{
try
{
Convert.ToInt32(x);
} catch (Exception err)
{
Console.WriteLine("The provided input was not an integer!");
return false;
}
return true;
}

static bool lengthCheck(string x, int minimum, int maximum)


{
if (x.Length < minimum || x.Length > maximum) return false;
else return true;
}

static bool presenceCheck(string x)


{
if (String.IsNullOrEmpty(x)) return false;
else return true;
}

static bool formatCheck(string email)


{
if (!email.Contains('@') || !email.Contains('.')) return false;
else return true;
}
Validation can only check that the data entered is reasonable.

Verification is used to double-check that the data has been typed in correctly. For example, a user setting a new
password may be asked to type it in twice. If the two passwords don’t match, they will be asked to start again.

Authentication routines are used to make sure a person is who they claim to be

Classification of programming languages


Machine code is specific to a particular processor or family of processors e.g. intel’s x86.

The computer can decode and execute a machine code instruction directly. All computers execute machine code
instructions but you don’t have to write in machine code.

A computer cannot execute a program instruction until it is translated into machine code.

Machine code: 101011001001 – this means LOAD the value 1 into the accumulator

Assembly language is also processor specific. It has to be translated into machine code before it can be executed.
Like machine code, it is classified as a low-level language.

Assembly language: LDA 51 – this means Load the contents of memory location 51 into the accumulator

High-level languages generally have statements that look a bit like English or maths so they are relatively easy to
learn and understand:
area = (base * height) / 2
print (area)
• They have selection and iteration constructs
• They use Boolean operators such as AND, OR, NOT
• They have data structures such as arrays and records
• A single statement usually translates into several machine code instructions
All high level languages must be translated into machine code. The translation is done by a program which may be
either a compiler or an interpreter

A different compiler or interpreter is used for each type of processor to translate the high level language code into
machine code

Advantages of high level languages:


• A high-level language is easier to learn
• Programs can be written faster in a high-level language
• It is easier to understand and debug a high-level language
Advantages of low level languages:
• A program written in a low-level language executes very fast
• The code will occupy less memory than the machine code produced by translating a high-level language
• Statements in a low-level language can be used to control and manipulate specific hardware components

Assembly language is often used to develop software for embedded systems because it can be used to control and
manipulate specific hardware components.

There are three common types of program translators:


1. A Compiler – a compiler translates high level language into machine code

2. An interpreter - An interpreter is another type of program that translates a high-level language into machine
code. Unlike a compiler, no object code is produced. It translates each line of code and executes it
immediately. If it reaches a line with a syntax error, it stops and displays an error message

Here are the differences between a compiler and an interpreter:

3. An Assembler - An assembler is a program which translates assembly code into machine code.

There is generally a 1:1 (one-to-one) correspondence between each assembly code statement and the equivalent
machine code statement.

High-level languages are compiled or interpreted.

Assembly language is assembled.

You might also like