You are on page 1of 30

Functions

|
Objectives
• Explain the concept of structured programming and
its significance
• Define a function and describe the main parts of a
function
• Differentiate between function parameters and
arguments
• Describe function calls and the concept of
recursion
• Write programs that use functions

Monday,
November
29, 2021
Functions | 2
Functions I

|
Structured Programming
• Structured programming is a programming
technique that involves breaking down of
individual program tasks into smaller tasks and
performing them by independent sections of
program code.

• By using functions in your C++ programs, you


are practicing Structured Programming.

Monday,
November
29, 2021
Functions | 4
Advantages of Structured Programming
• It’s easier to write a structured program, because
complex programming problems are broken down into
smaller, simpler tasks.

• It’s easier to debug (find errors) a structured program,


because the structured design makes it easy to isolate
the problem to a specific section of code.

• Time saving, because you can write a function to


perform a certain task once and reuse it more than
once or even in another program
Monday,
November
29, 2021
Functions | 5
Functions
A normal function
takes in an input
value, operates
on it and gives an
output:
f(x) = 2x + 10
Assume f(x) is a
function:

Source:
If input x = 2,
https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Function_mac
hine2.svg/1035px-Function_machine2.svg.png output f(x) = 14

Monday,
November
29, 2021
Functions | 6
Functions in Programming
• A function is a named, independent section
of code that performs a specific task and
optionally returns a value to the calling
program.

• Parts (Components) of a function:


– Function header

– Function body

Monday,
November
29, 2021
Functions | 7
Function Header
• Function header contains the following:
returnType functionName(type param1…)
– Name: every function has a unique name. Rule for naming functions
are the same as those for naming variables.
– Parameter List: Parameter list provides argument type information.
This is a list of variables followed by commas
– Return Type: specifies the data type that the function returns to the
calling program. The function can return any of the C data types
(char, int etc). You can also define a function to return nothing using
return type “void”

void unlock (int key)


float calculateArea (int length, int width)

Monday,
November
29, 2021
Functions | 8
Function Body
• Function body is enclosed in braces.
float calculateArea (int length, int width) //function header
{
statement1;
…….. //function body
}

• Function body has statements, which when


executed perform the specific task assigned to
that function.

Monday,
November
29, 2021
Functions | 9
Class Exercise 1a
• Identify the errors in the following function
headers:

Monday,
November
29, 2021
Functions | 10
How to Use Functions in C++

• There are 3 steps to be followed when


working with functions:
1. Prototype a function (.h) – optional

2. Define a function (.cpp)

3. Execute a function (.cpp)

Monday,
November
29, 2021
Functions | 11
Defining Functions

• Function Prototype is a description of a function that will


appear later in the program. Contains the function header
only.
float area (int radius); //ends with semi-colon

• Function definition is the function itself, and it is


composed of a function header and a function body.
float area (int radius) //function header with NO semi-colon
{
statement1;
…….. //function body
}

Monday,
November
29, 2021
Functions | 12
Executing Functions

• A C++ program does not execute the statements in


a function until the function is called by another
part of the program.

• When a function is called, the program can pass


data values to the function in the form of one or
more arguments (variables).

Monday,
November
29, 2021
Functions | 13
Executing Functions (Cont.)
• The statements in the function then execute,
performing whatever task each was designed to
do. When the function’s statements have finished,
execution passes back to same location in the
program that called the function .

• Functions can send data back to the program in the


form of a return value.

Monday,
November
29, 2021
Functions | 14
Execution Flow
Function calling initiates
execution of function
definition

Function definition should be


done before calling

Monday,
November
29, 2021
Functions | 15
Exercise
1. Write a program that will use functions
to calculate the area of a rectangle

Monday,
November
29, 2021
Functions | 16
Monday,
November
29, 2021
Functions | 17
Class Exercise

Write the output of the


program below:

Monday,
November
29, 2021
Functions | 18
Exercise

1. Write a program that will use functions


to calculate the area and circumference
of a circle.

Monday,
November
29, 2021
Functions | 19
Functions II

|
Parameters and Arguments
• A function is invoked (executed) when it is called by its
name in another function.

• Arguments are values passed to the function when it is


called. Parameters are (argument) type information
provided in the function header.

• The number and type of arguments passed to the


function must match the number and data type of the
parameters at the function header.

Monday,
November
29, 2021
Functions | 21
Parameters
and
Arguments

Monday,
November
29, 2021
Functions | 22
Variable Scope: Global & Local
• Variables declared inside a function are
called local Variables. This means that they
can only be used/accessed inside that
particular function only.

• Variables declared outside all the functions


are called global variables. They can be
used/accessed by all the functions.
Monday,
November
29, 2021
Functions | 23
Global and
Local
Variables

Monday,
November
29, 2021
Functions | 24
Recursion
• Recursion refers to a
situation in which a function
calls itself either directly or
indirectly.

• Indirect recursion occurs


when one function calls
another function that then
calls the first function.

Monday,
November
29, 2021
Functions | 25
Exercise

Write the output


of the code
segment.

Monday,
November
29, 2021
Functions | 26
Exercise

Write the output


of the following
code segment

Monday,
November
29, 2021
Functions | 27
Exercise
• Write a program that will declare a 0.7 as a global
constant. The program should have a function
getSal() to calculate and return the net salary of
person given the gross salary as the input. Use the
main() function to prompt the user to enter gross
salary and also to display the net salary.
[Hint: netSalary = 0.7*grossSalary]

Monday,
November
29, 2021
Functions | 28
References
• http://www.tutorialspoint.com/cprogram
ming/cprogramming_tutorial.pdf
• P. Aitken, B.L. Jones, Sams Teach Yourself
C, 5th Edition

Monday,
November
29, 2021
Functions | 31
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: info@strathmore.edu Website: www.strathmore.edu
|

You might also like