Functions in C++ - ProgrammerTech
Language:

Search

Functions in C++

  • Share this:
Functions in C++

Functions in C++

Function is a subprogram whose task is to perform a specific task when it is called (it is a group of commands grouped in one place and executed when we call it). Every C++ program has at least one main() program, and all simple programs can define additional functions.

 

What does the subprogram consist of?

The sub-program consists of three sections, which are as follows:

  1. The declaration of the program ends with a semicolon, but if the declaration is outside the void main() statement, then the subprogram is common to all programs available in the program, but if you write inside the body of void main() then the subprogram is exclusive to this instruction.
  2. Program request code and ends with a semicolon.
  3. The body of the program, which is similar to the way the main program is written, without a semicolon. The body of the function is written after the end of writing the body of the main program.

Declaring a function in C++

It is to write the name of the function's output, then the name of the function, and then write the parameters, ie the type of output.

void positive (int);

where void function output and positive function name and int parameter.

 

Building functions in C++

return_type function_name( parameter list ) {
    body of the function
}

whereas:

  1. return_type: Specifies the type that the function will return when it has finished.
  2. function_name: The name we give the function and call it when needed.
  3. Parameter list: Parameters (their setting is optional).
  4. body of the function: the body of the function and the commands that we put in the function.

As we have noticed that it consists of the function header and the body of the functions and then the program parts. The return_Type can be any data type ( int - double - bool - string etc..). In the event that the function does not return any value, the word void must be placed in the place of return_type.

Example 1

#include <iostream>
using namespace std;
int max(int num1, int num2);
int main () {
   int a = 100;
   int b = 200;
   int ret;
   ret = max(a, b);
   cout << "Max value is : " << ret << endl;
   return 0;
}
int max(int num1, int num2) {
   int result;
   if (num1 > num2)
      result = num1;
   else
      result = num2;
   return result;
}

We created two functions of type int, which are int num1 and int num2, then we entered the main program and created numerical variables, which are:

  • The variable a has a value of 100.
  • The variable b has a value of 200.
  • The variable ret and its value know the larger number of the previous two variables.

Then we created a print function and it prints the Max value is for us, then it prints the largest number, then we created the max function with the two variables.

And we created a variable result to store the result in it and we set conditions. If num1 is greater than num2 then print the value of num1 and otherwise print num2 and return the result to us.

When the code is run, the result will be 200.

200

Example 2

#include <iostream>
using namespace std;
int sum(int a, int b = 20) {
   int result;
   result = a + b;
   return (result);
}
int main () {
   int a = 100;
   int b = 200;
   int result;
   result = sum(a, b);
   cout << "Total value is :" << result << endl;
   result = sum(a);
   cout << "Total value is :" << result << endl;
   return 0;
}

We created two functions int which is sum and the two functions are int a and int b and the value of b is equal to 20 and we created another variable to store the result in which is result and the value of this variable is the sum of a+b and it returns the result.

Then in the main program, we assign a value to variable a, which is 100, and to variable b, which is 200.

When you run the program, the result will be:

Total value is: 300
Total value is: 120

 

Setting a default value for parameters in C++

C++ allows setting default values ​​for parameters, so when the function is called, the programmer has the option to pass values ​​in place of the parameters instead of being forced to. The default value for the parameter is Default Argument.

Example 3

#include <iostream>
using namespace std;
void printAdmin(string admin="Muhammad")
{
    cout << "Your admin is " << admin << endl;
}
int main()
{
    printAdmin();
    printAdmin("Kaissar");
    return 0;
}

We have defined a void function whose name is printAdmin, and when it is called, a value can be passed in the place of the parameter admin, and it is also possible not to pass a value because it basically has a value and it is Muhammad. And inside the main program we called the function without passing it in because it has a value and therefore its value will be the same as Muhammad. Then we called the function again and passed it a value which is Kaissar, so the parameter will be Kaissar. When you run the code, the result will be:

Your admin is Muhammad
Your admin is

If the function has more than one parameter in the program and you want to set a default value for only one of the existing parameters it has, you must put the parameters with default values ​​at the end.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com