Professional Documents
Culture Documents
Chapter 4
• 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
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.ReadLine(): It reads a line of text from the console, allowing user input.
• Math.Abs(): Returns the absolute value of a number, useful in situations where you
need a positive value.
• Example:
double result = Math.Round(3.14159, 2);
• rounds pi to two decimal places, giving 3.14.
STRING MANIPULATION FUNCTIONS
STRING MANIPULATION FUNCTIONS
• Example:
• string text = "C# Programming";
• int length = text.Length; //sets length to 14.
DATE AND TIME FUNCTIONS
DATE AND TIME FUNCTIONS
• 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
Used when a function doesn't return any value; it performs an action without
providing a result.
COMMON RETURN TYPES
Functions returning strings are useful for tasks involving text manipulation or
generating textual output.
COMMON RETURN TYPES
Developers can create their own classes or structs to serve as return types,
encapsulating more complex data.