Chapt. 4 Functions and File I - O
Chapt. 4 Functions and File I - O
1
Dept. of Computer & Electrical Eng.
TWENEBOAH-KODUAH S.
UENR
2
Chapter 4 –
Functions and
File I/O
“All progress is born of inquiry. Doubt is often better than overconfidence, for
it leads to inquiry, and inquiry leads to invention” – Hudson Maxim
UENR
3
Function Input and Output
UENR
05/07/2024 CENG 201 4
Software engineering
Software engineering is a discipline that is concerned with
the construction of robust and reliable computer programs.
Just as civil engineers use tried and tested methods for the
construction of buildings, software engineers use accepted
methods for analysing a problem to be solved, a blueprint
or plan for the design of the solution and a construction
method that minimizes the risk of error
One of the most important barriers to the development
of better computer software is the limited ability of human
beings to understand the programs that they write.
Functions in C/C++ is a way of dividing programs into
modules, sub-task, sub-system, etc
UENR
5
Predefined functions
UENR
6
Predefined functions
UENR
7
Concept of function
A function is a self-contained block of program statements that
performs a particular task
Imagine a program wherein a set of operations has to be repeated
often, though not continuously, n times or so.
If they had to be repeated continuously, loops could be used.
Instead of inserting the program statements for these
operations at so many places, write a separate program
segment and compile it separately.
As many times as it is needed, keep ‘calling’ the segment to get the
result.
The separate program segment or block that perform a specific task is
called a function
The program that calls the function is called the main program
UENR
8
Concept of function
Some definition: A function is a named, independent section of
C/C++ code that performs a specific task and optionally returns
a value to the calling program or/and receives values(s) from
the calling program.
Basically there are two categories of function:
◦ Predefined functions: available in C / C++ standard library such as
stdio.h, math.h, string.h etc.
◦ User-defined functions: functions that programmers create for
specialized tasks such as graphic and multimedia libraries,
implementation extensions or dependent etc.
A function is a group of statements that together perform a task
Functions allow a programmer to divide complex task among
several program modules Divide and conquer approach
UENR
9
Function mechanism
C/C++ program does not execute the statements in
a function until the function is called.
When it is called, the program can send
information to the function in the form of one or
more arguments although it is not a mandatory.
Argument is a program data needed by the function
to perform its task.
When the function finished processing, program
returns to the same location which called the
function.
UENR
10
Function prototype declaration
A function declaration tells the compiler about a
function's name, return type, and parameters
General form of declaring function
return_data_type function_name (para_dataType
paraVariable1, ...);
OR
return_data_type function_name
(para_dataType_list);
UENR
11
Parts of function declaration
function_name
◦ This is the name given to the function and it follows the same
naming rules as that for any valid variable in C.
return_data_type
◦ This specifies the type of data given back to the calling construct by
the function after it executes its specific task.
data_type_list
◦ This list specifies the data type of each of the variables, the values
of which are expected to be transmitted by the calling construct to
the function.
A function has a name that both identifies it.
The name is used to call it for execution in a program.
◦ The name of a function is global.
UENR
12
Example of prototyping function
float FtoC(float faren);
double power(double, int);
int isPrime(int x);
void printMessage(void);
void fibo_series(int y);
UENR
13
Function declaration and prototype
UENR
05/07/2024 CENG 201 14
Function definition
The collection of program statements in C/C++ that describes the
specific task done by the function is called a function definition.
Consists of the function header and a function body – block of
code enclosed in parentheses.
return Statement
◦ Syntax: return expression; or
return (expression)
◦ if not constant or value of a variable, expression must evaluate to a
value of the type specified in the function header for the return value.
The scope of variables declared within a function is limited to its
use in the function only.
◦ Any change made to these variables, internally in the function, is made
only to the local copies of the variables.
UENR
15
Function definition
Fucntion prototype int isPrime(int x);
The general format for function definition:
return_dataType function_name ( para_type para1, ...,
para_type paraN ) Function definition
{
body of the function
}
para_type just means the type for each argument or parameter --
for instance, an int, a float, or a char.
It's exactly the same thing as what you would put if you were
declaring a variable.
Example Function declaration
int mult ( int x, int y );
UENR
05/07/2024 CENG 201 16
Function definition
The general format of function definition:
return_dataType function_name ( para_type
para1, ..., para_type paraN )
{
body of the function
}
The function header in the definition is
return_data_type function name (data_type
variable1, data_type variable2,……)
UENR
05/07/2024 CENG 201 17
C/C++ FUNCTIONS
The Function header
The first line of every function definition is called function header. It has 3
components, as shown below,
1. Function return type - Specifies the data type that the function should
returns to the caller program. Can be any of C data types: char, float,
int, long, double, pointers etc. If there is no return value, specify a
return type of void. For example,
int calculate_yield(…) // returns an int type
float mark(…) // returns a float type
void calculate_interest(…) // returns nothing
UENR
C/C++ FUNCTIONS
For each argument that is passed to the function, the
parameter list must contain one entry, which specifies the
type and the name.
For example,
void myfunction(int x, float y, char z)
void yourfunction(float myfloat, char mychar)
int ourfunction(long size)
UENR
C FUNCTIONS
For the first function call:
UENR
Function definition – example
UENR
05/07/2024 CENG 201 22
Function call
A function call has the following syntax:
function_name(argument list)
UENR
05/07/2024 CENG 201 24
Function call: – call by value & call by
reference
Call by value:
◦ In call by value method, the value of the variable is passed to
the function as parameter.
◦ The value of the actual parameter can not be modified by
formal parameter.
◦ Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to
formal parameter.
Note:
◦ Actual parameter – This is the argument which is used in
function call.
◦ Formal parameter – This is the argument which is used in
function definition
UENR
05/07/2024 CENG 201 25
Function call: – call by value & call by
reference
OUTPUT
Call by value – Example:
◦ In call by value method, the value of the variable is passed to the
function as parameter.
values before swap m = 22
◦ The value of the actual parameter can not be modified by formal
and n = 44
parameter.
◦ Different Memory is allocated for both actual and formal
values after swap m = 44
parameters. Because, value of actual parameter is copied to formal
parameter.
and
Note:
n = 22
values after–calling
◦ Actual parameter swapwhich
This is the argument function:
is used in function
call.
M = 22parameter – This is the argument which is used in function
◦ Formal
definition
and n = 44
UENR
05/07/2024 CENG 201 26
Function call: – call by value & call by
reference
Call by reference:
◦ In call by reference method, the address of the
variable is passed to the function as parameter.
◦ The value of the actual parameter can be modified
by formal parameter.
◦ Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
◦ The use of pointers is required
UENR
05/07/2024 CENG 201 27
Function call: – call by value & call by
reference
Call by reference – Example:
◦ In call by reference method, the address of the variable is
passed to the function as parameter.
◦ The value of the actual parameter can be modified by formal
parameter.
◦ Same memory is used for both actual and formal parameters
since only address is used by both parameters.
◦ The use of pointers is required
UENR
05/07/2024 CENG 201 28
Animated function
UENR
05/07/2024 CENG 201 29
Example
Write a c programming using
user defined function to calculate
for Simple interest requesting for
P, R, and T values.
Make your program interactive
UENR
05/07/2024 CENG 201 30
Passing arrays as function arguments
1D array can be pass as double getAverage(int arr[],
function argument. Function int size)
parameters must be formally {
declared first
int i;
void myFunc (int *param)
double avg;
{…}
double sum;
void myFunc (int param[10])
{…}
for (i = 0; i < size; ++i)
{
void myFunc (int param[ ])
{…} sum += arr[i];
}
Example
avg = sum / size;
return avg;
}
UENR
return array from a function
Complete or entire array return is not allowed in C
One can however, return a pointer to an array by
specifying the array's name without an index
Returning 1D array from function declare a function a
pointer
Example: int * myFunc ( ) { … }
Local variable within a function must be declared as static
variable, WHY???
◦ A static variables have a property of preserving their value even
after they are out of their scope
It is not possible or not allowed to return the address of a
local variable within a function
UENR
Function
UENR
33
Return array from a function
Complete or entire array return is not allowed in C
Part 1
One can however, return a pointer to an array by
specifying the array's name without an index
Returning 1D array from function declare a
function a pointer
Example: int * myFunc ( ) { … }
Local variable within a function must be declared as
static variable, WHY???
It is not possible or not allowed to return the address
of a local variable within a function
UENR
Return array from a function
Part 2
#include <stdio.h>
#include <stdlib.h>
UENR
Uses of function in a program
Functions can be used in a program in various
ways:
◦ Function that perform operations on their parameters
and return a value.
◦ Function that manipulates information on their
parameters and returns a value that simply indicates
the success or failure of that manipulation.
◦ Function having no return type that is strictly
procedural
UENR
36