Coders Packet

FUNCTIONS IN C++

By Prachi Jain

In this packet, we will see more about the concepts of function in C++ programming. A function is a piece of code, a part of a program that runs when it is called.

FUNCTIONS IN C++

A function is a piece of code that gets executed when it is called. Let us more about the concepts of function in C++ programming.

Functions are used to perform certain actions, and they are important for reusing code.

In functions, we have to define the code once, post which we can use the function several times.

We can pass data, known as parameters, into a function. The size of the program is potentially reduced by using functions at different places in the program.

 

ADVANTAGES OF USING A FUNCTION

  • Functions avoid repetition of codes.
  • Functions increase program readability.
  • Functions help in dividing a complex problem into simpler ones.
  • Functions reduce the chances of error.
  • Functions help in modifying a program becomes easier by using the function.

FUNCTION PROTOTYPING

Function prototyping describes the function interface to the compiler by giving details such as the number and types of arguments and the type of return values. With function prototyping, a template is always used when declaring and defining a function. When a function is called, the compiler uses the template to ensure that proper arguments are passed, and the return value is treated correctly.

          Function prototype:

                                   

return_type  function_name (argument-list);

 

The general form of a C++ function definition is as follows −

                                    

return_type function_name( parameter list ) 
{
body of the function
}

 

 

CALLING A FUNCTION

Declared functions are not executed immediately. They are "saved for later use", and will be executed later when they are called.

To call a function, write the function's name followed by two parentheses () and a semicolon ;

When a program calls a function, program control is transferred to the called function. A called function performs a defined task ie it executes its block and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store returned value. For example –

#include
using namespace std;
int modd(int n,int m)
{
  int r=n%m;
  return r;
}
int main()
{
  int a=100;
  int b=200;
  int r=modd(a,b);
  cout<<"The remainder is"<<r;
  return 0;
}

 

While executing the above code, it would produce the following result −

The remainder is:100

 

INLINE FUNCTIONS

An inline function is a function that is expanded in line when it is invoked or called. In this case, the compiler replaces the function call with the corresponding inline function code.

The function is defined as follows:

                       inline function-header { function_body }  

 

Download project

Reviews Report

Submitted by Prachi Jain (jainprachi954)

Download packets of source code on Coders Packet