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

Prog II Chapter 2

This document provides an overview of functions in C++, covering topics such as modular programming, function declaration, calling functions, variable scope, function arguments, return values, and recursion. It explains the advantages of using functions, the difference between user-defined and built-in functions, and how to pass arguments by value or by reference. Additionally, it discusses default parameters and the concept of recursive functions, along with the scope of variables in programming.

Uploaded by

estifanos haile
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)
4 views

Prog II Chapter 2

This document provides an overview of functions in C++, covering topics such as modular programming, function declaration, calling functions, variable scope, function arguments, return values, and recursion. It explains the advantages of using functions, the difference between user-defined and built-in functions, and how to pass arguments by value or by reference. Additionally, it discusses default parameters and the concept of recursive functions, along with the scope of variables in programming.

Uploaded by

estifanos haile
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/ 34

DEPARTMENT OF

COMPUTER SCIENCES
Course Title: Computer 1
Programming II
Chapter 2:Functions

05/11/2025
OUTLINE
Introduction to Modular Programming (Functions);
Declaration of functions; Definition of functions
 Calling a function
Scope of Variables
Function Arguments; Return Values
Default Parameters; Parameters passing; Call by
value; Call by reference
Recursive functions
05/11/2025 2
INTRODUCTION TO FUNCTIONS

 A function is a named block of code that


perform some action.
 The statement written in function are
executed when it is called by its name.
 Each function has a unique name.
 Function are the building block of C++
programs.
 They are used to perform the tasks that are
repeated many times
05/11/2025 3
Advantages of Functions
Easier to Code
Easier to Modify
Easier to Maintain
Reusability
Less Programming
Time
Easier to Understand
05/11/2025 4
C++ Functions
 C++ allows the use of both internal
(user-defined) and external (Built
in) functions.
 External functions are usually
grouped into specialized libraries
 (e.g., iostream, stdlib, math,
etc.)

05/11/2025 5
User-Defined Functions
C++ programs usually
have the following form:

// include statement
// function prototype
// main() function
// function definitions

05/11/2025 6
FUNCTIONS
 In C++, a function is a group of statements that is given a name,
and which can be called from some point of the program.
 Functions allow to structure programs in segments of code to
perform individual tasks.
 The most common syntax to define a function is:
type name ( parameter1, parameter2, ...)
{
statements
}
Where:
- type : is the data type specifier of the data returned by the function.
- name is the identifier by which it will be possible to call the function
- parameters (as many as needed): Each parameter consists of a type followed by
an identifier, with each parameter being separated from the next by a comma.
-statements is the function's body. It is a block of statements surrounded by braces
{ }. 05/11/2025 7
DECLARING FUNCTIONS

The prototype above yields the following information to the compiler:


■ func is the function name
■ the function is called with two arguments:
 the first argument is of type int,
 the second of type double
■ the return value of the function is of type long.
05/11/2025 8
DECLARING FUNCTIONS
 A function has a name and a type, much like a variable.
 The function’s type is defined by its return value, that is,
the value the function passes back to the program.
 In addition, the type of arguments required by a function
is important.
 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.

05/11/2025 9
CALLING FUNCTION
 A function call is an expression of the same type as the function and whose value
corresponds to the return value. The return value is commonly passed to a
suitable variable.

 In this example the function pow()is first called using the arguments x and 3.0,
and the result, the power x3, is assigned to y.
 As the function call represents a value, other operations are also possible.
 Thus, the function pow() can be used to perform calculations for double values.

05/11/2025 10
CALLING FUNCTION CON…

Now suppose the function return some integer


value, you can use a variable to store that value.
i.e z=add(x,y);

05/11/2025 11
FUNCTION CATEGORIES

i) Function with no return value and no argument.


void add(void);
ii) Function with arguments passed and no return
value.
void add(int,int);
iii) Function with no arguments but returns a
value.
int add(void);
iv) Function with arguments and returns a value.
int add(int,int);

05/11/2025 12
FUNCTIONS CON…

 Example Function Out put of the


// function example program
#include <iostream>
using namespace std;
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;
}
A function can actually be called multiple times within a
program, and its argument is naturally not limited just to
literals: 05/11/2025 13
FUNCTIONS CON…
 In order to examine this code,:
 C++ program always begins its execution by the main function.
 We can see how the main function begins by declaring the variable z
of type int.
 Right after that, we see a call to a function called addition.
 Paying attention we will be able to see the similarity between the
structure of the call to the function and the declaration of the function
itself some code lines above:

 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
05/11/2025 14
function addition.
FUNCTIONS CON…
 At this moment the program follows it regular course from the same
point at which it was interrupted by the call to addition.
 But additionally, because the return statement in function addition specified a
value: the content of variable r (return (r);), which at that moment had a value of 8.
 This value becomes the value of evaluating the function call.

• So being the value returned by a function the value given to the


function call itself when it is evaluated, the variable z will be set to
the value returned by addition (5, 3), that is 8.
• To explain it another way, you can imagine that the call to a
function (addition (5,3)) is literally replaced by the value it returns
05/11/2025 15
(8).
FUNCTIONS WITH NO TYPE -THE USE OF VOID

The syntax shown above for functions:


type name ( argument1, argument2 ...)
{
statements
}
 Requires the declaration to begin with a type.
 This is the type of the value returned by the function.
 But what if the function does not need to return a
value? In this case, the type to be used is void, which is
a special type to represent the absence of value.
 For example, a function that simply prints a message
05/11/2025 16
may not need to return any value:
FUNCTIONS - THE RETURN VALUE OF MAIN
You may have noticed that the return type of main is int, but
most examples in this and earlier chapters did not actually
return any value from main.
Well, there is a catch: If the execution of main ends normally
without encountering a return statement the compiler
assumes the function ends with an implicit return statement:
return 0;
 Note that this only applies to function main for historical
reasons.
 All other functions with a return type shall end with a
proper return statement that includes a return value, even if
this is never used.
05/11/2025 17
FUNCTIONS - ARGUMENTS AND PARAMETER

A function declaration my have parameters for the entire


operation of the function
The parameters in the function declaration have a clear
correspondence to the arguments passed in the function call.
For Example

int addtion (int x, int y)


{

}
 We are passing values to the addition operation with two
parameters called X and Y.
 Parameter is variable in the declaration of function.
 Argument is the actual value of this variable that gets passed
05/11/2025 18
to function.
ARGUMENTS PASSED BY VALUE AND BY REFERENCE

 In most case, arguments have always been passed by value.


 This means that, when calling a function, what is passed to the
function are the values of these arguments on the moment of the
call, which are copied into the variables represented by the function
parameters.
 For example, take:
int x=5, y=3, z;
z = addition ( x, y );
 In this case, function addition is passed 5 and 3, which are copies
of the values of x and y, respectively.

05/11/2025 19
ARGUMENTS PASSED BY VALUE AND BY REFERENCE

 But in certain cases, though, it may be useful to access


an external variable from within a function.
 To do that, arguments can be passed by reference,
instead of by value.
 To gain access to its arguments, the function declares its
parameters as references.
 In C++, references are indicated with an ampersand (&)
following the parameter type, as in the parameters taken
by duplicate in the example above.

05/11/2025 20
ARGUMENTS PASSED BY VALUE AND BY REFERENCE

 For example the following code shows function


declaration with parameters as references.

05/11/2025 21
FUNCTION WITH NO RETURN VALUE AND NO ARGUMENT
No
Argumen
t

No
Return

05/11/2025 22
FUNCTION WILL NOT RETURN ANY VALUE BUT PASSES ARGUMENT

05/11/2025 23
DEFAULT VALUES IN PARAMETERS

 In C++, functions can also have optional parameters, for which


no arguments are required in the cal.,
 For example, a function with three parameters may be called with
only two. For this, the function shall include a default value for its
last parameter, which is used by the function when called with
fewer arguments.
 For example:
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
 The above function pass only one variabel value (i.e a) the second
variabel b has a defualt value thus we don’t need to pass value to05/11/2025
it.
24
DEFAULT VALUES IN PARAMETERS

In the code above, the function can be called as:


divide (12)
In this case, the function assumes the second parameter
to be 2 (notice the function definition, which declares its
second parameter as int b=2). Therefore, the result is 6.
In another call, the function may be called as:
divide (20,4)
The call passes two arguments to the function.
Therefore, the default value for b (int b=2) is ignored,
and b takes the value passed as argument, that is 4,
yielding a result of 5. 05/11/2025 25
RECURSIVE FUNCTION

Recursivity is the property that functions have to be


called by themselves.
It is useful for some tasks, such as sorting
elements, or calculating the factorial of numbers.
For example, in order to obtain the factorial of a
number (n!) the mathematical formula would be:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
More concretely, 5! (factorial of 5) would be:
5! = 5 * 4 * 3 * 2 * 1 = 120
And a recursive function to calculate this in C++
26
could be:.
05/11/2025
RECURSIVE FUNCTION

05/11/2025 27
RECURSIVE FUNCTION
The function factorial we included a call to
itself, but only if the argument passed was
greater than 1, since, otherwise, the function
would perform an infinite recursive loop, in
which once it arrived to 0, it would continue
multiplying by all the negative numbers
(probably provoking a stack overflow at some
point during runtime).

05/11/2025 28
SCOPE OF VARIABLES
 Scope is defined as the extent up to which something
can be worked with.
 In programming also the scope of a variable is defined as
the extent of the program code within which the variable can
be accessed or declared or worked with.
 scope of a variable is its lifetime in the program.
 This means that the scope of a variable is the block of code
in the entire program where the variable is declared, used,
and can be modified.
 There are mainly two types of variable scopes:
1.Local Variables
2.Global Variables
05/11/2025 29
GLOBAL VARIABLES
As the name suggests, Global Variables can be
accessed from any part of the program.
They are available through out the life time of a
program.
They are declared at the top of the program
outside all of the functions or blocks.
Declaring global variables: Global variables are
usually declared outside of all of the functions and
blocks, at the top of the program.
They can be accessed from any portion of the program.
05/11/2025 30
#DEMO GLOBAL VARIABLES

05/11/2025 31
LOCAL VARIABLES
Variables defined within a function or block
are said to be local to those functions.
Anything between ‘{‘ and ‘}’ is said to inside
a block.
Local variables do not exist outside the block
in which they are declared, i.e. they can
not be accessed or used outside that block.
Declaring local variables: Local variables
are declared inside a block.
05/11/2025 32
05/11/2025 33
END 05/11/2025 34

You might also like