Programming Part 3
Programming Part 3
[CHIBI TINODAISHE M]
FUNCTION
WHAT IS A FUNCTION ?
1. A function is a named block of code that performs a specific task and returns a value.
2. There are sub programs that perform a specific function and they return a value to the calling
program
3. Functions can be used to encapsulate common code, making it easier to reuse and maintain.
4. There are two types of function that are to be known at A level :
1) Inbuilt functions
2) User defined functions
Example
Sub Main()
Dim num1, num2, sum As Integer
Console.WriteLine("Enter the first number")
num1 = Console.ReadLine
Console.WriteLine("Input the second number") Calling statement for a
num2 = Console.ReadLine function to work there is need
of a calling statement
sum = sumfunction(num1, num2)
Console.WriteLine(num1 & " + " & num2 & " = " & sum)
Else MsgBox("input numrical terms")
End If
Console.ReadKey()
End Sub
Passing Parameter
A passing parameter, or argument is a value that is passed into a function when it is
called
Lets use an example to make this more concrete
So you have a function called square(x), which takes a number as an argument and
returns the square of that number.
Byval
It means that a copy of the argument is made and that a copy is passed into function.
We are going to copy the actual value from number 1 is placed to a variable to the argument
within the value
Function sumfunction(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
ByRef
We are not moving the value but the location where the value we are going to use is stored.
Inbuilt functions
Are functions that are provided by the visual basic language itself.
They can be used to perform a variety of tasks, such as string manipulation, mathematical
operations, and date and time calculations.
Inbuilt functions are called directly from your code, without the need to create a separate
function definition.
They are typically very efficient, and can save you a lot of time and effort when writing code
Examples in string manipulation
Left(
Right(
Mid(
Instr(
InstrRev(
Ucase
Replace
Len(
Mathematical Functions
Sqr()
Exp()
Int()
Rnd()
Round()
Sin()
Cos()
Tan()
Date and time functions
Date()
Time()
DateAdd()
DateDiff()
DatePart()
FormatDateTime() etc.
NB : sometimes you are not asked to use these inbuilt functions but from the function you can
tell that use of inbuilt functions to validate the program is needed study the question carefully
Solution
Dim n As Integer
Dim result As Integer
Console.WriteLine("Please enter a number: ")
n = Console.ReadLine()
If n < 0 Then
Console.WriteLine("Invalid number")
Console.WriteLine("Please enter a number greater than 0.")
Else
result = Math.Pow(n, 2)
Console.WriteLine(result)
End If
Console.ReadKey()
Recursion
Is technique which a function calls itself repeatedly until a certain condition is met.
It’s a way of breaking down a complex problem into a smaller , simpler problems.
Example let’s say you want to write a function that prints the numbers from 1 to 1o.
You could write a for loop to do this, or you could use recursion.
To use recursion you would write a function that prints the first number and then calls itself
with the next number.
This process would continue until it reached 10.
Typical exam question
Solution
Imports System.Console
Module Module1
Function factorial(ByVal n As Integer)
If n = 0 Then
Return 1
Else
Return n * factorial(n - 1)
End If
End Function
Sub Main()
WriteLine("Input the number")
Dim n As Integer = ReadLine()
Dim result As Integer = factorial(n) 'calling the function and storing the value
into the result variable
WriteLine(result)
ReadKey()
End Sub
End Module
String Manipulation
String manipulation is the process of changing or altering a string of text.
This can involve adding, removing, or changing characters or substrings, or performing
other operations like reversing the order of characters or finding specific patterns.
There are a variety of methods and techniques for string manipulation in Visual
Basic.NET.
Some common methods are Substring(), Replace(), Trim(), and Format().
Solution
Module Module1
Function ReplaceEWithO(sentence As String) As String
Dim newSentence As String = ""
Dim index As Integer = 0
Do While index < sentence.Length
Dim charIndex As Integer = sentence.IndexOf("e", index)
If charIndex >= 0 Then
newSentence += sentence.Substring(index, charIndex - index)
newSentence += "o"
index = charIndex + 1
Else
newSentence += sentence.Substring(index)
Exit Do
End If
Loop
Return newSentence
End Function
Sub Main()
Dim sentence As String = "This is a sentence with the letter e in it."
Dim newSentence As String = ReplaceEWithO(sentence)
Console.WriteLine(newSentence)
Console.ReadKey()
End Sub
End Module
Practice Questions
1. Write a program that takes a string as input and returns a new string with all the vowels
removed.
2. Write a program that takes a string as input and returns a new string with all the words
reversed.
3. Write a program that takes a string as input and returns a new string with all the
duplicate characters removed.
4. Write a program that takes a string as input and returns a new string with all the
characters sorted in alphabetical order.
5. Write a program that takes a string as input and returns a new string with all the words
capitalized.
6. Write a program that takes a sentence as input and checks if it is a palindrome (i.e., it reads the
same forwards and backwards).
7. Write a program that takes two sentences as input and checks if they are anagrams of each
other (i.e., they contain the same letters in different orders).
8. Write a program that takes a sentence as input and counts the number of occurrences of each
letter in the sentence.
-Chibi Tinodaishe M
Cell:0781081816
Email: tinodaishemchibi@gmail.com
Isaiah 43 vs 2
THANK YOU
=====================================================