Functions in C++

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What are Functions in C++?

A function is a collection of statements that accept inputs, do a given computation, and return output. The aim is to combine some regularly or repeatedly performed actions into a function so that we may call it instead of writing the same code for different inputs.​

Need of Functions

  • Functions reduce code redundancy by permitting the reuse of code blocks.
  • Instead of writing the same code repeatedly, define a function and call it as needed.
  • Easier maintenance: changes in functionality must be changed just within the function, not throughout.
  • Functions modularize code, enhancing readability and manageability.
  • Code that is organized into functions is easier to navigate and understand.
  • Functions provide abstraction, allowing you to use library functions without knowing how they work within.

Types of Functions

C++ has two Types of Functions:

  1. Library Functions
  2. User-Defined Functions

 

Library Functions

Library functions are pre-written functions included in the C++ Standard Library. They handle common activities and procedures like input/output, mathematical calculations, and string manipulation.

User-Defined Functions

User-defined functions are those created by the programmer to fulfill specific tasks. They help to organize code, reduce redundancy, and improve readability. A user-defined function is declared with a return type, name, and parameters, and it is defined with a block of code that performs the desired tasks.

Function Declaration

A function declaration, a function prototype, describes the function's name, return type, and parameters but does not include the function body. It informs the compiler what to expect from the function.

Function Call

A function call runs the function's code. It entails naming the function and supplying the required arguments to it. Functions are known by their names. If the function has no arguments, it can be called straight by its name. However, we can refer to functions with arguments in three different ways:​

  1. Call by Value.
  2. Call by Reference
  3. Call by Pointer

Call by Value

When calling by value, the original value is not changed. The function parameter stores the passed-in value in the stack memory address. Changing the value of a function parameter affects only the current function. This will not alter the value of variables within the caller method, such as main().

Call by Reference

We modify the original value by passing a reference (address). The address of the value is passed in the function, thus both the actual and formal parameters use the same address space. Value changes within the function are reflected both internally and externally.

Call by Pointers

The method of sending arguments to a function involves copying the address of an argument into the formal parameter. The function uses the address to get the call's real parameter. This signifies that changes to the parameter affect the supplied argument. Declare function parameters as pointer types. For example, swap() exchanges the values of two integer variables pointed to by its arguments.​

Function Definition

A function definition consists of the function declaration and the function body, which contains the code executed when the function is called.

Function Parameters

Functions can accept information as a parameter. They serve as variables within the function. They are specified within the parentheses following the function name. A comma can be used to separate several parameter values.

Function Prototype

A function prototype is a function declaration that has only the function's name, return type, and parameters. It informs the compiler about the function.

Return Statement

The return statement terminates the execution of a function and may optionally return a value to the function caller. If the function has a void return type, no value is returned. 

Function Overloading

Function overloading is a programming feature in which two or more functions have the same name but accept different numbers or types of inputs. This enables the same function name to be used for several purposes depending on the input parameters.

Default Argument

A default argument is a value specified in a function declaration that the compiler will assign if the caller function does not give a value for the argument. Passing a value overrides the default value. 

What is the Friend Function in C++?

  • C++ friend functions can access a class's private and protected data.
  • It is a non-member function declared within the class using the keyword "friend".
  • "Friend" appears only in the function declaration and not in the definition.
  • When invoking a friend function, neither the object's name nor the dot operator is required; it may accept objects as parameters.
  • A friend function can be declared in either the public, private, or protected section of the class.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this