Functions in CPP

function_in_cpp


Functions in C++ are blocks of reusable code that perform specific tasks. They make code modular, organized, and easy to understand. This section will cover the basics of defining and calling functions, function parameters and return values, scope and lifetime of variables, function overloading and default arguments, inline functions, and recursion.


Defining and Calling Functions

In C++, a function is defined by specifying its return type, name, and parameters. Functions must be defined before they are called or declared in advance (prototyping).

Syntax:


Function_call

Example: Simple function to add two numbers.
function_call

Here, the function add takes two integers as parameters and returns their sum. The function is then called in main.


Function Parameters and Return Values

  • Parameters are variables defined in the function header and receive values (arguments) from the caller.
  • Return Values are the values a function sends back to the caller after execution. If a function does not need to return anything, its return type is void.

Example: Function with parameters and a return value.

function_Parameter
In this example, calculateArea takes the radius as a parameter and returns the area of a circle.


Scope and Lifetime of Variables

The scope of a variable refers to the region in the code where it is accessible, while the lifetime of a variable determines how long it exists in memory.

  • Local Variables: Declared inside a function and can only be used within that function. Their lifetime ends when the function exits.
  • Global Variables: Declared outside any function and accessible to all functions. They exist for the duration of the program.

Example: Local and global variables.

variable_scope
In this example, localVar is a local variable in main and cannot be accessed by printGlobal, while globalVar can be accessed by both main and printGlobal.



Function Overloading and Default Arguments

  • Function Overloading: Defining multiple functions with the same name but different parameters, allowing functions to handle different types or numbers of inputs.
  • Default Arguments: Allows parameters to have default values, which are used if no arguments are passed for those parameters.

Example: Function overloading and default arguments.

Function_overloading

In this code, multiply is overloaded to handle both int and double types. The add function has a default value of 5 for its second parameter, so calling add(10) assumes b = 5.



Inline Functions

Inline functions are small functions defined with the inline keyword to suggest to the compiler to replace function calls with the actual function code. This can reduce overhead for small functions, especially in repetitive calls.

Syntax:

Inline_Function

Example: Inline function to compute the square of a number.

inline_function
In this example, square(6) directly replaces the function call with 6 * 6, reducing the overhead of a function call.



Recursion

Recursion is a technique where a function calls itself. Each recursive call creates a new instance of the function on the call stack. Recursive functions are useful for problems that can be broken down into smaller subproblems, such as calculating factorials or finding Fibonacci numbers.

Example: Recursive function to find the factorial of a number.

Recursion

In this code, factorial(5) calls itself until it reaches the base case n <= 1, at which point it returns 1. Each recursive call multiplies n by the factorial of n - 1.

Post a Comment

Your comment will be visible after approval.
© TechTestLog. All rights reserved. Premium By Raushan Design
//My script