Transcript Slide 1

Lesson xx
•Why
use functions
•Program that needs a function
•Function header
•Function body
•Program rewritten using a function
1.
Functions allow you to repeat a body of code
without having to physically rewrite it many
times.
2.
Functions allow you to modularize code. This
means that you can break down your problems
into small blocks and each block can be put into a
function. This makes a program easier to work
with and debug.
3.
Functions allow many people to work on one
program at the same time. Each person writes a
different function.
Write a program that:
1.
Prints a row of 45 ‘*’s
2.
Prints the contents of the variable a
3.
Prints a row of 45 ‘*’s
4.
Prints the contents of the variable b
5.
Prints a row of 45 ‘*’s
6.
Prints the contents of the variable c
7.
Prints 2 rows of 45 ‘*’s
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a = 5, i;
float b = 17.345;
char c = 'q';
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
cout << " a = " << a;
/************************************************/
cout << endl ;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
cout << " b = " << b;
/************************************************/
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
cout << " c = " << c;
/************************************************/
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
system ("pause");
return 0;
}
(3)
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
(4)
cout << " a = " << a;
(1)
(2)
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
cout << " b = " << b;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
void
funName
{
Body of code
}
()
///function header
Function
name
void funName ( )
Return
type
Argument
list
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
void prtStar ( ); ///function prototype
int main()
{
int a = 5,;
float b = 17.345;
char c = 'q';
prtStar ( );
cout << " a = " << a;
/************************************************/
prtStar ();
cout << " b = " << b;
/************************************************/
prtStar ();
cout << " c = " << c;
/************************************************/
prtStar ();
prtStar ();
return 0;
}
void prtStar ( )
{
int i;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
}
void prtStar ( )
{
int i;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
}
void prtStar ( )
{
int i;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
}
void prtStar ( )
{
int i;
cout << endl;
for (i = 0; i < 45; i++)
{
cout << "*";
}
cout << endl;
}
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
void prtStar ( ); ///function prototype
int main()
{
int a = 5,;
float b = 17.345;
char c = 'q';
prtStar ( );
cout << " a = " << a;
/************************************************/
prtStar ();
cout << " b = " << b;
/************************************************/
prtStar ();
cout << " c = " << c;
/************************************************/
prtStar ();
prtStar ();
return 0;
}
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
void prtStar ( ); ///function prototype
int main()
{
int a = 5,;
float b = 17.345;
char c = 'q';
prtStar ( );
cout << " a = " << a;
/************************************************/
prtStar ();
cout << " b = " << b;
/************************************************/
prtStar ();
cout << " c = " << c;
/************************************************/
prtStar ();
prtStar ();
return 0;
}
How to write a function
How to call a function
function header
{
// body
}
1. function prototype / declaration
2. function call / invocation
void fun ( )
{
//code goes here
}
void fun ( ) ; ///function declaration
void main ( )
{
. ..
fun ( ) ;
///function call
...
return 0;
}
•Why
use functions
•Program that needs a function
•Function header
•Function body
•Program rewritten using a function