0% found this document useful (0 votes)
13 views24 pages

C++Ch5

Chapter Five discusses the concept of functions in programming, emphasizing their role in breaking down complex problems into manageable tasks. It covers the structure of functions, including their declaration, definition, parameters, and the differences between passing by value and passing by reference. Additionally, it highlights the importance of local and global variables, as well as the use of default arguments.

Uploaded by

tadasatokuma
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)
13 views24 pages

C++Ch5

Chapter Five discusses the concept of functions in programming, emphasizing their role in breaking down complex problems into manageable tasks. It covers the structure of functions, including their declaration, definition, parameters, and the differences between passing by value and passing by reference. Additionally, it highlights the importance of local and global variables, as well as the use of default arguments.

Uploaded by

tadasatokuma
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/ 24

Chapter Five

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,

f(int i, int k, int j) /* correct */


f(int i, k, float j) /* incorrect */
03/06/2025 4
Summarized function basics.
1. Every function must have a name.
2. Function names are made up and
assigned by the programmer following the
same rules that apply to naming variables.
 They can contain up to 32 characters, they
must begin with a letter, and they can
consist of letters, numbers, and the
underscore (_) character.

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:

return type function name(parameters);


 The function return type. This specifies the
type of value the function returns. A function
which returns nothing should have a return
type void.

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:

 Using a function involves ‘calling’ it.


 Calling a function means making the
instruction of the function to be executed.
 A function call consists of the function name
followed by the call operator brackets ‘()’,
inside which zero or more comma-separated
arguments appear. The number and type of
arguments should match the number of
function parameters..
 When a function call is executed, the
arguments are first evaluated and their
resulting values are assigned to the
corresponding parameters. The function body
is then executed. Finally the return value (if
any) is passed to the caller.
03/06/2025 13
Cont…
void func1(); prototype of
function definition
void main() {
…………
func1();
…………
}
definiti
Funct void func1() no semicolon. on
ion
body
Decelerator{
……………
...................
}
03/06/2025 14
Function Parameters and
arguments
The parameters of a function are list
of variables used by the function to
perform its task and the arguments
passed to the function during calling
of a function and, are values sent to
the function.
The arguments of function calling can
be using either of the two supported
styles in C++: passing by value or
passing by reference.

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

You might also like