Ece532 Chapter 1.4 Function p1
Ece532 Chapter 1.4 Function p1
ECE 532
CHAPTER 1: REVISION OF COMPUTER
PROGRAMMING CONCEPT (FUNCTION
PART 1)
Lecturer: Dr. Roslina Mohamad
Room: Tower 2, Level 13, No: 14C
Tel: 03-55436068
2
Before We Begin…
• Recite Du’a
3
FUNCTION
PART 1 – PASS-BY-VALUE FUNCTION
1)VOID FUNCTION
2)VALUE-RETURNING FUNCTION
4
At the end of this lecture, you will be able to:
5
6
7
Introduction
• Functions are like building blocks
• They allow complicated programs to be
divided into manageable pieces
• Some advantages of functions:
• A programmer can focus on just that part of the
program and construct it, debug it, and perfect it
• Different people can work on different functions
simultaneously
• Can be re-used (even in different programs)
• Enhance program readability
User-Defined Functions
Value-returning functions: have a return
type
Return a value of a specific data type using the
return statement
Void functions: do not have a return type
Do not use a return statement to return a
value
8
PROTOTYPING, CALLING AND
DEFINING FUNCTIONS
9
Example of Function
Function
Prototype
Function
Call
10
Example of Function
Function
Definition
11
FUNCTION PROTOTYPE
Function prototype: function heading without the body of the
function
Syntax:
12
Calling a Function
13
14
Function Definition
15
Definition includes
return type: data type of the value the function returns to
the part of the program that called it
name: name of the function. Function names follow
same rules as variable names
parameter list: variables that hold the values passed to
the function
body: statements that perform the function’s task
Function Definition
16
17
Example using a Function Prototype
int main()
{
int a=10;
cout<<a<<" squared: "<<square(a)<<endl;
19
20
PASS-BY-VALUE FUNCTION
When an argument is passed by value, a copy of
the argument’s value is made and passed (on the
function call stack) to the called function.
Changes to the copy do not affect the original
variable’s value in the caller.
22
Passing Data by Value
O Pass by value: when an argument is passed to a function, a
copy of its value is placed in the parameter
O The function cannot access the original argument
O Changes to the parameter in the function do not affect the
value of the argument in the calling function
23
Passing Data by Value
24
val num
5 5
argument in parameter in
25
26
29
Value-Returning Functions with Passing
by Value
30
Syntax: Value-Returning Function
Syntax:
31
SYNTAX: FORMAL PARAMETER LIST
32
Returning a Value from a Function
return statement can be used to return a value
from the function to the module that made the
function call
Prototype and definition must indicate data type of
return value (not void)
Calling function should use return value, e.g.,
› assign it to a variable
› send it to cout
› use it in an arithmetic computation
› use it in a relational expression
33
* Returning a Value – the return
Statement
* Format: return expression;
* expression may be a variable, a literal value, or an
expression.
* expression should be of the same data type as the declared
return type of the function (will be converted if not)
34
35
36
Example
37
Example
38
Example of Value-Returning Functions
(Refer to Gaddis)
39