Save
...
Finals - MMW
2ndQ - Computer Programming 1
Functions
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
Marc
Visit profile
Cards (18)
A
function
is a unit of code designed to perform a particular purpose or specific task.
Function
- It allows a programmer to organize a program and break down a certain program into a smaller, manageable, and reusable codes.
A
function
can take input values, process them, and display or return an output.
Types of Functions
User-Defined Functions
- It is to create our own function based on our requirements.
Built-In Functions
- These are built-in functions in C++ that are available to use, sucha as sqrt(), abs(), isdigit(), etc.
Key Features of C++ Functions
Modularity
Reusability
Parameterization
Return Values
Built-in
and
User-defined
Modularity
Functions help break a program into smaller, manageable, and logical parts (modules).
Each function performs a specific task, making the code easier to organize and debug.
Reusability
Once a function is defined, it can be reused multiple times without rewriting the code.
This reduces redundancy and makes the code more efficient and easier to maintain.
Parameterization
Functions can accept inputs (parameters) to operate on, making them flexible.
For example, a function can calculate the sum of different pairs of numbers based on arguments passed.
Return Values
Functions can return a value to the caller after performing a task.
For instance, a function can return the result of a mathematical operation.
Built-in
and
User-defined Functions
Built-in Functions: C++ provides several predefined functions, such as sqrt() for square root or cout for output.
User-defined Functions: Programmers can create their own functions to perform specific tasks.
Function Declaration Syntax
returnType
functionName
(
parameter1
,
parameter2
, ...){
// function body
}
returnType
- It specifies what type of value a function returns, such as int, void - a return type does not return any value.
functionName
- It is a identifier that is used to refer to the particular function in a program.
parameters
(optional) - List of values passed to a function.
function body
- It includes the programming statements that are used to perform some tasks.
Calling a Function
functionName
(
argument1
,
argument2
, ...);
argument
(optional) - The values to be passed in the function.
functionName
- It is an identifier that is used to refer to the particular function in a program.