Functions Types
In C and C++, functions can be categorised based on various characteristics such as how they are used, how they are declared, and what they do. Below are the main types of functions in both languages
Recursive Functions
A function that calls itself.
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Function Pointers
Both C and C++ support function pointers, allowing functions to be passed as arguments to other functions
int add(int a, int b) {
return a + b;
}
int (*funcPtr)(int, int);
funcPtr = add;
int sum = funcPtr(3, 4);
Inline Functions
Functions defined with the inline
keyword are expanded in line where they are called (if the compiler decides it is beneficial).
inline int add(int a, int b) {
return a + b;
}
For C language, GCC performs inline substitution as the part of optimisation.
#include
// Inline function in C
inline int foo()
{
return 2;
}
// Driver code
int main() {
int ret;
// inline function call
ret = foo();
printf("Output is: %d\n", ret);
return 0;
}
This will produce compiler error:
In function `main':
undefined reference to `foo'
Explanation:
Normally GCC’s file scope is “not extern linkage”. That means inline function is never ever provided to the linker which is causing linker error.
Solution:
To resolve this problem use “static” before inline.
static inline int foo()
{
return 2;
}
Using static keyword forces the compiler to consider this inline function in the linker, and hence the program compiles and run successfully.
Variable Argument Functions
Functions that can take a variable number of arguments (using stdarg.h
in C or variadic templates
in C++)
#include
int sum(int count, ...) {
va_list args;
va_start(args, count);
int total = 0;
for(int i = 0; i < count; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
In C++ (with variadic templates)
template
int sum(Args... args) {
return (args + ...);
}
Lambda Function
The syntax of a lambda function
[ capture_clause ] ( parameters ) -> return_type {
// Function body
}
capture_clause
: Specifies which variables from the surrounding scope are accessible inside the lambda.parameters
: The parameters that the lambda function takes, just like a regular function.return_type
: (Optional) The return type of the lambda function. If omitted, it is inferred automatically by the compiler.function_body
: The actual code that is executed when the lambda is called.int add = [](int a, int b) -> int { return a + b; };
Lambda expression can not contain nested lambda expression
Function Overloading (C++ only)
Use two functions or more with the same name but changing the footprint (Declaration of the function number of parameters, or even the type of the parameters)), so as long as the compiler can distinguish between the two functions it is a valid overload.
but typically the overload is to add more parameters.
But you can't overload on the return type only.
because the return is optional for the C++ functions, so the function may be not returning anything and you need to overload it, so
this is why it is not allowed to depend on an optional parameter to overload.
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
Compile time polymorphism
Warning:
once you have overloaded the function you will lose the implicit conversion ex. from float to int, you need to make another overloaded function or explicitly cast the call to be matched to the function