Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Function in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 65
    Comment on it

    For declaring and defining functions in C++ we have the syntax and we have to follow it.

    The general form for declaring a function in C++ is:

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

     

    • Return Type: The return type specifies what a function should return.

    • Function Name: This is the name by which we call and access it.

    • Parameters: Parameters are used to find values and results at runtime.

    • Function Body: It is the collection of the logic that the function performs.

     

    // function returning the max between two numbers
     
    int max(int num1, int num2) 
    {
       // local variable declaration
       int result;
     
       if (num1 > num2)
          result = num1;
       else
          result = num2;
     
       return result; 
    }

     

     

    Types of arguments passed into the function:

     

    Call Type Description

    Call by Value

    The actual value of the called argument is passed to the calling function

    Call By Pointer

    The address value of the called argument is passed to the calling function

    Call By Reference

    The reference   of the called argument is passed to the calling function

     

     

    #include <iostream>
    using namespace std;
     
    int sum(int a, int b=20)
    {
      int result;
    
      result = a + b;
      
      return (result);
    }
    
    int main ()
    {
       // local variable declaration:
       int a = 100;
       int b = 200;
       int result;
     
       // calling a function to add the values.
       result = sum(a, b);
       cout << "Total value is :" << result << endl;
    
       // calling a function again as follows.
       result = sum(a);
       cout << "Total value is :" << result << endl;
     
       return 0;
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: