C++ 17: Functions Introduction

 A Function is a special block of code which is defined to perform a specific task.

The Function are of two types:

  1. Function returning value.
  2. Function not returning value.


Function returning Value:
  • Such function return (i.e. return = send back) its result to the point where the function was called.
  • As these functions return a value so, we must use them in an expression that means we must USE the operators like =,+,-,<,>, etc. with such functions.
Example:

#include <iostream>
using namespace std;
main()
{
    double x = sqrt(81);//Function squrt returns value
    cout << "Value of x " << x << endl;

    double y = 100 + pow(23);
    cout << "Value of y " << y << endl;
   
    return 0;
}

Output:

Value of x 9
Value of y 108

Function not returning value
  • Such function performs their task but do not return(i.e. send back) the result.
  • As these functions not return any value so we cannot use them in an expression CANNOT use them with any operators like +,-,*,/,<,> etc.

Example : Function not returning value:

#include <iostream>
using namespace std;
main()
{
    int z;
    clrscr();
    gotoxy(3010);
    textcolor(RED + BLINK);//Function not returning value
    textbaground(WHITE);
    cprintf("C++ With Ayush ");
 
    return 0;
}

Output:

This works fine in old compilers like turbo c++



Defining a New Function/ User Defined Functions: 
  • If we want to write some block of code at different places in a program then instead to writing same code again and again we can write it only once and can call it where we want that code.
  • Function avoids repetition of code.
  • Once function is defined we can call it any number of times.
  • The function works on the principle of write once and use whenever you required.
  • If required the programmer can define new function using the following syntax.
Syntax:

        return_type function_name(data_type arg1, data_type arg2,....){
                        .........
                        .........
                        return(result);
        }

Where:

return_type: It decides the data_type of the value the function will return as its result. If the function is not retuning value then for such function the retrun_type must be displayed. In C and C++ if the return_type is not specified then by default it is int.
return : The return jump statement is used to send back the result of the result of the function to the point where function was called. The return statement terminates the execution of the function and returns control to calling the function. Execution resumes in the calling function at the point immediately after the function call.  
Syntax: 1 return 2. return (value)






Example:
#include <iostream>
using namespace std;
void strdot(){
    for (int i = 0i <= 30i++)
    {
        cout<<"*";
        cout<<".";
    }
    cout<<endl;
    
}
void line(int n){
    for (int i = 0i<= n; i++)
    {
        cout<<"*";
        cout<<".";
    }
    cout<<endl;

}

main()
{      
   cout<<"Ayush"<<endl;
   strdot();
    
   cout<<"Coding"<<endl;
   line(12); 
    
}

Output:

Ayush
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
Coding
*.*.*.*.*.*.*.*.*.*.*.*.*.









Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected