Personal tools
Document Actions

lesson3a.cpp

by aullmj — last modified 2009-01-26 18:00

functions demo code

Click here to get the file

Size 1 kB - File type text/x-c++src

File contents

//companion code to lecture on functions
//Mark Aull, 2008

#include <iostream>
using namespace std;

int max(int lhs,int rhs=0);
char max(char lhs,char rhs);
double max(double lhs,double rhs);
//int max(double lhs,double rhs);

double abs(double num)
{
	if (num>=0)
		return num;
	return -num;
}

void main(void)
{
	cout << abs(5)<<' '<<abs(-5)<<endl;

	cout << max('A','z')<<endl;
	cout<<max(8,3)<<endl;
	cout<<max(-1)<<endl;
	cout<<max(600.3,double('a'))<<endl;
}

int max(int lhs,int rhs)
{
	cout << "int "<<lhs<<' '<<rhs<<' ';
	if (lhs>rhs)
		return lhs;
	else
		return rhs;
}

char max(char lhs,char rhs)
{
	cout << "char "<<lhs<<' '<<rhs<<' ';
	if (lhs>rhs)
		return lhs;
	else
		return rhs;
}

double max(double lhs,double rhs)
{//return (lhs>rhs)*lhs+(lhs<=rhs)*rhs; return (lhs>rhs)?lhs:rhs;
	cout << "double "<<lhs<<' '<<rhs<<' ';
	return (lhs>rhs)?lhs:rhs;
}
Related content