You are on page 1of 40

08/03/2017

PROGRAMMING CONSTRUCT - 3
07 Maret 2017

Institut Teknologi Del


Jl. Sisingamangaraja
Sitoluama, Laguboti 22381
Toba SUMUT
http://www.del.ac.id 1
08/03/2017

OUTLINE
Function
Pointer
File IO

2 ASD Problem Solving with C


08/03/2017

OBJECTIVES
Mahasiswa mampu menganalisa dan menjelaskan
perilaku dari program sederhana yang melibatkan
pemakaian contruct programming dasar.

3 ASD Problem Solving with C


FUNCTION

4 ASD Problem Solving with C


08/03/2017

WHAT IS FUNCTION ?
Function is a set of statement which is designed to
perform some specific task.
Function gives functionality to programmer to use
one module(task) for more times rather then write
same code again and again. By using Function we
can divide the large program to many separate
modules based on their functionality.
Every C program can be a thought of the
collection of functions.

5 ASD Problem Solving with C


08/03/2017

CLASSIFICATION OF FUNCTION
Library function
These are the in-built functions of C library.
These are already defined in header files.
e.g. printf( ); is a function which is used to print
output. It is defined instdio.hfile .

6 ASD Problem Solving with C


08/03/2017

CLASSIFICATION OF FUNCTION
User defined function
Programmer can create their own function in C to
perform specific task.
e.g.

7 ASD Problem Solving with C


08/03/2017

SOME CONCLUSION
Any C Program must contain at least one function.
If program contains only one function it must be
main( ).
There is no limit on the number of functions
present in a C program
Each function in a program is called in the
sequence specified by functions call in main( ).
After each function has done its thing, control
returns to main( ). When main( ) run out of
function calls program ends
C Program is a collection of one or more functions.
A function gets called when its name is followed by a
semicolon.

8 ASD Problem Solving with C


08/03/2017

USING FUNCTIONS
The main functions and other library functions
does need to be declared and defined but the main
functions body need to be defined by the
programmer.
There are 3 components associated with functions
:
1. The Declaration
2. The function definition
3. The Calling Statement

9 ASD Problem Solving with C


08/03/2017

COMPONENTS OF FUNCTIONS [1]


I ) Function Declaration : In this area we declare the
prototype and number of argument required for
function.
Syntax : return_type function_name (argument1,argument2.....)

eg : int div(int , int) { }

1 - int is a return type of function.


2 - div is a name for function.
3 - two int are arguments required to call the function.

10 ASD Problem Solving with C


08/03/2017

COMPONENTS OF FUNCTIONS [2]


II ) Function Definition : In this area we write the
actual code of function required to perform desired
task.

Syntax : return_type function_name (list of argument with data types)


{ code of function ; return; }

11 ASD Problem Solving with C


08/03/2017

COMPONENTS OF FUNCTIONS [3]


III ) Function Calling : In this part we call the function
and pass the argument for execution of function.

Syntax : function_name (arguments...)

12 ASD Problem Solving with C


08/03/2017

TYPES OF FUNCTIONS
Function can be divided into 4 categories:
A function with no arguments and no return value
A function with no arguments and a return value
A function with an argument or arguments and
returning no value
A function with arguments and returning a values

13 ASD Problem Solving with C


08/03/2017

FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUE

Called function does not have any arguments


Not able to get any value from the calling function
Not returning any value
There is no data transfer between the calling function and
called function.

14 ASD Problem Solving with C


08/03/2017

FUNCTION WITH NO ARGUMENTS AND A RETURN VALUE

Does not get any value from the calling function


Can give a return value to calling program

15 ASD Problem Solving with C


08/03/2017

FUNCTION WITH ARGUMENTS AND A RETURNING NO VALUE

A function has argument/s


A calling function can pass values to function called , but
calling function not receive any value
Data is transferred from calling function to the called
function but no data is transferred from the called function
to the calling function
Generally Output is printed in the Called function
A function that does not return any value cannot be used in
an expression it can be used only as independent
statement.

16 ASD Problem Solving with C


08/03/2017

FUNCTION WITH ARGUMENTS AND A RETURNING NO VALUE

17 ASD Problem Solving with C


08/03/2017

FUNCTION WITH ARGUMENTS AND RETURNING A VALUES

Argument are passed by calling function to the


called function
Called function return value to the calling function
Mostly used in programming because it can two
way communication
Data returned by the function can be used later in
our program for further calculation.

18 ASD Problem Solving with C


08/03/2017

FUNCTION WITH ARGUMENTS AND RETURNING A VALUES

Send 2 integer value x and y to add()

Function add the two values and send


back the result to the calling function

int is the return type of function

Return statement is a keyword and in


bracket we can give values which we want to return.

19 ASD Problem Solving with C


POINTER

20 ASD Problem Solving with C


08/03/2017

POINTER
Variable that declared occupies a memory
according to it size
It has address for the location so it can be referred
later by CPU for manipulation
The * and & Operator
Int x= 10
x Memory location name
10 Value at memory location
76858 Memory location address
We can use the address which also point the same
value.

21 ASD Problem Solving with C


08/03/2017

EXAMPLE

22 ASD Problem Solving with C


08/03/2017

EXAMPLE

* Symbols called the value at the address

23 ASD Problem Solving with C


08/03/2017

FILE I/O

24 ASD Problem Solving with C


08/03/2017

FILE MANAGEMENT
We have been using the functions such as printf
and scanf to read and write data. This works fine
as long as data is small.
But it has two major problems:
It becomes cumbersome and time consuming to handle
large volume of data through terminals.
The entire data is lost when either the program is
terminated or the computer is turned off .
So it is necessary to have more flexible approach
where data can be stored on the disks and read
whenever necessary, without destroying data.

25 ASD Problem Solving with C


08/03/2017

FILE MANAGEMENT
This concept is called files.
Files: A file is a place on the disk where a group of
related data is stored.
FILE is a structure declared in stdio.h . We have to
use file pointer, a pointer variable that points to a
structure FILE.

26 ASD Problem Solving with C


08/03/2017

C FILE MANAGEMENT
C supports a number o functions that have the
ability to perform basic file operations:
Creating a file
Opening a file
Reading data from a file
Writing data to a file
Closing a file
Renaming a file
Deleting a file

27 ASD Problem Solving with C


08/03/2017

FILES IN C
In C, each file is simply a sequential stream of bytes. C
imposes no structure on a file.
A file must first be opened properly before it can be
accessed for reading or writing. When a file is opened,
a stream is associated with the file.
Successfully opening a file returns a pointer to (i.e., the
address of) a file structure, which contains a file
descriptor and a file control block.

28 ASD Problem Solving with C


08/03/2017

FILES IN C
The statement:
FILE *fptr1, *fptr2 ;
declares that fptr1 and fptr2 are pointer variables of
type FILE. They will be assigned the address of a file
descriptor, that is, an area of memory that will be
associated with an input or output stream.

Whenever you are to read from or write to the file, you


must first open the file and assign the address of its file
descriptor (or structure) to the file pointer variable.

29 ASD Problem Solving with C


08/03/2017

OPENING FILES
The statement:
fptr1 = fopen ( "mydata", "r" ) ;
would open the file mydata for input (reading).

The statement:
fptr2 = fopen ("results", "w" ) ;
would open the file results for output (writing).

Once the files are open, they stay open until you close
them or end the program (which will close all files.)

30 ASD Problem Solving with C


08/03/2017

TESTING FOR SUCCESSFUL OPEN


If the file was not able to be opened, then the value
returned by the fopen routine is NULL.
For example, let's assume that the file mydata does not
exist. Then:
FILE *fptr1 ;
fptr1 = fopen ( "mydata", "r") ;
if (fptr1 == NULL)
{
printf ("File 'mydata' did not open.\n") ;
}

31 ASD Problem Solving with C


08/03/2017

READING FROM FILES


In the following segment of C language code:

int a, b ;
FILE *fptr1, *fptr2 ;
fptr1 = fopen ( "mydata", "r" ) ;
fscanf ( fptr1, "%d%d", &a, &b) ;

the fscanf function would read values from the file


"pointed" to by fptr1 and assign those values to a and b.

32 ASD Problem Solving with C


08/03/2017

END OF FILE
The end-of-file indicator informs the program when
there are no more data (no more bytes) to be
processed.
There are a number of ways to test for the end-of-file
condition. One is to use the feof function which returns
a true or false condition:
fscanf (fptr1, "%d", &var) ;
if ( feof (fptr1) )
{
printf ("End-of-file encountered.\n);
}

33 ASD Problem Solving with C


08/03/2017

END OF FILE
There are a number of ways to test for the end-of-file
condition. Another way is to use the value returned by
the fscanf function:
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == EOF )
{
printf ("End-of-file encountered.\n) ;
}

34 ASD Problem Solving with C


08/03/2017

WRITING TO FILES
Likewise in a similar way, in the following segment of C
language code:

int a = 5, b = 20 ;
FILE *fptr2 ;
fptr2 = fopen ( "results", "w" ) ;
fprintf ( fptr2, "%d %d\n", a, b ) ;

the fprintf functions would write the values stored in a


and b to the file "pointed" to by fptr2.

35 ASD Problem Solving with C


08/03/2017

CLOSING FILES
The statements:

fclose ( fptr1 ) ;
fclose ( fptr2 ) ;

will close the files and release the file descriptor space
and I/O buffer memory.

36 ASD Problem Solving with C


08/03/2017

READING AND WRITING FILES


#include <stdio.h>
int main ( )
{
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;

outfile = fopen ("testdata", "w") ;


fprintf (outfile, "%6.2f%2d%5.2f", a, b, c) ;
fclose (outfile) ;

37 ASD Problem Solving with C


08/03/2017

READING AND WRITING FILES


infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;

printf ("%6.2f,%2d,%5.2f\n", e, f, g) ;
}
12345678901234567890
****************************
13.72 5 6.68
13.72, 5, 6.68

38 ASD Problem Solving with C


08/03/2017

REFERENCE

39 ASD Problem Solving with C


TERIMA KASIH

Institut Teknologi Del


Jl. Sisingamangaraja
Sitoluama, Laguboti 22381
Toba SUMUT
http://www.del.ac.id

You might also like