RECURSION AND COMMAND-LINE-ARGUMENTS
Recursion
Recursion is a programming technique where a function calls itself repeatedly until a specific base condition is met. A function that performs such self-calling behaviour is known as a recursive function, and each instance of the function calling itself is called a recursive call.
Recursion is a programming technique where a function calls itself repeatedly until a specific base condition is met. A function that performs such self-calling behaviour is known as a recursive function, and each instance of the function calling itself is called a recursive call.
Example:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int factorial(int num)
{
if (num == 1)
return 1;
int factres = num* factorial(num - 1);
return factres;
}
int main()
{
cout << "main - source1" << endl;
int fact = factorial(10);
cout << "factorial = " << fact << endl;
return 0;
}
Command Line Arguments:
Command line arguments in C++ allow a program to receive input directly when it is executed from the terminal or command prompt, rather than requiring user interaction during runtime. This is achieved by modifying the signature of the main function.
Example:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout << "source2 - main" << endl;
for (int i = 0; i < argc; i++)
{
cout << "argv[" << i << "] = " << argv[i] << endl;
}
return 0;
}