C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » BeginnersRSS Feeds


Posted By: Ryan Evans     Category: C++ Programming     Views: 13302

This article explains about functions in c++.

Return From The Function

  • In C++ main returns type int to OS
  • The function prototyping is essential here
  • Prototyping requires each variable to be declared separately while variable names can be omitted
  • Function() is same as function(void) in c++
  • Void do_something(…) is possible to have 
  • In C++ use of void is redundant and unnecessary
  • Call by reference is by reference variables
  • Return by reference is also possible

Return by Reference Example

 int & max(int & x, int & y)
     if (x>y)
             return x;
             return y;

max(a,b) = -1 is then legal and assigns the value   -1 larger of a and b

Inline Request to Compiler

  • The context switching problem for small functions and inappropriateness of macro
  • The inline function definition
  • The inline request may not be accepted when
    • For function returning values  if loop, switch or goto exists
    • For functions not returning values, if a return statement exists
    • Functions either containing static variables 
    • Function is recursive 

Default Arguments

 float amt(float prin, int period, float rate = 1.5);
 value = amount ( 5000,5); // third arg missing 
 value = amount ( 5000,5,1.7); //no missing arg

A default argument is checked for type at the time of declaration and evaluated at the time of call
Only the trailing args can be default args

 int mul(int i, int j = 5,int k = 6) // valid
 int mul(int i = 5, int j) //invalid
 int mul(int i = 0, int j, int k =10) // invalid
 int mul(int i = 2, int j = 0, int k =10)// valid

Const Args and Function Overloading

 int strlen(const char *p);
 int length (const string & s);
 int add(int a, int b)
 int add(int a, int b, int c)
 double add(double x, double y)
 double add(int p, double q)
 double add(double p, int q)

Argument Matching

  • Here compiler tries to get an exact match 
  • Otherwise compiler uses the integral promotion like char to int, float to double
  • Otherwise it tries to use built in conversion
  • At the time of conversion, if more matches are found, compiler will generate an error
  • If all of the steps fail, compiler will try to use user defined conversions in combination with integral promotions and built in conversions

Didn't find what you were looking for? Find more on Function Or get search suggestion and latest updates.

Ryan Evans
Ryan Evans author of Function is from London, United Kingdom.
View All Articles

Related Articles and Code:

Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

Kenneth Vitalis from United States Comment on: Dec 28
programming C++ code to estimates the root of f(x)=tan?x-x-1 in the interval [0,0.5] using the regula-falsi method

TITUS DISHON from United States Comment on: Nov 10
It is my first day and i already like it a lot

View All Comments