0% found this document useful (0 votes)
15 views8 pages

Review 02

The document provides an overview of C++ function declarations, definitions, and examples of various function types, including passing by value, reference, and address. It also covers function templates, default values, and dynamic memory allocation. Each example illustrates different aspects of function usage in C++, demonstrating how to define and call functions with various parameters and return types.

Uploaded by

alqisimohammad46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Review 02

The document provides an overview of C++ function declarations, definitions, and examples of various function types, including passing by value, reference, and address. It also covers function templates, default values, and dynamic memory allocation. Each example illustrates different aspects of function usage in C++, demonstrating how to define and call functions with various parameters and return types.

Uploaded by

alqisimohammad46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ function declaration

ReturnType FunctionName(P.L);
C++ function definition
returnType FunctionName(Parameter List){
// function body
return statement
}
int main(){
//direct function call statement is function signature

}
ReturnType: is the data type of the value the function
returns FunctionName: is the function name and must not
be C++ keyword Function body: is C++ statements
Return statement: return keyword
Parameter list: is a list of comma separated variable
declaration statements
Function body: is made of one or more C++ statement
Function signature: is the function name and the
parameter list, used to call the function

Example 1: a function that take nothing and return nothing

#include <iostream>
using namespace std;
#include<math.h>
#include<string.h>
void fun(){
cout<<”fun”; fun
}
int main(){
fun();}
1
Page

MEng. Samah A. Massadeh


Example 2:
A function that takes two integers (pass by value) and
output their sum, returns nothing.
Pass by value means make a copy – no change in the
original value

#include <iostream>
using namespace std;
#include<math.h>
#include<string.h>
//passing by value, and return by value (values are copied)
int fun(int x ,int y ){
return (x+y); 8
}
int main(){
// direct function call for a function that return something
int x=fun(1,2);
cout<<fun(3,5);
}

Example 3: a function that return a value Pass by value


means the values in the function call statement is called
are copied to the function parameter list variables

int fun( )
{ return 5; 10
}
int main(){
// direct function call for a function that return something
int x=fun();
cout<<x + fun();
}

__________________________________________________________________________
2
Page

MEng. Samah A. Massadeh


Example 4: a function that takes a reference – original
passed variable value will change

//function takes a reference recall (int &x=k)


void fun(int &x ){
x=9;
return; k=1
} new value of k=9
int main(){
int k=1;
cout<<"k="<<k<<endl;
fun(k); // passed by reference value will change
cout<<"new value of k="<<k<<endl;
}

Example 5: return by reference


using namespace std;
#include<math.h>
#include<string.h> int & fun(int * p, int &x) {
//return by reference int z = *p+x;
int & fun(int x ){ return z; // return *p+x; -----> error
}
x=9;
return x;
}
int main(){
int k=1;
cout<<fun(k)<<endl; 9
9
int z=fun(k);
cout<<z;

Example 6: Passing by address(pointer) original variable


value will change

void fun(int *p){


*p=8; return; 5
} new value of z=8
int main(){ int z=5;
3

cout<<z<<endl; fun(&z);
Page

cout<<"new value of z ="<<z;


MEng. Samah A. Massadeh
}

Example 7:
int & fun(int x){
x=9;
int *q=&x;
return *q; //returns a reference 5
new value of z=9
}
int main()
{ int z=5;
cout<<z<<endl; z=fun(z); //
cout<<"new value of z ="<<z;
}

updated Example 8: Passing by address and returning by address

//passing by address (pointers) original value will


change
int * fun(int *p){
for (int i=0;i<3;i++){
*p=i;
} 0
return p; //returns a pointer 1
} 2
int main(){
int a[3]={2,2,2};
int *q=fun(a); for(int i=0;i<3;i++)
cout<<i<<endl;
}

Example 9: passing a dynamic pointer (address)


void fun(int *p)
{ cout<<*p<<endl;
}
int main(){
int x=5; 5
4

int *z= new int(x); 5


Page

cout<<*z<<endl;
MEng. Samah A. Massadeh
fun(z); //passing a dynamic pointer
}

Example 10: returning a dynamic pointers


double * fun(){
double *g=new double(2); 00C349C0
cout<<g<<endl; 00C349C0
return g; 00C34A58
2
}
int main(){
cout<<fun()<<endl;
double *m=fun();
cout<<*m<<endl;
delete m;
}

Function default values

Values that are used if we do not pass an argument to the


function (value, reference, or address)

We must start from the left as shown in the function’s


declaration statement

int fun(int x,int y, int z=0);


int fun(int x, int y=3,int z=9);
int fun(int x=0,int y=2,int z=3);
//error
//int fun(x=3,int y)

Example 11: default values


using namespace std;
#include<math.h>
#include<string.h>
//if you did not pass a value to y and z then y=3 and z=4
void fun(int x,int y=3,int z=4){
cout<<x+y+z<<endl;
}
5
Page

int main(){
MEng. Samah A. Massadeh
fun(3);
fun(1,2); 10
fun(1,2,3); 7
} 6

Function templates

Function templates are special functions that can operate


with generic types.
This allows us to create a function template whose
functionality can be adapted to more than one type or
class without repeating the entire code for each type.
In C++ this can be achieved using template parameters. A
template parameter is a special kind of parameter that can
be used to pass a type as argument:
just like regular function parameters can be used to pass
values to a function, template parameters allow to pass
also types to a function. These function templates can use
these parameters as if they were any other regular type.
The format for declaring function templates with type
parameters is:

template <class identifier> function_declaration;


template <typename identifier> function_declaration;

Example 12: using function template you can pass any data
type to function

//declaring a function template


template <class T> T fun(T x){
cout<<x<<endl;
return x; 4
} 4
int main(){ 5.9
int x=4; 5.9
cout<<fun(x)<<endl; a
double z=5.9; a
6

cout<<fun(z)<<endl;
Page

char k='a';
MEng. Samah A. Massadeh
cout<<fun(k)<<endl;
}

Example 13:declaring a function template


template <class T,class U>
void fun(T x, U y)
7
{ cout<<x+y<<endl;
11.4
}
int main(){
int x=4,y=3;
fun(x,y);
double z=5.9, m=5.5;
fun(z,m);
}

Example 14: function templates


#include <iostream>
using namespace std;
#include<math.h>
#include<string.h>

//declaring a function template


template <class T,class U, class V> void fun(T x, U y,V c){
cout<<x<<endl;
cout<<y<<endl; 4
cout<<c<<endl; 3
} a
int main(){
int x=4;
double y=3;
char b='a';
fun(x,y,b);
}

****note
We cannot use default values with templates
A function template must declared before the function
7
Page

MEng. Samah A. Massadeh


int* p = new int [3];
p[0] = 0;
p[1] = 22;
p[2] = 33;
Example 15: two function templates
//*++p;
#include <iostream> cout << *p+1; //1
using namespace std; cout << ++ * p; //1
#include<math.h> cout << * ++ p; //22
#include<string.h>
#include <iostream>
using namespace std; 4
//declaring a function template 3
template<class T> a
T xxx(T x)
{ cout<<x<<endl; 4
return x;
} 4
template <class T,class U, class V> a
void fun(T x, U y,V c){ a
cout<<x<<endl; 3
cout<<y<<endl; 3
cout<<c<<endl;
}
int main(){ int* fun(int* p) {
int x=4; for (int i = 0;i < 3;i++) {
*p = i;
double y=3;
p++;
char b='a'; }
fun(x,y,b); return p; //returns a pointer
output:
20
cout<<" \n"; } 11
cout<<xxx(x)<<endl; int main() { 02
cout<<xxx(b)<<endl; int a[3] = { 2,2,2 };
cout<<xxx(y)<<endl; int* q = fun(a);
}
for (int i = 0;i < 3;i++)
{
int* fun(int x) { --q;
x = 9; cout << *q << a[i]<<endl;
return &x; output: ;
} 000000B42AAFFCB0 } fun(*p)
9 }
a array
int main() { p pointer
int k = 1; & reference
cout << fun(k) << endl;
int *z = fun(k); void fun(int* p) {
cout << *z;
return 0; * fun() &fun() cout << *p; }
------------------------- output :
} p x int main() 834
8

&x *p {
Page

int x = 8;
int* p = new int (3);
MEng. Samah A. Massadeh int a[] = {4, 5};
fun(&x);
fun(p);
fun(a);
}

You might also like