You are on page 1of 23

Fundamentals Of Computer

Problem Solving
(CSC415)

by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA

1
Function II

Chapter 6

2
Contents

6.1 void function


- value and reference parameters
6.2 The scope of an identifier
- local and global identifiers

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
6.1
void function
 void functions and value-returning functions have
similar structures
 Both have a heading part and a statement part

 User-defined void functions can be placed either


before or after the function main
 A void function does not have a return type
- The return statement without any value is
typically used to exit the function early
 Formal parameters are optional
 A call to a void function is a stand-alone statement

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
6.1
void function (cont.)
void function without parameters :
 Function definition syntax:

 void is a reserved word


 Function call syntax:

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
6.1
void function (cont.)
void function with parameters :
 Function definition syntax:

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
6.1
void function (cont.)
void function with parameters :

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 7


PROGRAMMING
6.1
void function (cont.)
void function with parameters :

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
6.1
void function (cont.)
void function with parameters :

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
6.1
void function (cont.)
void function with parameters :

• A formal parameter receives a copy of the content of


corresponding actual parameter

• A value parameter - a formal parameter that


receives a copy of the content of the corresponding
actual parameter

• A reference parameter - a formal parameter that


receives the location (memory address) of the
corresponding actual parameter
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 10
PROGRAMMING
6.1
void function (cont.)
void function with parameters :
• Value parameters :
• If a formal parameter is a value parameter
- The value of the corresponding actual parameter
is copied into it

• The value parameter has its own copy of the data

• During program execution, the value parameter


manipulates the data stored in its own memory
space
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 11
PROGRAMMING
6.1
void function (cont.)
void function with parameters :
• Reference parameters :
• If a formal parameter is a reference parameter
- It receives the address and stores the
corresponding actual parameter

• During program execution to manipulate the data,


the address stored in the reference parameter
directs it to the memory space of the corresponding
actual parameter

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
6.1
void function (cont.)
void function with parameters :
• Reference parameters :
• Can:
 Pass one or more values from a function
 Change the value of the actual parameter

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 13


PROGRAMMING
Example 6.4 :
6.1
void function (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
Example 6.5 :
6.1
void function (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
6.1
void function (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
6.1
void function (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
6.1
void function (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
6.1
void function (cont.)
Example 6.7 :

Calculate Grade.
This program has 3 functions :
1. main
a. get the course score
b. print the course grade

2. getScore
a. prompt the user for the input
b. get the input
c. print the course score

3. printGrade
a. calculate the course grade
b. print the course grade
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 19
PROGRAMMING
6.2
The scope of an identifier
• the scope of variables declared within a function or
any other block of instructions can only be used by
the own function and cannot be used outside of
them.

• can also declare global variables that can be used


by all do it outside any function; directly in the body
of the program.

• See Example 6.8.

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
6.2
The scope of an identifier (cont.)
// Example 6.8 : function example

#include <iostream.h>

int addition (int, int);

int main ()
{
int z; z is local variables to the
z = addition (5,3); function addition.
cout << "The result is " << z;
return 0;
}

int addition (int a, int b)


{ a, b and r are local
int r;
r = a+b;
variables to the
return (r); function addition.
}

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 21


PROGRAMMING
6.2
The scope of an identifier (cont.)
global variables:

#include <iostream.h>

float number = 50.6; // a global variable named


number

int main()
{
float number = 34.7; // a local variable named
number
cout << “The value of number is “<< number << endl;
return 0;
}

♦ The following output is displayed.


The value of number
December 2, 2023 is 34.7
CSC425 : INTRODUCTION TO COMPUTER
PROGRAMMING
22
6.2
The scope of an identifier (cont.)
• We can still access the global variable by using C++’s scope
resolution operator.
• Use symbol ‘ :: ‘; must be placed immediately before the
variable, as in ::number.
• The ‘ :: ‘ tells the complier to use the global variable.
#include <iostream.h>

float number = 50.6; // a global variable named


number

int main()
{
float number = 34.7; // a local variable named
number
cout << “The value of number is “<< ::number << endl;
return 0;
} 2, 2023
December CSC425 : INTRODUCTION TO COMPUTER 23
PROGRAMMING

You might also like