CHAPTER 6
Functions
BIC 10204 : ALGORITHM AND PROGRAMMING
Modular Programming
User defined functions
Function elements:
Outline • Function definition
• Function prototype
• Function call
Types of functions
Objectives
EXPLAIN FUNCTION CONCEPT WRITE C PROGRAM USING FUNCTION
CONCEPT.
Names are considered to be
identifiers (follow naming
SIMILARITIES identifiers rules)
BETWEEN
VARIABLE AND Has types (int, float, double, char,
FUNCTION etc.) associated to them
Must be declared prior to their
use
Is a program segment that
perform specific tasks, made up
of C statements.
LET’S TRY THIS
Write a program that can add, subtract, multiply and divide two numbers.
What do you think?
There are so many operations in main().
• We can separate the operations (addition,
subtraction, multiplication and division ) into several
functions.
• So, how to divide them ?
Separate the operations into functions
Modular programming in C is called Functions
• This technique is called divide and conquer
• Problem is divided into smaller pieces or modules
Modular
• Module can be divided into sub-modules
Programming
• Benefits:
software reusability –
make program
program structure is using existing functions
development more
readable as building block to
manageable
create new program
Functions elements
Prototype Definition Call
Separate the operations into functions
Prototype
Call
Definition
– Must be added to a C program
before the main() function, if call that
function before defining it
– It tells the compiler:
Function 1. What type of value 3. Order in which
2. Number and types
the function returns these parameters are
of parameters
Prototype
(function return type) expected.
– There is no need for a prototype if
the function is called after its
definition.
Function
Prototype
Example
void addition (int a, int b);
or
void addition (int, int);
– A function definition has
two principal components:
1. The first line
(including the parameter/ 2. The body of the
argument function
declarations)
Function
– The first line of a function
Definition definition contains:
1. Type of the value 3. (optionally) a set of
returned by the function parameters/ arguments,
2. The function name
(function separated by commas and
return type) enclosed in parentheses.
Function
Definition
A return value of type
void indicates a function
Format of a function definition does not return a value
return-value-type function-name (parameter-list)
{
declarations
statements
}
FUNCTION DEFINITION
– The arguments are called formal argument
• because they represent the names of data items that are transferred
into the function from the calling portion of the program.
Function – There are also known as parameters or formal parameters.
Definition The identifiers used as formal arguments are “local”
• because they are not recognized outside of the function.
• the names need not to be same as the names of the actual
arguments/parameters arguments in the calling portion of the program.
Compound statement / function body
defines the action to be taken by the function.
Function
Definition Can contain expression statements, other
compound statements, control statements,
and so on.
Compound statement/function
body
It should include one or more return
statements, in order to return a value to the
calling portion of the program.
With prototype
FUNCTION
DEFINITION
EXAMPLE
Without prototype
FUNCTION
DEFINITION
EXAMPLE
– A function can be accessed (i.e., called) by
specifying function name, followed by a list of arguments enclosed in
parentheses and separated by commas.
Function
– If the function call does not require any arguments
Calls empty pair of parentheses must follow the name of the function.
The arguments appearing in the function call are referred to as actual
arguments, in contrast to the formal arguments that appear in the
first line of the function definition.
FUNCTION
CALLS
In a normal function call
• there will be one actual argument for each formal
argument.
• may be expressed as constants, single variables,
or more complex expressions.
Function
If the function returns a value, the function
Calls access is written in a statement; e.g.,
• b = sum(a);
If the function does not return anything, the
function access appears by itself; e.g.,
• view(a,b,c);
A function can be In C, functions call
called by can be
• the main function • by value Function
• other functions • by reference
• itself (recursion) Calls
call by value
• Copy of variable passed to function
• If that variable is modified within the function, only the copy
has been modified, the actual variable is not
modified
Function
Call by reference
Calls
• Pass the address of a variable (i.e. a pointer) to a
function
• The variable pointed to can be modified within that
function
SCOPE OF
VARIABLE
SCOPE OF
VARIABLE
SCOPE OF
VARIABLE
EXAMPLE 1
SCOPE OF
VARIABLE
EXAMPLE 2
OUTPUT?
SCOPE OF
VARIABLE
EXAMPLE 3
SCOPE OF VARIABLE
EXAMPLE 4
Format
Types of Determine whether
function returns a value
Function Function type function_name
(parameter_list);
1. Does not receive and not
return value Determine whether
2. Receive but does not function receive a value
return any value
3. Receive and return value
Types of
Function
1.Function that does not
receive and not return
value
TYPES OF
FUNCTION
EXAMPLE
Types of
Function
2. Function that receive
but does not
return any value
TYPES OF
FUNCTION
EXAMPLE 1
TYPES OF
FUNCTION
EXAMPLE 2
TYPES OF
FUNCTION
EXAMPLE 3
Types of
Function
3. Function that
receives and returns
value
TYPES OF
FUNCTION
EXAMPLE
return
statement
Modify Example 3 (slide 42) from a program that
consist of functions that receive but does not return
any value to a program that consist of functions
that receive and
return value.
EXERCISE
Give the function prototype for each of the following:
1. Function smallest that takes three integers, x, y, z and
returns an integer.
2. Function instructions that does not receive any
arguments and does not return a value.
3. Function inToFloat that takes an integer argument,
number, and returns a floating-point result.
EXERCISE
EXERCISE
Symbol ‘&’
- An operator that means “address of”
Symbol ‘*’
1. the binary multiplication operator d=e*f;
2. The declarator specifier indicating that an address is
to be stored in a variable’s memory cell
Pointer in C void function1 (int a, int b, double r,
double s, int *c , double *t);
3. The unary operator indicating to go to the address
*c = a+b;
*t = r+s +(*c);
#include<stdio.h>
void function1 (int a, int b, double r ,double s, int *c, double *t);
void main (void)
{
int i=5, j=6, k;
double x=10.6, y=22.3, z;
printf (" i = %d \n\r j = %d \n\r x = %lf \n\r y = %lf \n\n",i,j,x,y);
function1 (i,j,x,y,&k,&z);
printf (" k = %d \n\r z = %lf \n\n", k, z);
}
void function1 (int a,int b,double r,double s,int *c,double *t)
{
*c = a+b;
*t = r+s +(*c);
printf (" *c = %d \n\r *t = %lf \n\n", *c, *t );
}
5
6 void main (void)
int i=5, j=6, k;
10.6
double x=10.6, y=22.3, z;
22.3 printf (" i = %d \n\r j = %d \n\r x = %lf \n\r y = %lf \n\n",i,j,x,y);
f u n c t i o n 1 ( i , j , x , y, & k , & z ) ;
printf (" k = %d \n\r z = %lf \n\n", k, z);
}
function1 (i,j,x,y,&k,&z);
void function1 (int a,int b,double r,double s, int *c, double *t)
5 5
6 6
10.6 11
22.3 10.6
FFF0 22.3
FFD8 43.9
#include<stdio.h>
void function1 (int a, int b, double r ,double s, int *c, double *t);
void main (void)
{
int i=5, j=6, k;
double x=10.6, y=22.3, z;
printf (" i = %d \n\r j = %d \n\r x = %lf \n\r y = %lf \n\n",i,j,x,y);
function1 (i,j,x,y,&k,&z);
printf (" k = %d \n\r z = %lf \n\n", k, z);
printf (" Address of k = %p\n\r Address of z = %p \n", &k, &z);
}
void function1 (int a,int b,double r,double s,int *c,double *t)
{
*c = a+b;
*t = r+s +(*c);
printf (" *c = %d \n\r *t = %lf \n\n", *c, *t );
printf (" Value contained in c = %p\n\r"" Value contained in t = %p\n\n", c, t);
}