FUNCTIONS and
SUB ROUTINES
KALVIN KARL C. NONATO, MIT
Objectives
▷ Define Function;
▷ Differentiate pre-defined and built-in functions;
▷ Identify the different parts of a function;
▷ Declare and call a function;
▷ Differentiate a function with parameter and
parameter less;
▷ Create a function with return and no return values.
2
BUILT-IN FUNCTION
▷ Predefined or built-in functions: Are
those that are part of the C# library.
These are functions that are
embedded in a particular
programming language.
3
USER-DEFINED FUNCTION
▷ User-defined functions: A
programmed routine that has its
parameters set by the user of the
system. Functions created by the
user or programmer.
4
What is a Function?
▷ A function allows you to encapsulate
a piece of code and call it from other
parts of your code. You may very
soon run into a situation where you
need to repeat a piece of code, from
multiple places, and this is where
functions come in.
5
What is a Function?
▷ A function is a named, independent
section of C# code that performs a
specific task and optionally returns a
value to the calling program.
6
A function is named.
Each function has a unique name. By
using that name in another part of the
program, you can execute the
statements contained in the function.
This is known as the calling program.
7
A function is independent.
A function can perform its task
without interference from or
interfering with other parts of the
program.
8
A function performs a specific task
This is the easy part of the definition. A
task is a discrete job that your program
must perform as part of its overall
operation, such as sending a line of text
to a printer, sorting an array into
numerical order, or calculating a cube
root.
9
Return a value to the calling program
When your program calls a
function, the statements it contains
are executed. These statements, if
desired, can pass information back
to the calling program.
10
SYNTAX:
access_modifier return_type function_ name(parameters)
{
//function body;
}
11
Access Modifier
It defines the type of the method from
where it can be accessed in your C#
application. In C# there are Public,
Protected, Private access modifiers.
12
Function Return Type
specifies the data type that the
function returns to the calling
program.
13
Function name
Function name must be unique. It
describes the name of the user
defined function by which the user
calls it or refer it.
14
Argument
is an actual value passed to the
function by the calling program. Each
time a function is called, it can pass
different arguments.
15
Parameter
is an entry in a function header; it
serves as a “place holder” for an
argument. A function’s parameter is
fixed; they do not change during
program execution.
16
Predefined rules in naming a function:
▷ The function name must be some kind of a
Noun or a verb.
▷ Its naming should be done in such a way that it
must describe the purpose of that function.
▷ The first letter of the function name can either
be a small letter or a Capital letter, however, it
is recommended to use the capital one.
17
USING NO PARAMETER AND RETURN TYPE
18
USING PARAMETER BUT NO RETURN TYPE
19
USING PARAMETER AND RETURN TYPE
20
USING PARAMETER BUT NO RETURN TYPE
21
USING PARAMETER AND RETURN TYPE
22