You are on page 1of 24

COMPUTER PROGRAMMING 2

Chapter 4

PREPARED BY: JOSHUA RELATORRES


OBJECTIVES:

• Understand the concept of procedures and functions in C# and their role in code
organization.
• Learn how to define and declare functions in C#, including specifying their return
types.
• Explore various return types such as void, int, string, and custom data types.
• Comprehend the significance of parameters in functions and their impact on data
flow.
• Master the process of passing parameters to functions and utilizing them within the
function's scope.
PROCEDURES AND
FUNCTIONS
PROCEDURES/FUNCTIONS

• referto reusable blocks of code that perform a


specific task or set of tasks.
• also known as subroutines, or methods, depending
on the programming language you are using.
PROCEDURES/FUNCTIONS -
IMPORTANCE
PROCEDURES/FUNCTIONS -
IMPORTANCE

• They simplify software development by providing reusable


and tested code, reducing the need for developers to
reinvent the wheel and speeding up development processes.
PROCEDURES VS FUNCTIONS
PROCEDURES VS FUNCTIONS

Procedures
• Typically used when you want to encapsulate a series of
statements to be executed sequentially, without needing to return a
result.
• It does not return a value. They are used for their side effects, such
as modifying data, performing actions, or controlling program
flow.
PROCEDURES VS FUNCTIONS

Functions
• It is used to group a sequence of statements, but their primary
purpose is to return a value or result based on the computation
performed inside them.
• It always returns a value, which can be of a specified data type,
and this value can be used in expressions or assigned to variables.
STANDARD INPUT/OUTPUT FUNCTIONS
STANDARD INPUT/OUTPUT FUNCTIONS

• Console.WriteLine(): This function is used to output text to the console, making it a


fundamental tool for communicating with users in console applications.

• Console.ReadLine(): It reads a line of text from the console, allowing user input.

• Example: Console.WriteLine("Hello, World!"); prints "Hello, World!" to the console.


MATHEMATICAL FUNCTIONS
MATHEMATICAL FUNCTIONS

• Math.Abs(): Returns the absolute value of a number, useful in situations where you
need a positive value.

• Math.Round(): Rounds a floating-point number to the nearest integer.

• Example:
double result = Math.Round(3.14159, 2);
• rounds pi to two decimal places, giving 3.14.
STRING MANIPULATION FUNCTIONS
STRING MANIPULATION FUNCTIONS

• string.Length: Provides the length (number of characters) of a string.

• string.Substring: Extracts a portion of a string based on specified indices.

• Example:
• string text = "C# Programming";
• int length = text.Length; //sets length to 14.
DATE AND TIME FUNCTIONS
DATE AND TIME FUNCTIONS

• DateTime.Now: Retrieves the current date and time.

• DateTime.Parse: Converts date and time strings into DateTime objects.

• Example:
DateTime currentDateTime = DateTime.Now;
• gets the current date and time.
RETURN TYPES
RETURN TYPES IN C#

• Return types are declarations that specify what type of data a function will return.

• Function's Output: They determine what the function will provide as its output
when called.

• Role in Function Design: Return types play a pivotal role in designing functions
and defining their behavior.
COMMON RETURN TYPES

• void Return Type


• Numeric Return Types
• string Return Types
• Custom Data Types
COMMON RETURN TYPES

void Return Type:

Used when a function doesn't return any value; it performs an action without
providing a result.
COMMON RETURN TYPES

Numeric Return Types:

Functions can return integers (int), floating-point numbers (double/float), or


other numeric types.
COMMON RETURN TYPES

string Return Type:

Functions returning strings are useful for tasks involving text manipulation or
generating textual output.
COMMON RETURN TYPES

Custom Data Types:

Developers can create their own classes or structs to serve as return types,
encapsulating more complex data.

You might also like