0% found this document useful (0 votes)
17 views

Functions

This document contains C++ code examples demonstrating different ways of passing arguments to functions including pass by value, pass by address, pass by reference, return by address, return by reference, and use of global, local, and static variables. It also shows examples of function pointers and how to call functions using function pointers.

Uploaded by

Akash Dhaker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Functions

This document contains C++ code examples demonstrating different ways of passing arguments to functions including pass by value, pass by address, pass by reference, return by address, return by reference, and use of global, local, and static variables. It also shows examples of function pointers and how to call functions using function pointers.

Uploaded by

Akash Dhaker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;

// ##1 Template Functions

// template <class T>


// T Max(T a, T b)
// {
// if (a > b)
// return a;
// else
// return b;
// }

// int main()
// {
// cout<<Max(10,5)<<endl;
// cout<<Max(12.5f,17.3f);
// return 0;
// }

// int add(int x, int y, int z = 0)


// {
// return x + y + z;
// }

// int main()
// {
// cout<<add(8, 10)<<endl; //third argument is set
to zero
// cout<<add(8, 10, 10)<<endl; //third argument is set to
10
// return 0;
// }

// //can be used as overloading function


// int max(int a=0,int b=0, int c=0)
// {
// return a>b && a>c ? a:b>c? b:c;
// }
// int main()
// {
// cout<<max()<<endl;
// cout<<max(10)<<endl;
// cout<<max(10,13)<<endl;
// cout<<max(10,13,15)<<endl;
// return 0;
// }

1
// ##2 PASS BY VALUE

// void swap(int a, int b)


// {
// int temp;
// temp = a;
// a = b;
// b = temp;
// }
// int main()
// { int x = 10;
// int y = 20;
// swap(x, y);
// cout<<x<<" "<<y;
// }

//##3 PASS BY ADDRESS

// void swap(int *a, int *b)


// {
// int temp;
// temp = *a;
// *a = *b;
// *b = temp;
// }
// int main()
// {
// int x = 10, y = 20;
// swap(x, y);
// cout<<x<<" "<<y;
// }

// ##4 PASS BY REFERENCE

// void swap(int &a, int &b)


// {
// int temp;
// temp = a;
// a = b;
// b = temp;
// }
// int main()
// { int x = 10;
// int y = 20;
// swap(x, y);

2
// cout<<x<<" "<<y;
// }

// ##5 RETURN BY ADDRESS

// int * fun(int size)


// {
// int *p = new int[size];
// for (int i = 0; i < size; i++)
// p[i] = i + 1;
// cout<<p<<endl;
// cout<<&p<<endl;
// return p;
// }
// int main()
// { int *ptr = fun(6);
// cout<<ptr<<endl;
// cout<<&ptr<<endl;
// }

// ##6 RETURN BY REFERENCE

// int& func(int &a)


// {
// cout<<a<<endl;
// return a;
// }
// int main()
// {
// int x = 10;
// // cout<< func(x);
// func(x) = 25;
// cout<<x<<endl;

// }

// ##7 GLOBAL AND LOCAL VARIABLES

// int g=6969;
// void fun()
// {
// int g = 10;
// {
// int g = 0;
// g++;
// cout<<g<<endl;

3
// }
// cout<<g<<endl;
// cout<<::g<<endl;
// }
// int main()
// {
// cout<<g<<endl;
// fun();
// cout<<g<<endl;
// }

// ##8 POINTERS TO A FUNCTION

// void display()
// {
// cout<<"Hello"<<endl;
// }
// int main()
// {
// void (*fptr)(); //declaration
// fptr = display; //initialisation
// (*fptr)(); //calling

// return 0;
// }

// int max(int x, int y)


// {
// return x > y ? x : y;
// }
// int min(int x, int y)
// {
// return x < y ? x : y;
// }
// int main()
// {
// int (*fp)(int, int);
// fp = max;
// cout<<(*fp)(10, 5)<<endl; //max function is
called

// fp = min;
// cout<<(*fp)(10, 5); //min function is called

// return 0;
// }

4
// int fun()

// {
// static int x=10;
// return ++x;
// }

// int main()
// {
// cout<<fun()+fun(); // 11 + 12 = 23
// }

You might also like