Using Functions — Computer Science

Jake Cutter
Technology Hits
Published in
2 min readJun 23, 2021

As stated in an earlier writing, a Function is a way, our computers characterize a way that a program reads information. It's like a file folder we may have in our offices. Each function is a different folder, but it may still be in the same drawer.

Photo by Christopher Gower on Unsplash

Functions are highly useful when it comes to simplifying your code down so that others can understand your work, it organizes different processes in the program, and it makes your program run better & smoother.

Example:

void myFunction(){

cout << “ I just got executed!”;

}

int main(){

myFunction();

return 0;

}

Explanation:

In the above program, myFunction and main are the two functions within the program. main() is where the program starts. This is the mass majority of what your attempting to execute. myFunction is the thing that is called by main() to execute myFunction.

Example 2:

void myFunction(string fname){

cout << fname << “Doe”;

}

int main(){

myFunction(“John”);

return 0;

}

Explanation:

The outside function, myFunction, gets called by main with a given parameter, fname. Void allows your function to not require a return output in C++.

Example 3:

void myFunction(int x, int y){

int temp_var = x* 5;

int temp_var_2 = y / 5;

cout << temp_var;

cout << temp_var_2;

}

int main(){

myFunction(4,5);

return 0;

}

Explanation:

The program takes the two parameters, 4 & 5, and plugs them into myFunction’s math operations.

Note:

First two coding examples taken from https://www.w3schools.com/cpp/exercise.asp?filename=exercise_classes1

This is a great place to practice your C++ skills. It gives you answers if you can’t figure it out with 58 different practice problems. Not affiliated, it's just a good site.

--

--