0% found this document useful (0 votes)
25 views7 pages

Programming Part 3

A function is a named block of code that performs a specific task and returns a value. There are two types of functions: inbuilt functions provided by Visual Basic and user-defined functions created by the programmer. User-defined functions encapsulate common code, making it easier to reuse and maintain. Functions can accept parameters using ByVal to pass a copy of the argument or ByRef to pass the argument by reference. String manipulation involves changing or altering strings using methods like Substring(), Replace(), and Trim(). Practice questions test skills like removing vowels, reversing words, removing duplicates, and checking for palindromes and anagrams.

Uploaded by

shammahzuze05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
25 views7 pages

Programming Part 3

A function is a named block of code that performs a specific task and returns a value. There are two types of functions: inbuilt functions provided by Visual Basic and user-defined functions created by the programmer. User-defined functions encapsulate common code, making it easier to reuse and maintain. Functions can accept parameters using ByVal to pass a copy of the argument or ByRef to pass the argument by reference. String manipulation involves changing or altering strings using methods like Substring(), Replace(), and Trim(). Practice questions test skills like removing vowels, reversing words, removing duplicates, and checking for palindromes and anagrams.

Uploaded by

shammahzuze05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

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

User Defined functions


 Functions that are created by a programmer(you).
 They are used to perform any task that the programmer needs, and can be used to make code
more reusable and efficient.
 A function is built on top of the sub main or at the end of the end sub.
Format
 Function function name (parameter list) As Data type Add function

Example

Function sumfunction(num1 As Integer, num2 As Integer) As Integer


Dim sum As Integer
sum = num1 + num2
Return sum
End Function

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.

We have two methods

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.

Function sumfunction(ByRef num1 As Integer, ByRef num2 As Integer) As Integer

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

Typical exam question


 Write a program that asks the user to enter a number and prints out the square of that number.
If the user enters a negative number, the program should print "Invalid number" and ask the
user to enter a new number.
Use Visual Basic.NET and the inbuilt Math.Pow() function.(10)

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

Write a function that will calculate the factorial of a given number(10)

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().

Typical exam question


 Write a program that takes a sentence as input and replaces every instance of the letter
"e" with the letter "o". If a letter is not an "e", do not replace it.
 This question requires you to use the Substring() and IndexOf() methods, as well as a
few other techniques.(15)

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.

For the solutions to question contact details below


Programming is a simple and pure form of programming.

-Chibi Tinodaishe M

Cell:0781081816

Email: tinodaishemchibi@gmail.com

Isaiah 43 vs 2

THANK YOU
=====================================================

You might also like