Chapter 3

 

 

 


Text Box:  
Basic Function
 
#include <iostream.h>
 
int sum (int, int); /* This function takes in 2 integers, and returns an 
  integer */ 

/* Functions can take in and return characters, floats, doubles, integers, or nothing at all (next example) */
 
main()
{
	int batman, robin, catwoman;
	
	cout<<"Insert a number: ";
	cin>>batman;
 
	cout<<"Now insert another number: ";
	cin>>robin;
 
catwoman=sum(batman, robin); /* sum (batman, robin) is a function 
call; it calls the sum function, and tells it which integers 
it is supposed to add, in this case batman and robin 

   	The sum function will return an integer, and that value 
   	will be assigned to catwoman */
 
cout<<sum(batman, robin)<<endl; /* If you just want to print out
whatever the function returns, you don’t need an intermediate
integer, or float, or double, or character to do so (like
catwoman)*/

cout<<catwoman<<endl;
	
	return 0;
}
 
int sum (int b, int r)
{
	return b+r;	/* The sum function returns the sum of the two integers 
   it received */
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Function Calling Another Function
 
#include <iostream.h>
 
void greeting (void); /* The greeting function doesn’t take in or return 
anything; it is a function of type void */

float average (float, int, int); 
 
main()
{
	float spiderman, shocker=4.5;
	int vulture=3, carnage=7;
 
	spiderman=average(shocker, vulture, carnage); /* The average 
function is taking in a float and 2 integers */
 
	cout<<spiderman<<endl;
 
	return 0;
}
 
float average (float s, int v, int c)
{
	greeting();	/* The average function is calling another function, the 
greeting function.  Notice how the greeting function doesn’t 
take in or return anything */
	
	float smythe=(s+v+c)/3;
 
	return smythe;	/* The average of the three numbers is returned.  
An intermediate float is not necessary, but is used for 
clarification */
}
 
void greeting (void)
{
	cout<<"Hello! The average of the 3 numbers is "; /* All this 
function does is print out this statement */
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Random Number Generator
 
#include <iostream.h>
#include <time.h>
#include <iomanip>
 
main()
{
srand (time(0));	// This seeds the random number generator
/* Because of this, the random numbers in the program are 
different each time you run the program */
	
	int hulk;
 
	hulk=rand()%100;	// hulk will be a random number from 0-99
	cout<<hulk<<endl;
 
	hulk=1+rand()%5;	// hulk will be a random number from 1-5
	cout<<hulk<<endl;
 
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  	
	Casting
 
#include <iostream.h>
main()
{
	char first='A', wonder_woman='W';
 
	cout<<(int)first<<endl;
	cout<<(int)wonder_woman<<endl;
 
	int smiley=2;
 
	cout<<(char)smiley<<endl;
	
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Recursion 
 
#include <iostream.h>
#include <math.h>
 
int sum_of_cubes (int);
 
main()
{
	//Here is the loop method:
	int sum=0;
 
for (int i=4; i>=1; i--)
		sum+=pow(i,3);
 
 	cout<<sum<<endl;

//Here is the recursive (AND MORE EXCITING) way:
cout<<sum_of_cubes (4);
 
	cout<<endl;
	
	return 0;
}
 
int sum_of_cubes (int flash)
{
	int cube;
	cube=pow(flash,3);		
	
	if (flash>1)
		return (cube + sum_of_cubes(flash-1));
	
		else		
return 1;  /* If flash equals one, then return one because 
 				     one cubed is one */
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Global Variable vs. Local Variable
 
#include <iostream.h>
 
int superman=7;  /* superman can be accessed in the main and in all 
functions defined in this program */
 
int sum (int);
 
main()
{
	int bizarro=3; /* bizarro is a LOCAL VARIABLE, therefore it can only 
be accessed in the main (unless it is sent to a function) */
 
	cout<<sum (5)<<endl;
 
	cout<<bizarro+superman<<endl;	/* superman is a GLOBAL VARIABLE, so 
it can be accessed in the main, even though it is defined 
outside the main */
	
	return 0;
}
 
int sum (int titano)
{
	return superman+titano;	/* The sum function doesn't receive 
superman, but since superman is a GLOBAL VARIABLE, the sum 
function has access to it.  Pretty cool, huh? */
}