functions : C++ for beginners

Functions are a block of code that is executed when called from the outside, and function are one of the important functions in the C++ language, which provides great use of memory, based on the abstraction and separation of codes to avoid overlapping functions.

 

1. Function Property

Function must have properties that clearly show their function. They are based on:

  • Declaration: The declaration is the method of defining the function’s title and properties, for example, and overloading and its nature in general. The function is often declared at the top of the Main in this way.
#include<iostream>
using namespace std;




void testFunction();


int main() {
    
    
    return 0;
}

 

  • Definition: It is the internal body of the function, which contains the code stack inside, which contains a function in a repository that is requested when needed and is like this below the Main.
#include<iostream>
using namespace std;




void testFunction();


int main() {
    
    
    return 0;
}

void testFunction()
{
    std::cout << "I am a function !";
}

 

  • Call Definition
#include<iostream>
using namespace std;




void testFunction();


int main() {
    
    testFunction();

    return 0;
}

void testFunction()
{
    std::cout << "I am a function !";
}

 

2. Example

Please, for a better understanding of the functions, copy the following code into your code editor.

#include<iostream>
using namespace std;




void testFunction();


int main() {
    
    testFunction();

    return 0;
}

void testFunction()
{
    std::cout << "I am a function !";
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *