Sitemap

Software Development- 7

5 min readMay 4, 2024

--

Functions

F(x) = 2x +11; what is the function equation for f(5), find it 😊 Remember the questions we will write as code with the same logic for this basic function, especially since I started here because it was very useful for me to understand, (The relation that maps each element of set A to only one element of set B is called the function defined from A to B.)

The definition set is A and the value set is B and f is the name of the function that contains the mapping rule between the two sets.

f : A1 x A2 x … x An → B

Press enter or click to view image in full size

Now take the basics of the logic of let’s transform this function into code through an example, Functions are important building blocks in our code. We can create an in-depth function. Our main goal is to write the operation we want to do as a single function and then use it in our other code.

f(x) = 2x + 11

#include<iostream>

using namespace std;

int f(int x)

{

return 2*x + 11;

}

If we examine the above function line by line, the first line contains the definition of the function. According to this, the name of the function is f, the return type is int, and the parameter is int x. It can take more than one parameter. The return value of the function is the calculation of the operation to be performed. If we have grasped the logic, let’s see the writing rule(syntax rule) in step-by-step coding;

Function Writing Rules

1- Prototyping the Function

Prototype means the original example, the first example. It is the first and most primitive derivative of any concrete object, entity, or phenomenon that can serve as an example for the rest.

These meanings are important, we should proceed by knowing what is what from the very basics in our minds.

When writing cpp cod, our code is read from the bottom up, so when we write a function, we need to specify the type of the function as integer, string, bool, void, etc. Then the function must have a name, parentheses, and parameters to use.

return_type function_name(parameter_type1, parameter_type2, …);

Continuing with the example;

What is f(3,4) for f(x, y) = 3x + 4y?

#include <iostream>

using namespace std;

int f(int x, int y); //function prototype

int main() { //process executed results f(3,4)

cout<<f(3,4);

return 0;

}

int f(int x, int y) //the function itself return value part

{

return 3*x + 4*y;

}

The function prototype contains the name of the function, its return type, and the parameters it takes. This step tells the compiler what kind of value the function will return and what parameters it will take.

2- Defining the Function itself

What the function is written for, what it is needed for, and what it will return to us.

int f(int x, int y) //the function itself return value part

{

return 3*x + 4*y;

}

3- Function Calling, Processing

To use the function, we need to call the function we wrote in main() or another function. This call must contain the name of the function and the required parameters.

int main() { //process executed result f(3,4)

cout<<f(3,4);

return 0;

}

— — — — — — — — — — — — — — — — — — — — — — — — —

Example: Create an aggregate function!

#include <iostream>

using namespace std;

int add(int a, int b); // function prototype

int main() {

int sum;

sum = add(10, 20);

cout << “10 + 20 = ” << sum << endl;

return 0; }

int add(int a, int b) {

return (a + b);

}

A separate issue we need to consider;

Value Returning and Non-Value Returning Functions

(VOID AND NON-VOID that is the whole point ;)

The codes we wrote were functions that had a return value in the main program and returned the result. There are also functions called void, which means empty as a data type. These functions do not return a value after processing, they do not give a result. Instead of writing more than one line of code in the operations that we need to use repeatedly in other parts of the program, writing in a code written with void and writing it on a wireline makes the code more readable by grouping the operations.

As an example, let’s write a simple function that does not return a value.

Press enter or click to view image in full size

I want you to examine the following codes. What is requested here is to create a structure that controls the numbers entered as many as the number of pieces received from the user, this control is single, double, or prime, and each of them was written in the function.

#include<iostream>

using namespace std;

void arrayCreate(int numbers[],int size);

void arrayWrite(int numbers[],int size);

int dataInput(string info);

int evenNumber(int numbers[],int size);

int oddNumber(int numbers[],int size);

int primeNumber(int numbers[],int size);

int main()

{

int quantity = dataInput(“Quantity: “);

int numbers[quantity];

arrayCreate(numbers,quantity);

bool error = false;

Get Ceren Ağım’s stories in your inbox

Join Medium for free to get updates from this writer.

do

{

switch(dataInput(“1)Find Even Number\n2)Find Odd Number\n3)Find Prime Number\nSelect: “))

{

case 1:

cout<<”\nNumber of even numbers:: “<<evenNumber(numbers,quantity);

break;

case 2:

cout<<”\nNumber of odd numbers: “<<oddNumber(numbers,quantity);

break;

case 3:

cout<<”\nNumber of prime numbers: “<<primeNumber(numbers,quantity);

break;

default:

error = true;

}

}while(error);

return 0;

}

void arrayCreate(int numbers[],int size)

{

for(int i = 0; i < size ; i++)

{

cout<<i+1<<”.”;

numbers[i] = dataInput(“Number: “);

}

}

int oddNumber(int numbers[],int size)

{

int odd = 0;

for(int i = 0; i < size ; i++)

{

if(numbers[i] % 2 != 0)

odd++;

}

return odd;

}

int primeNumber(int numbers[],int size)

{

int number = 0;

for(int i = 0; i < size ; i++)

{

bool prime = true;

for(int j = 2; j < numbers[i]; j++)

{

if(numbers[i] % j == 0)

prime = false;

}

if(prime)

number++;

}

return number;

}

void arrayWrite(int numbers[],int size)

{

for(int i = 0; i < size ; i++)

{

cout<<numbers[i]<<endl;

}

}

int dataInput(string info)

{

int value;

cout<<info;

cin>>value;

return value;

}

int evenNumber(int numbers[],int size)

{

int even = 0;

for(int i = 0; i < size; i++)

{

if(numbers[i] % 2 == 0)

even++;

}

return even;

}

Output like;

Press enter or click to view image in full size

I used both the function and the arrays you mentioned in my last post. Please take the same codes and write them down, then you can write your questions. Once the series is finished you will be able to find all the examples on my GitHub account.

Work hard, don’t save your hope, and focus only on yourself, even if it is small, you are ahead of everyone who has not started, if not today, it will be tomorrow, if not tomorrow, it will be tomorrow.

Work hard, like, follow, and ask your questions in the comments :))

--

--

Ceren Ağım
Ceren Ağım

Written by Ceren Ağım

Coding and coding https://www.linkedin.com/in/ceren-959ba2155/ Ich fordere die Welt heraus... Behalten Sie die Straße im Auge...