Professional Documents
Culture Documents
CO1401 Week 2 Lecture
CO1401 Week 2 Lecture
Programming
Week 2
Functions & Files
A function example
void my_func()
{
cout << "hello world\n";
}
int main()
{
my_func();
// other things happen
my_func();
}
2 Programming
Program flow
Function
Invoke
function
3 Programming
void Function1( )
{
cout << "Inside function 1" << endl;
}
void Function2( )
{
cout << "Inside function 2" << endl;
}
int main( )
{
Function1();
cout << "inside main" << endl;
Function1();
cout << "inside main" << endl;
Function2();
cout << "inside main" << endl;
}
4 Programming
Example Sequence
Inside function 1
inside main
Inside function 1
inside main
Inside function 2
inside main
5 Programming
• Some functions int main()
just execute useful {
code. clear_screen();
}
7 Programming
Local Variables
#include <iostream>
using namespace std;
int main()
{
int counter1; // local to main()
// body of main
}
8 Programming
void foo(int number) // local to foo()
{
cout << number;
}
int main()
{
int value = 4;
foo( value )
}
9 Programming
Communication
10 Programming
Area of a square 1
float calcArea()
{
float width = 5.7;
float height = 4.3;
float area;
area = width * height;
return area;
}
int main()
{
cout << "box area is ";
float area = calcArea();
cout << area << "\n";
}
11 Programming
Parameters
12 Programming
Area of a square 2
int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
13 Programming
Parameters
14 Programming
"return"
15 Programming
Reminder of return types
16 Programming
Two types of parameters
17 Programming
Call by copy
18 Programming
Call by copy
19 Programming
void CallByCopy( int number )
{
number = 30; // number is changed,
} // but this is local
int main()
{
int a = 10;// 10 assigned to ‘a’
CallByCopy( a );
cout << a;// a still remains 10
}
20 Programming
Reference parameter
21 Programming
void ReferanceParameter( int& number )
{
number = 30; // original is changed,
}
int main()
{
int a = 10;// 10 assigned to ‘a’
ReferanceParameter( a );
cout << a;// a now 30
}
22 Programming
Call by reference
23 Programming
Function prototypes
24 Programming
Function prototypes
25 Programming
void ReferanceParameter(int& number);
int main()
{
int a = 10;// 10 assigned to ‘a’
ReferanceParameter( a );
cout << a;// a now 30
}
Files
27 Programming
File I/O
28 Programming
File input
30 Programming
Files and standard input
31 Programming
File input stream
ifstream infile("filename.txt");
if(!infile)
{
cout << "ERROR: ";
cout << "Can't open input file\n";
}
32 Programming
File input
33 Programming
End of File (EOF)
• With input files, you will need to detect when the end
of the file has been reached:
infile.eof();
34 Programming
while( !infile.eof() )
• Why the extra test for
{
end-of-file?
char ch;
infile >> ch; • eof() doesn’t return
if( !infile.eof() ) true until it read after
{ the end of the file. You
cout << ch; will get a spurious extra
} character at the end
} without the test.
• This is one of several
ways to deal with the
issue.
35 Programming
File with function
36 Programming
File with function
37 Programming
File with function
int main( )
{
ifstream infile;
OpenFile( infile );
ReadFile( infile);
}
38 Programming
Third Topic
Writing to a file
39 Programming
Output to a file
• Use "ofstream".
ofstream outfile( "filename" );
40 Programming
Output to a file
41 Programming
File Output
• You need to open the file and declare the file stream.
42 Programming
Check successful opening
• You should always check that the file has been opened
successfully. The file handle equates to a boolean:
43 Programming
#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
ofstream outfile( "output.txt" );
if (!outfile )
{
cout << "Cannot open output file" << endl;
exit(1);
}
outfile << "Written to the file" << endl;
outfile.close( );
}
44 Programming