0% found this document useful (0 votes)
2 views15 pages

Functions

The document explains the concept of functions in C++, including predefined functions and how to define custom functions. It details the components of a function, such as declaration and definition, as well as the use of return statements and void functions. Examples are provided to illustrate how functions can be implemented in a program to perform specific tasks.

Uploaded by

sglearn685
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)
2 views15 pages

Functions

The document explains the concept of functions in C++, including predefined functions and how to define custom functions. It details the components of a function, such as declaration and definition, as well as the use of return statements and void functions. Examples are provided to illustrate how functions can be implemented in a program to perform specific tasks.

Uploaded by

sglearn685
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/ 15

Functions

Software Development
Function
 What is a function?

• A program can be thought of as consisting


subparts, such as obtaining the input data,
calculating the output data and displaying the
output data.

• C++, like most programming languages, has


facilities to name and code each of these subparts
separately.

• In C++ these subparts are called functions.


Predefined Functions
Example on predefined
functions
//Computes the size of a dog house that can be purchased given the
//user’s budget
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double COST_PER_SQ_FT = 10.50;
double budget, area, length_side;
cout << "Enter rhe amount budgeted for your dog house $";
cin >> budget;
area = budget/COST_PER_SQ_FT;
length_side = sqrt(area)
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "For a price of $" << budget << endl
Enter the amount budgeted
<< "I can build for you a luxurious square dog house\n" for
<< "that is " << length_side
<< "feet on each side.\n"; your dog house: $ 25.00
return 0; For a price of $ 25.00
} I can build you a luxurious
square dog house
that is 1.54 feet on each side.
Programmer Define
Function
 We can define our own functions, either in
the same file as the main part of our
program of in a separate file so that the
functions can be used by several different
programs.
Function
 There are two components to define a
function:

1. Function declaration (or function prototype)


◦ Shows how the function is called
◦ Must appear in the code before the function can be called
◦ Normally placed before the main part of your program

2. Function definition (or function body)


◦ Describes how the function does its task
◦ Can appear before or after the function is called
◦ Normally placed after the main part of your program or in a
separate file.
Function Declaration

double total(int number, double price);


#include <iostream>
using namespace std;

double total_cost(int number_par, double price_par);

int main()
{
double price, bill;
int number;

cout << "Enter the number of items purchased: ";


cin >> number;
cout << "Enter the price per item $";
cin >> price;

bill = total_cost(number, price);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << number << " items at " << "$" << price << " each. \n" <<"Final bill, including tax, is $" << bill
<< endl;
system("pause");
return 0;
}
double total_cost(int number_par, double price_par)
{
const double TAX_RATE = 0.05; //5% sales tax
double subtotal;
subtotal = price_par * number_par;
return (subtotal + subtotal*TAX_RATE);
}
The return statement
• A return statement consists of the keyword return
followed by an expression.

• When a return statement is executed, the function


call ends

• Returns the value calculated by the function

• A void function does not necessarily contain a


return statement, however it may optionally have
one or more return statement.
The return statement
• Syntax
return expression;
• expression performs the calculation
or
• expression is a variable containing the calculated
value

• Example
return (subtotal + subtotal * tax);
 or
 return subtotal + subtotal * tax;
Function Call
• A function call is an expression consisting of
the function name followed by arguments
enclosed in parentheses.

• If there is more than one argument, the


arguments are separated by commas.

• A function call is an expression that can be


used like any other expression of the type
specified for the value returned by the
function.
Function Call
function name
arguments list

bill = total(number, price);

return value assigned to bill


Void Function
 What is a void-function?

• Subtasks are implemented as functions in


C++.

• In C++, a function must either return a


single value or return no values at all.

• A function that returns no value is called a


void function.
Void Function
void Function_Name(Parameter_List);

 There are two main differences between


void-function definitions and the definitions
of functions that return one value:

 Keyword void replaces the data type of the


function
 No return statement
Void Function

void print (int number, double price);


{
double tax= 0.05;
double subtotal;

subtotal= number * price_par - tax;


cout << subtotal;
}

You might also like