0% found this document useful (0 votes)
43 views39 pages

Ece532 Chapter 1.4 Function p1

Uploaded by

Husna Nuna
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)
43 views39 pages

Ece532 Chapter 1.4 Function p1

Uploaded by

Husna Nuna
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/ 39

1

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:

Revise how to construct and use a value-


returning, void, user-defined function in a
program
Revise the concept of pass-by-value
function

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

Function prototype: a function declaration statement

Function call: a statement that causes a function to execute


Function definition: the statements that make up a function

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:

It is not necessary to specify the variable name in the


parameter list
The data type of each parameter must be specified

12
Calling a Function
13

 To call a function, use the function name


followed by () and ;
printHeading();
 When a function is called, the program executes
the body of the function
 After the function terminates, execution resumes
in the calling module at the point of call
 Format Function Call
CALLING FUNCTIONS WITH
MULTIPLE ARGUMENTS ILLUSTRATION
displayData(height, weight); // call

void displayData(int h, int w)// heading


{
cout << "Height = " << h << endl;
cout << "Weight = " << w << endl;
}

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

//See Example 1.19 18


Example using a Function
Definition (without Function
Prototype)
#include<iostream>
using namespace std;

int square(int x) // Function Definition


{
return x*x;
}

int main()
{
int a=10;
cout<<a<<" squared: "<<square(a)<<endl;

//See Example 1.20

19
20

Void Function (Non-return Type) & Function Return


Type

• If a function does not return a value, its return type is


void
void printHeading()
{
cout << "\tMonthly Sales\n";
}

• If a function returns a value, the type of the value must


be indicated
int main()
21

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

 Example: int val = 5;


evenOrOdd(val);

val num
5 5
argument in parameter in

calling function evenOrOdd function


 evenOrOdd can change variable num, but it will
have no effect on variable val
Void Functions

25
26

Example of Void Functions with no Parameter (Refer to Gaddis)

•Program without using function prototype

//See Example 1.21, Example 1.22,

Example 1.23, Example 1.24

•Program using function prototype

// See Example 1.25


27

Example of Void Functions with a Parameter(s)/ Pass-by-Value (Refer to Gaddis)

// See Example 1.26, Example 1.27,


Example 1.28, Example 1.29
28
Value-Returning Functions with
Passing by Value
 To use these functions you must:
◦ Include the appropriate header file in your program
using the include statement
◦ Know the following items:
 Name of the function
 Number of parameters, if any
 Data type of each parameter
 Data type of the value returned: called the type
of the function

29
Value-Returning Functions with Passing
by Value

 Because the value returned by a value-returning


function is unique, must:
 Save the value for further calculation
 Use the value in some calculation
 Print the value
 A value-returning function is used in an assignment or
in an output statement
 One more thing is associated with functions:
 The code required to accomplish the task

30
Syntax: Value-Returning Function

 Syntax:

 functionType is also called the data type or return


type

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

Example of Value-Returning Functions


Value-Returning Functions
• Heading: first four properties above
– Example: int abs(int number)
• Formal Parameter: variable declared in the
heading
– Example: number
• Actual Parameter: variable or expression listed
in a call to a function
– Example: x = pow(u, v)

36
Example

//See Example 1.30

37
Example

//See Example 1.31

38
Example of Value-Returning Functions
(Refer to Gaddis)

//See Example 1.32, Example 1.33,


Example 1.34

39

You might also like