0% found this document useful (0 votes)
29 views35 pages

L9 - Functions

Uploaded by

lkixome
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)
29 views35 pages

L9 - Functions

Uploaded by

lkixome
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/ 35

FUNCTIONS

By Sir. Joshua
Definition
 A function is a block of code which only runs when it is
called.
 You can pass data, known as parameters, into a function.
 Functions are used to perform certain actions, and they
are important for reusing code: Define the code once,
and use it many times.
 C++ provides some pre-defined functions, such as main(),
which is used to execute code.
 But you can also create your own functions to perform
certain actions.
 To create (often referred to as declare) a function, specify
the name of the function, followed by parentheses ():
Advantages of functions
 Using functions facilitates the reuse of the codes
in writing computer programs
 Theuse of functions help to improve the
program’s readability because it reduces the
complexity of the main function
 Functions help you to focus on just that part of
the program and construct it, debug it, and
perfect it.
 Different
people can work on different functions
concurrently.
Types of functions
 There
are two types of functions applied in
C++ programming, namely
i. user defined function and
ii. built-in/pre-defined function
User-defined function
 A user-defined function is the type of function
designed by a programmer according to the
user’s requirement.
 Programmers use the function to break down
large programs to simplify debugging, testing,
and maintenance of the program.
 A user-defined function that returns value is
called ‘returning function’.
 The function that does not return value is called
‘void function’
Syntax
 Thegeneral form of a C++ function
definition is as follows:

A C++ function definition consists of a function
header and a function body. Here are all the parts of
a function:
 Return Type: A function may return a value. The
return_type is the data type of the value the
function returns. Some functions perform the
desired operations without returning a value In this
case, the return_type is the keyword void.
 Function Name: This is the actual name of the
function. The function name and the parameter
list together constitute the function signature.

 Parameters: A parameter is like a
placeholder. When a function is invoked, you
pass a value to the parameter. This value is
referred to as actual parameter or argument.
The parameter list refers to the type, order,
and number of the parameters of a function.
Parameters are optional; that is, a function
may contain no parameters.
 Function Body: The function body
contains a collection of statements that
define what the function does.
Elements of user-defined function
 User-defined function contains three elements:
function definition, function call and function
prototype.
Function definition
 Function definition specifies all characteristics and
elements required in a user-defined function.
Function definition includes function header and
function body.
 Function header includes data type or returns type,
function name, and a list of parameters.
 On the contrary, function body involves all
executable statements within it.

Function call
 If
you want to use a defined function within the
main function, you have to call that function.
 During calling the function, you have to specify
the name of the function, return value and
parameters if they are included within the
function.
 There are different ways of calling a function
depending on whether the function returns a
value or not.

Note:
There are two ways to call functions which are
function call by value (pass by value) and
function call by reference (pass by reference).
a) Call by value involves copying the values of
actual parameters into the formal parameters.
b) Call by reference involves copying the
address of the arguments into actual
parameters within the function

Function prototype
 Before calling any function into the main function, it should be
defined.
 This is possible through writing function definition or function
prototype.
 Function prototype is the function header without function body.
 The function body will be given, but the function should be
introduced by writing its prototype.
 A function prototype contains return type, name, and
parameters usually terminated by a semicolon.
 An example of a function prototype is
int average (int a, int b);
Example of Returning Function
Example of Void Function
Function with Parameters

Example
Example 1
Example 2
Function Arguments
A function argument is a value that is passed
to a function when it is called, and the
function uses it to perform operations and
produce results.
 Forexample: when we call a function sum
we pass values 10 and 25, these values are
called arguments
Default Parameter Value
 You can also use a default parameter value,
by using the equals sign (=).
 If we call the function without an argument,
it uses the default value
Example:
Example 1
Example 2
Pass By Reference

 You can also pass a reference to the


function.
 This can be useful when you need to change
the value of the arguments.
A reference is a variable that points to the
address of another variable, representing its
value, location, or name.
 It is created with the & operator.
Example
Example
Function Overloading
 With function overloading, multiple
functions can have the same name with
different parameters.
 Instead of defining two functions that should
do the same thing, it is better to overload one.
 Inthe example below, we overload the
plusFunc function to work for both int and
double
Example
Recursion
 Recursionis the technique of making a
function call itself.
 This
technique provides a way to break
complicated problems down into simple
problems which are easier to solve.
 Recursion
may be a bit difficult to
understand.
 Thebest way to figure out how it works is to
experiment with it.
Recursion Example
 Adding two numbers together is easy to do,
but adding a range of numbers is more
complicated.
 In the following example, recursion is used
to add a range of numbers together by
breaking it down into the simple task of
adding two numbers:

Example Explained
 When the sum() function is called, it adds parameter k to
the sum of all numbers smaller than k and returns the
result. When k becomes 0, the function just returns 0.
When running, the program follows these steps:
 10 + sum(9)
 10 + ( 9 + sum(8) )
 10 + ( 9 + ( 8 + sum(7) ) )
 ...
 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the
program stops there and returns the result.
Built-in function
 Built-in
functions are functions stored within
standard library files.
 Such functions comprise mathematical, string
manipulation, conversions, and console based I/O
function.
 The application of the built-in function should
involve their header files.
 Forexample, <cmath> for the mathematical
functions and <cctype> for character
manipulation.
Some of Built-in functions
Example
Casting
 C++ provides the use of casting technique to
avoid implicit type conversion.
 Casting is used to do data type conversion from
one data type to another through the use of a
cast operator.
 The cast operator is also called type conversion
or type casting.
 Syntax for type casting is:
data_type(variable_name);
 Example: int(area)
Example

You might also like