Professional Documents
Culture Documents
Functions
Return type
Input arguments
Functions
//demonstrates a simple function
#include <iostream>
using namespace std;
int cube(int x);
// function deration
int main(){
// tests the cube() function:
int n = 1;
Input Arguments
while (n != 0){
cin >> n;
cout
<< "\tcube(" << n << ") =
<< cube(n) << endl; // Calling a function
}
// end of while loop
system("PAUSE"); return 0;
Return type
}//end of main
int cube( int x ){ // function definition
return x*x*x;
// returns cube of x:
}
// { function body }
Functions
Calculating Permutations
// Calculates Permutation using Factorial
#include <iostream>
#include <stdlib.h>
using namespace std;
long fact(int n){ // returns n! = n*(n-1)*(n-2)*...*(2)(1)
if (n < 0) return 0;
int f = 1;
while (n > 1)
f *= n--;
// first f=f*n then n decrements
return f;
}
long perm(int n, int k){ // returns n Permutation k
if ( n<0 || k<0 || k>n ) return 0;
return fact(n)/fact(n-k);}
Calculating Permutations
int main(){ // tests the perm() function:
for (int i=-1 ; i < 6; i++){
for (int j=-1; j <=i+1 ; j++)
cout << i << "P" << j << "=" << perm(i,j) << " ";
cout << endl;
}
system("PAUSE"); return 0;
}//end of main
Default Arguments
#include <iostream>
#include <stdlib.h>
using namespace std;
//declaration with default arguments
void repchar(char='*', int=45);
int main(){
repchar();
//prints 45 asterisks
repchar('=');
//prints 45 equal signs
repchar('+', 30); //prints 30 plus signs
system("PAUSE"); return 0;
}
void repchar(char ch, int n){// displays line of characters
// defaults supplied if necessary
for(int j=0; j<n; j++)
// loops n times
cout << ch;
// prints ch
cout << endl;
}
Inline Function
Inline Function
// demonstrates inline functions
#include <iostream>
#include <stdlib.h>
using namespace std;
inline float lbstokg(float pounds){
// converts pounds to kilograms
return 0.453592 * pounds;
}
int main(){
float lbs;
cout << "\nEnter your weight in pounds: "; cin >> lbs;
cout << "Your weight in kilograms is " << lbstokg(lbs)
<< endl;
system("PAUSE");
return 0;
}
Function Overloading
// Calculates Factorial
#include <iostream>
#include <stdlib.h>
using namespace std;
int add(int a, int b){
cout << " Calling Int " << endl; return a+b;
}
float add(float a, float b){
cout << " Calling float " << endl; return a+b;
}
int main(){ // tests the factorial() function:
cout << add(5,3) << endl; // Calls int add( int, int)
cout << add(5.5F, 4.7F) << endl; // Calls float version
system("PAUSE"); return 0;
}//end of main
Recursion
Recursion
#include <iostream>
#include <stdlib.h>
using namespace std;
// calls itself to calculate factorials
unsigned long fct(unsigned long n){
static int I = 0; I++;
cout << "You Called Me " << I << " times" << endl;
if(n > 1)
return n * fct(n-1);
//self call
else
return 1;
}
int main(){
int n; cout << "Enter an integer: "; cin >> n;
cout << "Factorial of " << n << " is " << fct(n) << "\n";
system("PAUSE"); return 0;
}
Recursion
Returning by Reference
int& seta(){
return 3; // Error
}
int& seta(){
int x = 3;
return x; // error
}
Returning by Reference
#include <iostream>
#include <stdlib.h>
using namespace std;
int a = 10;
int& seta(){ // return type is int&
return a; //only global var can be returned by reference
}
int main(){
cout << "a = " << a << endl;
int b = 20;
b = seta() + b; // seta is used as a value
cout << "b = " << b <<endl;
seta() = b + 20; // seta is used as a variable
cout << "a = " << seta() <<endl;
system("PAUSE"); return 0;
}
Assignment #3
1.
2.
3.
4.
n!
Cr
r!(n r )!
Assignment #3
1.