0% found this document useful (0 votes)
9 views10 pages

Tosend CC102-Functions

The document provides an overview of C++ functions, including standard and user-defined functions, their structure, and examples of usage. It explains the importance of function headers, return values, parameters, and common mistakes in function usage. Additionally, it highlights the concept of function prototypes and their placement in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Tosend CC102-Functions

The document provides an overview of C++ functions, including standard and user-defined functions, their structure, and examples of usage. It explains the importance of function headers, return values, parameters, and common mistakes in function usage. Additionally, it highlights the concept of function prototypes and their placement in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CC102 – Computer Programming, Intermediate

C++
Functions
Melgine M. Bauat, MSIT
Asst. Professor III
The C++ Functions
A C++ function or a subprogram is simply a chunk of C+
+ code that has:
A descriptive function name: Examples
computeTaxes to compute the taxes for an employee
isPrime to check whether or not a number is a prime number

A returning value (for value returning function)


The computeTaxes function may return with a double number
representing the amount of taxes
The isPrime function may return with a Boolean value (true or
false)

A Sub-process (for non-value returning function)


Execution of Processes or Statements

CC102 – Computer Programming, Intermediate


The C++ Standard Functions
A C++ standard function are groups in different libraries
which can be included in the C++ program

Example: SAME OUTPUT WITHOUT


USING FUNCTION
pow standard function in <cmath>
ibrary int base=2, exponent=3,result=1;
for (int exp=1; exp<=exponent; exp++
cout << “2 raised to 3 is “ << pow(2,3); result *= base;
}
OUTPUT: cout << “2 raised to 3 is “ << result;

2 raised to 3 is 8
Visit https://www.programiz.com/cpp-programming/library-function for more
standard library functions
CC102 – Computer Programming, Intermediate
The C++ User-Defined Functions
A C++ User-defined functions are functions developed by
the programmer to implement in the program
FUNCTION HEADER.
Containing return value type, function
VALUE RETURNING FUNCTION. name, and parameters.

Example: int -> the return value type is integer


add -> the function name.
int num1, int num2 -> Parameters
int add(int num1, int num2) {
required by the function to perform the
int sumofnumbers = num1 + task
num2;
return sumofnumbers;
FUNCTION BODY.
The statements inside the
} function (inside {} “curly
braces”)
Usage: int sum = add(4,5);
OR –cout
CC102 << “The Programming,
Computer sum is “ << add(4,5);
Intermediate
The C++ User-Defined Functions
NON-VALUE RETURNING FUNCTION
Example: FUNCTION HEADER.
Containing void keyword, function name,
parameters (but not required).
void showHeader() {
cout << “Student Information void -> keyword to define non-value
System”; returning function
cout << endl; showHeader -> the function name.
}
FUNCTION BODY.
Usage:
The statements to be executed inside the function
(inside {} “curly braces”)
showHeader();

CC102 – Computer Programming, Intermediate


The C++ User-Defined Functions
NON-VALUE RETURNING FUNCTION (sample with
parameter)
void greetEmployee(string employeename) {
cout << “Student Information System”
<< endl;
cout << “Welcome ” <<
employeename;
}
Usage:
string employee = “Melgine”;
greetEmployee(employee);
Output:
Student Information System
Welcome Melgine
CC102 – Computer Programming, Intermediate
The C++ User-Defined Functions
FUNCTION PARAMETERS. A function may don’t have or
can have 1 or more parameters to perform its task.
void employeeTax(string employeename, double salary, double tax_percent=0.12)
{
cout << “Employee Name: ” << employeename << endl;
cout << “Salary: ” << salary << endl; OUTPUT:
cout << “Tax Percent: ” << tax_percent << endl;
double tax = salary * tax_percent; Employee Name: Melgine
cout << “Tax: ” << tax << endl; Salary: 30000
Tax Percent: 0.12
} Tax: 3600
Usage:
employeeTax(“Melgine”, 30000.00); Employee Name: Ronaldin
Salary: 50000
employeeTax(“Ronaldin”, 50000.00, 0.15); Tax Percent: 0.15
Tax: 7500
CC102 – Computer Programming, Intermediate
The C++ User-Defined Functions
FUNCTION PARAMETERS. A function may don’t have or
can have 1 or more parameters to perform its task.

Required parameters must be placed at the start and the optional


parameters must be placed at the ends part
CORRECT DECLARATION:

void employeeTax(string employeename, double salary, double tax_percent=0.12)

WRONG DECLARATION:
void employeeTax(double tax_percent=0.12, string employeename, double salary)

CC102 – Computer Programming, Intermediate


The C++ User-Defined Functions
Incorrect Usage

int add(int num1, int num2) {


int sumofnumbers = num1 + num2;
return sumofnumbers;
}
1. Incompatible data type -> string sum = add(4,5);
2. Insufficient arguments -> cout << “The sum is “ <<
add(4);
3. Incorrect data type arguments -> cout << “The sum is “ <<
add(4,’a’);
4. Too much arguments supplied -> int sum = add(4,5,6);

CC102 – Computer Programming, Intermediate


The C++ User-Defined Functions
The function prototype. declaration in C++ of a function, its
name, parameters and return type before its actual declaration
Function Prototype.
Declared before the driver function (int
main)

Actual Declaration.
Placed after int main
driver.

CC102 – Computer Programming, Intermediate

You might also like