Professional Documents
Culture Documents
Functions
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1
Todays Topics
Function
Scope
Parameter Passing
Modular Programming
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 2
1
Functions
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 3
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 4
2
Example: Calculate the average of four numbers (a,b,c and d)
avrg= (a+b+c+d)/4
avrg
One piece of data come out
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 5
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 6
3
Figure 4-4: Declaring, calling and defining functions
Back to the
calling function
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 7
Declaring a function
return_type function_name ( formal_parameter_list );
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 8
4
Examples of function prototypes
float avrg(int num1, int num2, int num3);
Function avrg takes three integers as parameters and returns a floating-
point value.
calculate();
The return type and the formal parameters are not written.
This function does not receive any parameter. It returns an integer
value.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 9
Defining a function
The syntax of a function definition is:
function header
{
statements;
function body
return an_expression;
The header is similar to
} prototype but no semicolon
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 10
5
Calling a function
The name of a function is called in order to
execute the function.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 11
Example:
if given function defintion as below:
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 12
6
void function cannot be used in an expression because it does not
return any value. It can only be used as a statement.
Example:
if given function defintion as below:
void greeting(void)
{
printf("Hello");
return;
}
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 13
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 14
7
Figure 4-5: void function with parameters
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 15
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 16
8
Function that calls itself is known as recursive function
Example:
int factorial(int n)
{
if (n>1) return n * factorial(n-1);
return 1;
}
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 17
return statement
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 18
9
The following function always returns 10.
n = n * n;
These two lines
return n; are ignored and
never executed
}
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 19
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 20
10
Example: Local vs. Global
#include<stdio.h>
void print_number(void);
p is declared outside of all
int p;
functions. So, it is a global
variable.
void main (void)
{
q is declared inside the
int q = 5; function main. So, it is a local
printf(q=%d, q); variable to the function.
p=10;
print_number();
}
p can be used anywhere
void print_number(void)
{
printf(%d,p);
Error! q can only be used in
q = q + 5; the function main, because it
} is a local variable.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 21
#include<stdio.h>
Global and local variables use
int p=10; the same name, p. They are
valid statements.
void main (void)
{
int p=5;
Which p will be used? The
printf(p=%d,p); global or local? When this
} happen, the local variable is
more dominant. So, the value
of local variable p=5, will be
used.
Output: p=5
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 22
11
Example: Local vs. Global
#include<stdio.h>
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 23
Scope
Scope determines the area of the program in which
an identifier is visible (ie. the identifier can only be used
in that area)
Examples:
Scope of a local variable : only in the function body where
it was declared.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 24
12
Scope
Scope that enclosed in { } is called a block.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 25
y;
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 26
13
Example: Scope of inner and outer block and function
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 27
Parameter Passing
To call a function, we write its name and give it some
information which are called parameters.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 28
14
Parameter Passing
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 29
Pass by Value
When a data is passed by value, a copy of the data
is created and placed in a local variable in the
called function.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 30
15
Figure 4-14: Pass by value
The content of
variable a is copied
The value of a is then placed into
unchanged. Passing parameter x
by value does not
cause side effect
x=5
x=5+3=8
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 31
Pass by Value
You have been introduced with the term input
parameter in Tutorial 4. This type of parameter is
passed using Pass by Value.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 32
16
Passing expression by value
a+3
=5+3
=8
fun ( a + 3 )
The expression is
evaluated first. Then the
result is copied and
placed into parameter x
x=8
8
x = 8 + 3 = 11
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 33
Pass by Reference
Passing by reference is a passing technique that passes the
address of a variable instead of its value.
Thats why it is also called Pass by Address
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 34
17
Pass by Reference
The formal parameter must be a pointer.
Example:
void fun(int *x) // x is a pointer variable
{
// function body
}
Example:
int n;
fun(&n); // &n means address of variable n
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 35
int a=5
The value of a was
changed in fun.
Passing by reference
causes side effect
a=a+3
a = 5 + 3 =8
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 36
18
Pointer
How the passing by reference works? To understand
this, lets look first at the concept of pointer.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 37
Pointer
Example:
int n=5; // n is a normal variable (or data variable)
int *p = &n; // p is pointer that holds the address of variable n
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 38
19
Pointer
There are two special operators for pointers: address operator
and indirection operator.
Indirection operator, *
Get the value of variable where its address is stored in the
pointer.
Example: *ptr
meaning: give me the value of variable where its address is stored
in the variable ptr
Pointer declaration also uses the asterisk (*) . Dont get
confused. Pointer declaration and indirection operator are
different.
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 39
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 40
20
Now, lets look
how passing by
reference works.
5 content
variable (address)
int a=5
The value of a was
changed in fun.
Passing by reference
causes side effect What is copied
is the address
of a, not its
content.
Tips:
x receives the &a
x is equal to &a
address of a, not 5
*x is equal to a
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 41
Modular Programming
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 42
21
Other reason: without functions
#include<stdio.h>
void main(void)
{ /*Farenheit-to-Celsius */
double degF,degC;
printf("Enter degrees F: ");
scanf("%f", °F);
/* F-to-C conversion: */
ratio = 5.0 / 9.0; /* We will unify this */
degC = (degF-32)*ratio; /* set of statements to */
/* make a new function. */
printf("%f degrees F are %f degrees C\n",degF,degC);
}
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 43
with functions
#include<stdio.h>
void main(void)
{ /*Farenheit-to-Celsius */
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 44
22
Modular Programming
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 45
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 46
23
Summary
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 47
Summary
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 48
24
Summary
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 49
Summary
return statement does two things:
returns a value to the calling function
returns the control of execution to the calling function
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 50
25
Summary
Parameter can be passed to a function in two ways:
pass by value
pass by reference
Chapter 4:Functions| SCP1103 Teknik Pengaturcaraan C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 51
26