Function in C++

Syntax:

function_type function_name(parameter_type parameter_name,parameter_type parameter_name){

function_body;

return function_type;

}

return will work for any function_type except void….void does not return any value but it returns..so we can use return keyword in void to exit but no value after return keyword  in void method / function

Normally we have to declare function protoype if we add the calling function after the main function

#include<iostream>
using namespace std;
int maxs(int num1,int num2);//function prototype

int main(){

int a=100;
int b=200;

int retur;

retur=maxs(a,b);

cout<<"Max value is "<<retur;


return 0;
}


int maxs(int num1,int num2){
int result;

if(num1>num2){
    result = num1;
}
else
    result=num2;


return result;
}

We can also use without function prototype when main function is after the calling function

#include<iostream>
using namespace std;

int maxs(int num1,int num2){
int result;

if(num1>num2){
    result = num1;
}
else
    result=num2;


return result;
}


int main(){

int a=100;
int b=200;

int retur;

retur=maxs(a,b);

cout<<"Max value is "<<retur;


return 0;
}

We can also use function when there is not equal parameter is passing like

#include<iostream>
using namespace std;

int sum(int a, int b=3){
int result;
result=a+b;
return result;
}

int main(){
int x=10;
int y=5;
int folafol;
folafol=sum(x,y);
cout<<"Total value is: "<<folafol<<endl;//10+5=15 will show

folafol=sum(x);
cout<<"Total value is:"<<folafol<<endl;//10+3=13 will show 

return 0;
}

One example below created by me:

#include<iostream>
using namespace std;
int zaki(int);
int diu(int);

int main()
{
    int result1,r,result2,p;
    r=10;
    p=200;
//passing r
    result1=zaki(r);
    cout<<"Result 1 : "<<result1<<endl;
//passing p
    result2=diu(p);
    cout<<"Result 2 : "<<result2<<endl;
    return 0;
}


int zaki(int res)
{
    int mins;

    if(res=10)
    {
        mins=100;
    }
    else
    {
        mins=20;
    }
    return mins;
}


int diu(int dhk)
{
    int maxs;

    if(dhk=2)
    {
        maxs=15;
    }
    else
    {
        maxs=19;
    }
    return maxs;
}

Another example

#include<iostream>
using namespace std;
void odd(int x);
void even(int x);

int main()
{
int i;

do{
    cout<<"Enter your number(0 to exit) : ";
    cin>>i;
    odd(i);
}while(i!=0);

    return 0;
}


void odd(int x){
if((x%2)!=0){
    cout<<"It is odd \n";
}else{
even(x);
}
}

void even(int x){
if((x%2)==0)
{
  cout<<"It is even \n";
}
else{
    odd(x);
}
}

Recursive function: Factolrial
courtesy:cplusplus.com

//factorial calculator
#include<iostream>
using namespace std;

long factorial(long a)
{
    if(a>1){
        return(a*factorial(a-1));
        }else{
       return 1;
        }

}

int main()
{
long number=9;

cout<<number<<"! = "<<factorial(number);

    return 0;
}

Another one:

#include<iostream>
using namespace std;

void printmessage()
{
cout<<"Hello world! :D";
}

int main()
{

printmessage();

return 0;
}

 

C++ Functioon Call By Value

 

void swap(int x,int y){

int tempo;

tempo=x; //save value of x in tempo variable

x=y; //put value of y into x;

y=tempo; //put the value of tempo actually the value of x into y

return;

}

C++ Function call by Reference

#include<iostream>
using namespace std;

void swaps(int &x,int &y){
int tempo;
tempo=x;
x=y;
y=tempo;

return;
}

int main()
{
int a=10;
int b=20;
cout<<"before swap a: "<<a<<endl;
cout<<"before swap b: "<<b<<endl;

swaps(a,b);//calling swap function

cout<<"after swap a: "<<a<<endl;
cout<<"after swap b: "<<b<<endl;

return 0;
}

C++ Function call by Pointer

#include<iostream>
using namespace std;

void swaps(int *x,int *y){
int tempo;
tempo=*x;
*x=*y;
*y=tempo;

return;
}

int main()
{
int a=10;
int b=20;
cout<<"before swap a: "<<a<<endl;
cout<<"before swap b: "<<b<<endl;

swaps(&a,&b);//calling swap function
/*
&a is addressing the variable a and indicating thje poiter to variable a
&b is addressing the variable b and indicating the pointer to variable b

*/
cout<<"after swap a: "<<a<<endl;
cout<<"after swap b: "<<b<<endl;

return 0;
}

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.