Learn c++ (functions) with nauman ur rehman

30
Lecture by: Nauman Ur Rehman Organized by: Shahid Jameel

Transcript of Learn c++ (functions) with nauman ur rehman

Lecture by: Nauman Ur Rehman

Organized by: Shahid Jameel

A function is a type of procedure or routine

which performs some operation.

Note: A function can have multiple input

(arguments) but only one output (return value)

Output = f(a,b)

A function is a group of statements that

together perform a task.

It is the part of a code that is called with

some input (arguments), on which it

performs a task, and then returns some

value.

Arguments

Body

(Statements)

Return

It makes program more manageable by

dividing it into small parts.

We can avoid the repetition of the program

using functions.

It also makes a program easy to debug (find

errors, if any)

There are many built-in functions included in

the pre-defined libraries; e.g. getline, read,

cin.

Prototype or Function Declaration

Function Call

Function Definition or Function Body

<return type> <function_name> (parameters);

1. int search (int value, int size);

2. int search (int , int);

Both 1 and 2 are the “prototypes” of the same

function.

Never forget the semicolon at last.

A function parameter is a variable declared in

the prototype or declaration of a function.

To call a function, we simply write the name of the function and provide it arguments:

1. y =search (7, x); //x=5 & int search (int value, int size);

2. cout<< search (7,x); //displays the returned value

3. search (7, x); // void search (int, int );

An argument is the value that is passed to the function in place of a parameter.

<return type> <function name> (parameters)

{ //curly braces

statement 1;statement 2;.return <int_value>;

}//no semicolon before or after curly braces

int search (int value, int size)

{ size = value * 2;

return size;}

#include<iostream>

using namespace std;

int func (int, int);

int main()

{

int x=0, y=1;

cout<<x<<endl<<y<<endl;

x=func(x,y);

cout<<x<<endl;

cout<<func(x,y)<<endl;

func(x,y);

return 0;

}

int func (int x, int y)

{

return x+y;

}

Declaration or prototype

Function Call 2

Definition or Body

Function Call 1

Function Call 3

1. Functions with no input and no return:

void search (void);

void main (void)

{

search ();

}

void search (void)

{

int x;

x = 4;

cout<<x<<endl; //displays 4 but no return

}

2. Functions with input but no return:

void search (int);

void main ()

{

search (4);

}

void search (int size)

{

size++;

cout<<size<<endl; //displays 5 but

} // no return

3. Functions with no input but return:

int function (void);

int main ()

{

cout<<search ()<<endl;

return 0;

}

int search (void)

{

int x = 4;

return x; //return 4

cout<<x<<endl; //do not execute

} // as return is already encountered

4. Functions with input and return:

int function (int);

void main ()

{

cout<<search (4)<<endl; //displays 5

}

int search (int size)

{

size++;

return size; //return 5

}

Pointer is a variable that is used to store address

of a variable.

Declaration:

<data type> * <pointer_name>;

int * a;

Initialization:

a = &b ; //address of b will be stored in a

// & sign is used to denote address

Dereferencing:

*a; //denote the value stored in variable

//where a is pointing

// ‘ * ‘ is called the dereference operator.

int var = 50; //address of var = 1001

int * ptr; // ptr declared (address=2047)

ptr = & var; //ptr initialized

cout<<var<<endl;

cout<<&var<<endl;

cout<<ptr<<endl;

cout<<*ptr<<endl;

cout<<&ptr<<endl;

* ptr = 5;

cout<< var<<endl;

cout<<* ptr<<endl;

cout<<*& ptr<<endl //*& cancel each other

Note:

address of var = 0012FF60

address of ptr = 0012FF54

int i=50; //address of i = 2000

int * ptr2 = &i; //direct initialization

int * * ptr1 = &ptr2; //address of ptr1 = 3000

cout<< i<<endl;

cout<<ptr2<<endl;

cout<<*ptr2<<endl;

cout<<ptr1<<endl;

cout<<*ptr1<<endl;

cout<<* * ptr1<<endl;

cout<<& ptr1<<endl;

int i=50; //address of i = 2000

int * ptr2 = &i; //direct initialization

int * * ptr1 = &ptr2; //address of ptr1 = 3000

cout<< i<<endl; // 50

cout<<ptr2<<endl; // 2000

cout<<*ptr2<<endl; // 50

cout<<ptr1<<endl; //3000

cout<<*ptr1<<endl; //2000

cout<<* * ptr1<<endl; //50

cout<<& ptr1<<endl; //4000

1. Pointer pointing to variable:

int * ptr;e.g.int * ptr = &a;a++;ptr = &b;

2. Pointer pointing to constant variable:

const int * ptr;e.g.const int * ptr = &a;

a++; // errorptr = &b;

3. Constant pointer pointing to variable:

int * const ptr;

e.g.

int * const ptr = &a;

a++;

ptr = &b; // error

4. Constant pointer pointing to constant variable:

const int * const ptr;

e.g

const int * const ptr = &a;

a++; // error

ptr = &b; // error

Array is itself, in a way, a pointer.

Array name is actually the address of the very

first element of the array.

Array uses the subscripts to access different

elements e.g. Arr[5]: gives access to 5th

element of an array named “Arr”.

A pointer to an array is initialized using array

name to get address of the array’s 1st element.

#include <iostream>

using namespace std;

int main ()

{ int arr[5]; //declared array named “arr” with 5 as a size

int * p; //declared a pointer p

p = arr; //address of 1st element of array stored in p

*p = 10; //puts arr[0] = 10

p++; //address of 2nd element of arry stored in p

*p = 20; //puts arr[1] = 20

p = &arr[2]; // address of 3rd element of array stored in p

*p = 30; //puts arr[2] = 30

p = arr + 3; // address of 4th element of array stored in p

*p = 40; // puts arr[3] = 40

p = arr; // address of 5th element of array stored in p

*(p+4) = 50; // puts arr[4] =50

for (int n=0; n<5; n++)

cout << arr[n] << ", ";

return 0; }

p

1. Pass-by-value

2. Pass-by-reference with reference

arguments

3. Pass-by-reference with pointer

arguments

void multiply (int x);

void main(){

int x=3;multiply (x);cout<<x; // displays 3

}

void multiply (int x){

x*=4; // x = x * 4 = 12cout<<x<<endl; // displays 12

}

void multiply (int &a);

void main(){

int x=3;multiply (x);cout<<x; //displays 12

}

void multiply (int &a){

a*=4;cout<<a<<endl; //displays 12

}

Note: a is put equivalent to x.address of a = address of x

Pass-by-reference with pointer arguments

void multiply (int *a);

void main()

{

int x=3;

multiply (&x);

cout<<x; //displays 12

}

void multiply (int *a)

{

*a*=4; //*a = *a * 4

cout<<*a<<endl; //displays 12

}

void multiply (int [], int );

void main(){

int x[3]={1,2,3};multiply (x, 3);cout<<x[1]<<endl; //displays 8

}

void multiply (int a[], int size){

for (int y=0; y<size; y++)a[y]*=4; //a[y] = a[y] * 4

cout<<a[1]<<endl; //displays 8}

Note: Arrays are passed by refrence.