Activity 6 - Functions
Very well!
You’ve already done a lot of the program!
In this activity we will use functions to group sections of the code.
Functions
Functions allow us to divide the work of a program into smaller tasks separate from the main section, that is, from the main
function.
We can think of functions as a machine such that when we turn it on, it executes instructions to return a final result. You can have several machines that perform different processes, and we identify each one with a name, and the type of result it returns.
In this activity we will use functions with the void (empty) return type. These functions only process instructions, and do not return anything.
To declare (create) a function, we must use the following structure outside the main function:
void name_function(){
//Code here
}
Let’s analyze this code in more detail:
- The return type of the function indicate the type of object the function will return. In this case, we use
void
to specify that this function doesn’t return anything. - The name of this function is
name_function
. - The parentheses
()
are added after the name to identify that it is a function. - The braces
{}
determine the block of code that will execute when we use the function.
Calling a function
Since the functions we declare go outside the main()
function, when we press Run, the block of code we write in the functions will not run. Therefore, within the function main()
we must call the function. We must tell the program when we want that block of code to run. To call a function, we must use the following statement:
name_function();
We just have to write the name we gave to the function, followed by the parentheses, and finish with a semicolon ;
.
This will cause the instructions that are inside the function name_function()
to be executed.
Parameters of a function
When we declare a function, we can pass parameters to it. These are additional objects that the function can use to execute its instructions. The parameters go within the parentheses of the function. For example:
void name_function(int sum, int &number){
number += sum;
}
And in the main()
function we can call name_function
with some parameters:
int main(){
int x = 25;
int y = 5;
name_function(y, x);
}
Let’s understand in detail what we did:
- When declaring
name_function
, we write within the parentheses which variables we are going to pass into the function. Then, within the function body, we can use those variables. - In
main
, we had two variables already declared, which we then pass as parameters into the created functionname_function
. By doing this, the variablesum
equals the variabley
. - The variable
number
has this symbol&
in front. This means that if we modify the variable within the function, the variable that we pass to it in the main function will also be modified. That is, the variablex
will be modified by the function since we pass it by reference when writing that symbol in front. When we do not put this symbol, even if we modify its value in the function, we will not see that change inmain
.
That’s great! Now, let’s create 2 functions - one to encrypt the message, and the other to decrypt it. For this, we’re going to use code we already had, and we’ll program something new as well.
First, declare a function of return type
void
, just like encrypting, but to decrypt the name. Name this functiondecrypt()
.Copy the contents of the
encrypt()
function into the newdecrypt
function.In the
sum
variable, instead of adding, subtract the character of the name and character of the keyword, and add a lettera
(instead of subtracting) so that the final character is a letter of the alphabet and not some random character.Change the condition to turn in the alphabet. Instead of checking to see if the
sum
is greater than the characterz
, check if thesum
is less thana
; if so, add25
to the variable, instead of subtracting.In the
main
function, call the corresponding function in theif
condition depending on if we want to encrypt or decrypt a message.
When you’re done, you can press Run and try the following examples:
programmer
1
reina
witvczxipigrmg
2
reina
You should also try using your own secret words!
Remember that you can always go back to the previous activities to review anything!