C++Ch5
C++Ch5
Functions
03/06/2025 1
Functions
A very complex problem can be
divided to smaller tasks which may be
divided into simpler tasks, then
implementing each simple task by a
subprogram or a function
A function is a block of code designed to
tackle a specific problem.
Advantage:
Easy to read and to write
Easy to troubleshoot (error)
Easy to understand
03/06/2025 2
Cont…
The general form of a function is:
ret-type function-name(parameter list)
{
body of the function
}
The ret-type specifies the type of data that the
function returns.
The parameter list is a comma-separated list of
variable names and their associated types that
receive the values of the arguments when the
function is called.
A function may be without parameters, in which
case the parameter list is empty. However, even if
there are no parameters, the parentheses are still
required.
03/06/2025 3
Cont…
In contrast to a variable declaration,
all function parameters must be
declared individually, each including
both the type and name.
That is, the parameter declaration list
for a function takes this general form:
f(type varname1, type varname2, . . .
, type varname)
For example,
03/06/2025 5
3. All function names have one set of
parenthesis immediately following them.
This helps you (and C++ compiler)
differentiate them from variables.
4. The body of each function, starting
immediately after parenthesis of the
function name, must be enclosed by
braces.
03/06/2025 6
Rules for defining
functions
1. Define prototype //declaration
2. Define the body of a function
3. Call the function //inside the main function
#include <iostream>
using namespace std;
int main ()
{
Cout<<“Welcome to function body ”
return 0;
}
03/06/2025 7
Declaring function (prototype):
When a function is declared, the compiler must
therefore be provided with information on
the name and type of the function and
the type of each argument.
o This is also referred to as the function prototype.
The interface of a function (also called its
prototype) specifies how it may be used.
Syntax:
03/06/2025 8
Cont…
The function name. this is simply a
unique identifier
The function parameters (also called
its signature). This is a set of zero or
more typed identifiers used for passing
values to and from the function.
Example: int test(int);// informs the
compiler that the function test() is of
type int, i.e. its return value is of type
int, and it expects an argument of type
int.
03/06/2025 9
Defining a function:
• A function definition consists of two parts:
interface (prototype) & body. The brace
of a function contains the computational
steps (statements) that computerize the
function.
• The definition consists of a line called the
decelerator.
• If function definition is done before the
main function, then there is no need to
put the prototype, otherwise the
prototype should be scripted before the
main function starts.
03/06/2025 10
Example
#include<iostream>
Using namespace std;
Void display(int a, int b){
Cout<<a+b;
}
int main(){
Stt;
return 0;
}
03/06/2025 11
Cont…
But if function definition is done after
main function you have to script the
prototype above the main function.
void display(int, int);
int main(){
return 0;
}
void display(int a, int b){
Statement;
}
03/06/2025 12
Calling the function:
03/06/2025 15
Passing by value:
A value parameter receives a copy of
only the value of the argument passed
to it.
As a result, if the function makes any
changes to the parameters, this will
not affect the argument.
This method copies the value of an
argument into the formal parameter of
the subroutine. In this case, changes
made to the parameter have no effect
on the argument.
03/06/2025 16
Example:
#………
void display(int num) {
num=0;
cout<<"The num is="<<num<<endl;
}
int main() {
int x=10;
display(x);
cout<<"x is="<<x;
return 0; }
03/06/2025 17
Passing by Reference
In this method, the address of an
argument is copied into the
parameter.
Inside the subroutine, the
address is used to access the
actual argument used in the call.
This means that changes made
to the parameter affect the
argument. Passing parameters in
this way is called pass-by-
03/06/2025 18
Example
#………
void display(int &num) {
num=0;
cout<<"The num
is="<<num<<endl;
}
int main() {
int x=10;
display(x);
cout<<"x is="<<x;
return 0; }
03/06/2025 19
Global versus local variables
Everything defined at the program scope
level (outside functions) is said to have a
global scope, meaning that the entire
program knows each variable and has the
capability to change any of them.
#.....
int year=2008;//global variable
int sum(int, int); //global function
int main(){
………………
}
03/06/2025 20
Local variable
Variables defined within a local scope are visible to that
scope only.
Hence, a variable need only be unique within its own
scope. Local scopes may be nested, in which case the
inner scope overrides the outer scopes.
Eg: int x;//x is global
void display(int xyz)//xyz is local to the body of
display
{
if(xyz > 0)
{
Double xyz;//xyz is local to this block
…
}
}
03/06/2025 21
Default Argument
Some times it is called optional
parameters
A default parameter is nothing,
but initializing the default value
to variable during function
definition
A default value is a value to use
if none is supplied.
03/06/2025 22
Example
Void display(int a, int b=56, int c=12)
{
cout<<“a is”<<a;<<endl;
cout<<“b is”<<b<<endl;
cout<<“c is”<<c<<endl;
}
int main()
{
display(20, 30, 50);
return 0;
}
03/06/2025 23
The
End!
03/06/2025 24