0% found this document useful (0 votes)
33 views13 pages

CH 5

The document discusses functions in C++. It explains that a function must be declared with a prototype specifying its name, return type, and parameters. It then must be defined with a body of statements. Functions can take parameters by value or by reference. Passing by reference means changes to the parameters will be reflected in the calling function's arguments.

Uploaded by

lemmademewakene
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)
33 views13 pages

CH 5

The document discusses functions in C++. It explains that a function must be declared with a prototype specifying its name, return type, and parameters. It then must be defined with a body of statements. Functions can take parameters by value or by reference. Passing by reference means changes to the parameters will be reflected in the calling function's arguments.

Uploaded by

lemmademewakene
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/ 13

Chapter Five

Functions
Introduction
 Using functions in your program requires that you first declare the
function and that you then define the function
 The declaration tells the compiler the name, return type, and
parameters of the function
 The definition tells the compiler how the function works
 The declaration of a function is called its prototype
Defining the Function
The definition of a function consists of the function header and its body
The header is exactly like the function prototype, except that the
parameters must be named, and there is no terminating semicolon
The body of the function is a set of statements enclosed in braces
Function Definition Syntax
return_type function_name ( [type parameterName1], [type
parameterName2]...)
{
statements;
}
Function Definition Examples
Long area(long l, long w)
{
return l*w;
}
Void printmessage(int whichMsg)
{
If (whichMsg == 0)
Cout<<“Hello.\n”;
If (whichMsg == 1)
Cout<<“Goodbye.\n”;
If (whichMsg >1)
Cout<<“I am confused.\n”;
}
Function Statements
There is virtually no limit to the number or types of statements that
can be in a function body
Although you can't define another function from within a function,
you can call a function
Declaring the Function
There are three ways to declare a function:
Write your prototype into a file, and use the #include directive to
include it in your program
Write the prototype into the file in which your function is used
Define the function before it is called by any other function
Function Prototype Syntax
return_type function_name ( [type [parameterName1] type
[parameterName2]]...);
The function prototype and the function definition must agree exactly
about the return type, the name, and the parameter list
A prototype that looks like this is perfectly legal
long Area(int, int);
This prototype declares a function named Area() that returns a long
and that has two parameters, both integers
Function Prototype Examples
• long FindArea(long length, long width); // returns long, has two
parameters
• void PrintMessage(int messageNumber); // returns void, has one
parameter
• int GetChoice(); // returns int, has no parameters
• BadFunction(); // returns int, has no parameters
Example:- // function example
#include <iostream>
using namespacestd;
int addition (int a, int b)
{
Int r;
r=a+b;
return(r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is "<< z;
return 0;
}

We can see how the mainfunction begins by declaring the variable z of type
int
Right after that, we see a call to a function called addition
Cont..

The parameters and arguments have a clear correspondence.


 Within the main function we called to addition passing two values: 5 and 3,
that correspond to the int a and int b parameters declared for function
addition
The value of both arguments passed in the call (5 and 3) are copied to the local
variables int a and int b within the function.
Function addition declares another local variable (int r), and by means of the
expression r=a+b, it assigns to r the result of a plus b.
Because the actual parameters passed for a and b are 5 and 3 respectively, the
result is 8.
Scope of Variables
Reading Assignment
Arguments passed by value and by reference
The arguments passed to the functions have been passed by value
 For example, suppose that we called our first function addition using
the following code:
int x=5, y=3, z;
z = addition ( x , y );
What we did in this case was to call to function addition passing the
values of x and y, i.e. 5 and 3 respectively, but not the variables x and
y themselves
Cont…

To explain it in another way, we associate a, b and c with the arguments


passed on the function call (x, y and z)
That is why our program's output, that shows the values stored in x, y
and z after the call to duplicate, shows the values of all the three
variables of main doubled.
Void duplicate (int& a, int& b, int& c = void duplicate (int a, int b, int c)
// more than one returning
Quiz value
#include <iostream>
What is the output of the following function? using namespacestd;
voidprevnext (intx, int& prev,

?
int& next)
{
prev = x-1;
next = x+1;
}
intmain ()
{
intx=100, y, z;
prevnext (x, y, z);
cout << "Previous="<< y << ",
Next="<< z;
return0;
}
// passing parameters by reference
#include <iostream>
using namespacestd;
Example:- voidduplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
intmain ()
{
intx=1, y=3, z=7;
duplicate (x, y, z);
cout << "x="<< x << ", y="<< y << ", z="<< z;
return 0; }

The first thing that should call your attention is that in the declaration of
duplicate the type of each parameter was followed by an ampersand sign (&)
This ampersand is what specifies that their corresponding arguments are to
be passed by reference instead of by value

You might also like