0% found this document useful (0 votes)
17 views

C++ Functions

Uploaded by

annemaxine93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

C++ Functions

Uploaded by

annemaxine93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

FUNCTIONS

IN
C++
INTRODUCTION TO FUNCTIONS
 A function groups a number of program
statements into a unit and gives it a name.
 This unit can then be invoked from other parts of
the
 Another reason to use functions is to reduce
program size.
 Any sequence of instructions that appears in a
program more than once is a candidate for being
made into a function. program.
FLOW OF CONTROL TO A FUNCTION
#include <iostream>
void starline(); //function declaration/ (prototype)
int main()
{
starline(); //call to function
cout << “Data type Range” << endl;
starline(); //call to function
cout << “char -128 to 127” << endl
starline(); //call to function
return 0;
}
// function definition
void starline() //function declarator/definition
{
for(int j=0; j<45; j++) //function body
cout << ‘*’;
}
THE FUNCTION DECLARATION
 The declaration tells the compiler that at some
later point we plan to present a function called
starline.
 The keyword void specifies that the function has
no return value, and the empty parentheses
indicate that it takes no arguments.
 Function declarations are also called prototypes
CALLING THE FUNCTION
 The function is called (or invoked, or executed)
three times from main().
 Each of the three calls looks like this:

starline();
 This is all we need to call the function:

1) the function name


2) followed by parentheses.
The syntax of the call is very similar to that of the
declaration, except that the return type is
not used.
3)The call is terminated by a semicolon.
THE FUNCTION DEFINITION
void starline() //declarator
{
for(int j=0; j<45; j++) //function body
cout << ‘*’;
cout << endl;
}
The definition consists of a line called the
declarator, followed by the function body. The
function body is composed of the statements that
make up the function, delimited by braces.
FUNCTION
SYNTAX
FUNCTION COMPONENTS
ELIMINATING THE DECLARATION
#include <iostream>
// starline() //function definition
void starline()
{
for(int j=0; j<45; j++)
cout << ‘*’;
cout << endl;
}
int main()
{
starline(); //call to function
cout << “Data type Range” << endl;
starline(); //call to function
cout << “char -128 to 127”
starline();
PASSING ARGUMENTS TO FUNCTIONS
 An argument is a piece of data (an int value, for
example) passed from a program to the function.
 Arguments allow a function to operate with
different values, or even to do different
 things, depending on the requirements of the
program calling it.
PASSING VARIABLES
#include <iostream>
void repchar(char, int); //function
declaration
int main()
// function definition
{
void repchar(char ch, int
char chin;
n) //function declarator
int nin;
{
cout << “Enter a character: “;
for(int j=0; j<n; j++)
cin >> chin; //function body
cout << “Enter number of times cout << ch;
to repeat it: “;
cout << endl;
cin >> nin;
}
repchar(chin, nin);
return 0;
}
Passing
by value.
RETURNING VALUES FROM FUNCTIONS
 When a function completes its execution, it can
return a single value to the calling program.
RETURNING VALUES FROM FUNCTIONS
#include <iostream>
float lbstokg(float);
//declaration
int main() float lbstokg(float pounds)
{ {
float lbs, kgs; float kilograms = 0.453592
cout << “\nEnter your * pounds;
weight in pounds: “; return kilograms;
}
cin >> lbs;
kgs = lbstokg(lbs);
cout << “Your weight in
kilograms is “ << kgs <<
endl;
return 0;
INLINE FUNCTIONS
To save execution time in short functions, you
may elect to put the code in the function body
directly inline with the code in the calling
program. That is, each time there’s a function
call in the source file, the actual code from the
function is inserted, instead of a jump to
the function
Functions
versus
inline
code
INLINE FUNCTION
This kind of function is written like a normal
function in the source file but compiles into
inline code instead of into a function.
The source file remains well organized and easy
to read, since the function is shown as a separate
entity.
However, when the program is compiled, the
function body is actually inserted into the
program wherever a function call occurs.
#include <iostream>
inline float lbstokg(float pounds)
{
return 0.453592 * pounds;
}
int main()
{
float lbs;
cout << “\nEnter your weight in pounds: “;
cin >> lbs;
cout << “Your weight in kilograms is “ << lbstokg(lbs)
return 0;
}

You might also like