The main function contains a programs ________ logic which is the overall logic of the program.

A function is a set of statements that take inputs, do some specific computation,and produce output. The idea is to put some commonly or repeatedlydone tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call the function.
In simple terms, a function is a block of code that only runs when it is called.

Syntax:

The main function contains a programs ________ logic which is the overall logic of the program.

Syntax of Function

Example:

C++

#include <iostream>

using namespace std;

int max(int x, int y)

{

    if (x > y)

        return x;

    else

        return y;

}

int main()

{

    int a = 10, b = 20;

    int m = max(a, b);

    cout << "m is " << m;

    return 0;

}

Why Do We Need Functions?

  • Functions help us in reducing code redundancy. If functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call it everywhere. This also helps in maintenance as we have to change at one place if we make future changes to the functionality.
  • Functions make code modular. Consider a big file having many lines of code. It becomes really simple to read and use the code if the code is divided into functions.
  • Functions provide abstraction. For example, we can use library functions without worrying about their internal work.

Function Declaration

A function declaration tells the compiler about the number of parameters function takes data-types of parameters, and returns the type of function. Putting parameter names in the function declaration is optional in the function declaration, but it is necessary to put them in the definition. Below are an example of function declarations. (parameter names are not there in the below declarations) 

The main function contains a programs ________ logic which is the overall logic of the program.

Function Declaration

Example:

C++

int max(int, int);

int* swap(int*, int);

char* call(char b);

int fun(char, int);

Types of Functions

The main function contains a programs ________ logic which is the overall logic of the program.

Types of Function in C++

User Defined Function

User Defined functions are user/customer-defined blocks of code specially customized to reduce the complexity of big programs. They are also commonly known as “tailor-made functions” which are built only to satisfy the condition in which the user is facing issues meanwhile reducing the complexity of the whole program.

Library Function 

Library functions are also called “builtin Functions“. These functions are a part of a compiler package that is already defined and consists of a special function with special and different meanings. Builtin Function gives us an edge as we can directly use them without defining them whereas in the user-defined function we have to declare and define a function before using them. 
For Example: sqrt(), setw(), strcat(), etc.

Parameter Passing to Functions

The parameters passed to function are called actual parameters. For example, in the program below, 5 and 10 are actual parameters. 
The parameters received by the function are called formal parameters. For example, in the above program x and y are formal parameters. 

The main function contains a programs ________ logic which is the overall logic of the program.

Formal Parameter and Actual Parameter

There are two most popular ways to pass parameters:

  1. Pass by Value: In this parameter passing method, values of actual parameters are copied to the function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. 
     
  2. Pass by Reference: Both actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in the actual parameters of the caller.

Function Definition

Pass by reference is used where the value of x is not modified using the function fun().

C++

#include <iostream>

using namespace std;

void fun(int x)

{

    x = 30;

}

int main()

{

    int x = 20;

    fun(x);

    cout << "x = " << x;

    return 0;

}

Functions Using Pointers

The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, the value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&x)’, the address of x is passed so that x can be modified using its address. 

C++

#include <iostream>

using namespace std;

void fun(int* ptr) { *ptr = 30; }

int main()

{

    int x = 20;

    fun(&x);

    cout << "x = " << x;

    return 0;

}

Difference between call by value and call by reference in C++ 

Call by valueCall by reference
A copy of value is passed to the function An address of value is passed to the function
Changes made inside the function is not 
reflected on other functions
Changes made inside the function is reflected 
outside the function also
Actual and formal arguments will be created in 
different memory location
Actual and formal arguments will be created in 
same memory location.

Points to Remember About Functions in C++

1. Most C++ program has a function called main() that is called by the operating system when a user runs the program. 

2. Every function has a return type. If a function doesn’t return any value, then void is used as a return type. Moreover, if the return type of the function is void, we still can use the return statement in the body of the function definition by not specifying any constant, variable, etc. with it, by only mentioning the ‘return;’ statement which would symbolize the termination of the function as shown below: 

C++

void function name(int a)

{

    .......

        return;

}

3. To declare a function that can only be called without any parameter, we should use “void fun(void)“. As a side note, in C++, an empty list means a function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.

Main Function 

The main function is a special function. Every C++ program must contain a function named main. It serves as the entry point for the program. The computer will start running the code from the beginning of the main function. 

Types of Main Functions

1. Without parameters:

CPP

int main() { ... return 0; }

2. With parameters:

CPP

int main(int argc, char* const argv[]) { ... return 0; }

The reason for having the parameter option for the main function is to allow input from the command line. When you use the main function with parameters, it saves every group of characters (separated by a space) after the program name as elements in an array named argv
Since the main function has the return type of int, the programmer must always have a return statement in the code. The number that is returned is used to inform the calling program what the result of the program’s execution was. Returning 0 signals that there were no problems.

C++ Recursion

When function is called within the same function, it is known as recursion in C++. The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn’t perform any task after function call, is known as tail recursion. In tail recursion, we generally call the same function with return statement.
Syntax:

C++

recursionfunction()

{

    recursionfunction();

}

To know more see this article.

C++ Passing Array to Function

In C++, to reuse the array logic, we can create function. To pass array to function in C++,  we need to provide only array name.
 

functionname(arrayname); //passing array to function

 Example: Print minimum number

C++

#include <iostream>

using namespace std;

void printMin(int arr[5]);

int main()

{

    int ar[5] = { 30, 10, 20, 40, 50 };

    printMin(ar);

}

void printMin(int arr[5])

{

    int min = arr[0];

    for (int i = 0; i < 5; i++) {

        if (min > arr[i]) {

            min = arr[i];

        }

    }

    cout << "Minimum element is: " << min << "\n";

}

Output

Minimum element is: 10

C++ Overloading (Function)

If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload:

  • methods,
  • constructors and
  • indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

  • Function overloading
  • Operator overloading

C++ Function Overloading

Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don’t need to use different names for the same action.
Example: changing number of arguments of add() method
 

C++

#include <iostream>

using namespace std;

class Cal {

public:

    static int add(int a, int b) { return a + b; }

    static int add(int a, int b, int c)

    {

        return a + b + c;

    }

};

int main(void)

{

    Cal C;

    cout << C.add(10, 20) << endl;

    cout << C.add(12, 20, 23);

    return 0;

}

Example: when the type of the arguments vary.

C++

#include <iostream>

using namespace std;

int mul(int, int);

float mul(float, int);

int mul(int a, int b) { return a * b; }

float mul(double x, int y) { return x * y; }

int main()

{

    int r1 = mul(6, 7);

    float r2 = mul(0.2, 3);

    cout << "r1 is : " << r1 << endl;

    cout << "r2 is : " << r2 << endl;

    return 0;

}

Output

r1 is : 42
r2 is : 0.6

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.
When the compiler shows the ambiguity error, the compiler does not run the program.
Causes of Function Overloading:

  • Type Conversion.
  • Function with default arguments.
  • Function with pass by reference.

Type Conversion:-

C++

#include <iostream>

using namespace std;

void fun(int);

void fun(float);

void fun(int i) { cout << "Value of i is : " << i << endl; }

void fun(float j)

{

    cout << "Value of j is : " << j << endl;

}

int main()

{

    fun(12);

    fun(1.2);

    return 0;

}

The above example shows an error “call of overloaded ‘fun(double)’ is ambiguous“. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.
Function with Default Arguments:-

C++

#include <iostream>

using namespace std;

void fun(int);

void fun(int, int);

void fun(int i) { cout << "Value of i is : " << i << endl; }

void fun(int a, int b = 9)

{

    cout << "Value of a is : " << a << endl;

    cout << "Value of b is : " << b << endl;

}

int main()

{

    fun(12);

    return 0;

}

The above example shows an error “call of overloaded ‘fun(int)’ is ambiguous“. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).
Function with Pass By Reference:-

C++

#include <iostream>

using namespace std;

void fun(int);

void fun(int&);

int main()

{

    int a = 10;

    fun(a);

    return 0;

}

void fun(int x) { cout << "Value of x is : " << x << endl; }

void fun(int& b)

{

    cout << "Value of b is : " << b << endl;

}

The above example shows an error “call of overloaded ‘fun(int&)’ is ambiguous“. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).


What is the first line in a function called?

The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, the indentation is always four spaces (see Section 3.13). The body can contain any number of statements.

What is the term used by programmers to describe the part of a program in which a variable may be accessed?

A local variable's scope is the part of a program in which the variable may be accessed and is only visible to the statements in the variable's scope.

What is a group of statements that exists within a program for the purpose of performing?

A function is a group of statements that exist within a program for the purpose of performing a specific task.

When a function is called by its name during the execution of a program it is?

When a function is called by its name during the execution of a program, then it is. executed.