Passing Functions

The image is property of carteretistem.wikispaces.com

Example 1

When passing functions it is really a matter of practice. NOTE: You must pass the functions within the main function. Lets start with a basic example.

#include <iostream>
using namespace std;

void example (int& a, int& b, int& c)
{
 cin>>a;
 cin>>b;
 cin>>c;
}

int main ()
{
 int x, y, z;
 example (x, y, z);
 cout << "x=" << x << ", y=" << y << ", z=" << z;
 return 0;
}

Conclusion 1

The output of this function proves that you rewrite the values of x, y, z to the values of a, b, c by writing code in another function.

Example 2

For passing functions it is important to call to the function by its name however don’t include its type. The best way to see the passing of functions is through examples. So LETS SEE THE CODE!! This is a vignere encryption/decription. Please view the project requirements online by  researching the vignere cipher via as well as the photo at the top of the page:

https://carteretistem.wikispaces.com/Team+SALL-Z+Cryptology

#include <string>

#include<iostream>

using namespace std;

string decrypt (string key, string CLE);//variables are also shown in decleration

int encrypt();//define your function names and types

string message, key, CLE;

int main(){

encrypt();//this is calling to the function encrypt and not bringing any variables from the

// main function.

cout<<”ENTER A MESSAGE WITH ONLY CAPITAL LETTERS:”<<endl;

getline (cin, message);

int messagelength = message.length();

cout<<message;

message[messagelength]=’\0’;

if(message.length()<81){

cout<<”ENTER A KEY IN CAPITAL LETTERS:”<<endl;

getline(cin, key);

if(key.length()<81){

cout<<”Your encrypted code is:”<<endl;

for(intx = 0; x<message.length();x++;){

CLE.push_back((((message[x]-‘A’) +(key[x % key.length()]- ‘A’))%26 +’A’);

cout<<CLE[x];}

const char* y = new char;

y = message.c_str();

const char*z = new char;

z = key.c_str();

decrypt (key, CLE);}}else{cout<<”bad input”;}//whew more passing functions and variables!!

return 0;}

String decrypt (string key, string CLE){

String decrypted;

Int val

For(intx = 0, x < CLE.length(); x++{

Val = (CLE[x] – ‘A’)-(key[x% key.length()]-‘A’);

If (val >=0)

Decrypted.push_back(val %26 + ‘A’);

Else

Decrypted.push_back(26+val+’A’);

}cout<<endl<<”Your decrypted code is”<<endl<<decrypted;}

Return decrypted;}//AHA finally it isn’t an int function we must return a string

Conclusion 2

Passing functions is a great way to break up the work. It is what programmers call “the sandbox” and is how coding is done in the workplace. You create functions individually (as main functions) then combine them in the end when they are processing the data correctly. If you have to pass variables you do so by including them in both function deceleration. This means that they will be included as if you declared the value that previous function found. To call to the function simply write the name of the function followed by a “;”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s