0% found this document useful (0 votes)
60 views20 pages

Functions: Com - Cplusplis - WWW

The document discusses functions in C++ programming. It covers defining and calling functions, passing parameters to functions, return values, function overloading, and recursion. Examples of different types of functions are provided. Additional resources on functions in C++ are recommended for learning purposes.

Uploaded by

Elec Tronic
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)
60 views20 pages

Functions: Com - Cplusplis - WWW

The document discusses functions in C++ programming. It covers defining and calling functions, passing parameters to functions, return values, function overloading, and recursion. Examples of different types of functions are provided. Additional resources on functions in C++ are recommended for learning purposes.

Uploaded by

Elec Tronic
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/ 20

Compiler_x@yahoo.

com

Functions

C++

c++

www.cplusplis.com

2006
Compiler_x@ yahoo.com

Compiler_x@yahoo.com

3 .............................................................................................. functions

9 ...................................................................... void

10 ............................................................. scope of variable

Passed by value and reference

11 ..............................

13 ...................................... Default values of parameters

14 .......................................................... Overloaded functions

15 ............................................................. Recursive functions

) Declaring functions(prototype

17 .....................................

Compiler_x@yahoo.com

: functions

))(( main

-1

-2

-3

-4

..

...

"( .

)"

"(

)"

++

.++
:

)(Int main
{
.
.
.
"<<cout

;"
.
.
.
"<<cout

;"
.
.
"<<cout

;"
.
.
..
;return 0
}

Compiler_x@yahoo.com

4
)(int main
{
.
.

"<<cout

"

.
.

.
.
.

.
;return 0
}

"(

)"

"(

)"

"<<cout

"

)(int main
{
.
.

.
.

.
.
;return 0
}

Compiler_x@yahoo.com

-1

++

compiler

-2
++

-3

"

"

Type function_name (parameter1,par


a
me
t
e
r
2
,.
)
{
;statement 1
;statement 2
;statement 3
.
.
.
return value
}

.. void

long
float
int

: Type

: Function_name
.

++

: Parameter

)
.

: Statement

Compiler_x@yahoo.com

++

: Return
(.

value

:Value
type

># include <iostream.h

)int sum (int x,int y


{
;int z
;z=x+y
;return z
}

)(int main
{
;int c
;)c=sum(3,5
;cout<<" 3 + 5 = "<<c
;return 0
}

3
4
5

6
7
8
9
10

. iostram.h

sum

int

x y

int

.z

y
x

int

.c

.c

=3+5

)(. main

++

-1

Compiler_x@yahoo.com

int a

-2

compiling

sum

)sum(3,5

3,5

)sum (3,5

) int sum ( x , y

;) c=sum ( 3 , 5

. 5

x,y

)4

( return z ) 5

8 )(8=5+3

)(8

sum

sum

). c=sum (3,5

) int sum ( x , y
8
;) c=sum( 3 , 5

.8

10

Compiler_x@yahoo.com

The first result is 5


The second result is 5
The third result is 2
The fourth result is 6

// function example
>#include <iostream.h
)int subtraction (int a, int b
{
;int r
;r=a-b
;)return (r
}
)( int main
{
;int x=5, y=3, z
;)z = subtraction (7,2
;'cout << "The first result is " << z << '\n
;'cout << "The second result is " << subtraction (7,2) << '\n
;'cout << "The third result is " << subtraction (x,y) << '\n
;)z= 4 + subtraction (x,y
;'cout << "The fourth result is " << z << '\n
;return 0
}

a , b

subtraction

;)z = subtraction (7,2


;'cout << "The first result is " << z << '\n

;'cout << "The second result is " << subtraction (7,2) << '\n

cout

;'cout << "The third result is " << subtraction (x,y) << '\n

2
7

)( x,y

;)z= 4 + subtraction (x,y

;z= subtraction (x,y)+4

Z=4+2
Z=2+4

Compiler_x@yahoo.com

void

Ty
pef
unc
t
i
o
_n
(name
pa
r
a
me
t
e
r
1
,
par
a
me
t
e
r
2
,.
)
{
;statement 1
;statement 2
;statement 3
.
.
.
return value
}

...

type

++

++

(procedure

void

type

return

// void function example


>#include <iostream.h

!I'm a function
void

)( void printmessage
{
;"!cout << "I'm a function
}
) ( int main
{
;) ( printmessage
;return 0
}

printmessage

""I'm a function

. return

void

) (printmessage

void
)void printmessage(void

) (

printmessage

;)( printmessage

;Printmessage

Compiler_x@yahoo.com

10

)(void

I'm a function

// function example
>#include <iostream

the result is 8

)void addition (int a, int b


{
;cout<<"the result is"<<a+b
}
)( int main
{
;)addition (3,5
;return 0
}

void

scope of variable

local variable

global variable

Compiler_x@yahoo.com

11
>#include <iostream.h
//global variable

;int age

)]void print_info(char n[20


{
;'cout<<"name is : "<<n<<'\n
;cout<<"age = "<<age
}

//local variable

)(void main
{
;"char name[20]="my name
;cin>>age
;)print_info(name
}

global

age

print_info

print_info

local

name

print_info

name

name

Passed by value and reference

) pass by

(value

)(pass by reference

Compiler_x@yahoo.com

12

pass by reference

// passing parameters by reference


>#include <iostream.h

x=2, y=6, z=14

)void duplicate (int& a, int& b, int& c


{
;a*=2
;b*=2
;c*=2
}
)( int main
{
;int x=1, y=3, z=7
;)duplicate (x, y, z
;cout << "x=" << x << ", y=" << y << ", z=" << z
;return 0
}

duplicate

) & (

by reference

a,b,c

a b c

x y z

duplicate

a,b,c

a=1 b=3 c=7

&

x
duplicate

&

. x=1, y=3, z=7

) void duplicate ( int& a , int& b , int& c


x
y
z
;)

( duolicate

Compiler_x@yahoo.com

13
// more than one returning value
>#include <iostream.h

Previous=99, Next=101

)void prevnext (int x, int& prev, int& next


{
;prev = x-1
;next = x+1
}
)( int main
{
;int x=100, y, z
;)prevnext (x, y, z
;cout << "Previous=" << y << ", Next=" << z
;return 0
}

next

previous

& ,

& .

Default values of parameters

) = (

:
.

3
5

// default values in functions


>#include <iostream.h
)int divide (int a, int b=2
{
;int r
;r=a/b
;)return (r
}
)( int main
{
;)cout << divide (6
;cout << endl
;)cout << divide (20,4
;return 0
}

Compiler_x@yahoo.com

14

divide

)divide (6

divide

) . ( int b = 2

) . ( 6/2

)divide (20,4

( int b=2 ) b

5 . (20/4)=5

Overloaded functions

c++

10
2.5

// overloaded function
>#include <iostream.h
)int operate (int a, int b
{
;)return (a*b
}
)float operate (float a, float b
{
;)return (a/b
}
)( int main
{
;int x=5,y=2
;float n=5.0,m=2.0
;)cout << operate (x,y
;"cout << "\n
;)cout << operate (n,m
;"cout << "\n
;return 0
}

int

, operate

compiler

. float

float

. int

Compiler_x@yahoo.com

15

, int

operate

, float

operate

.(overloaded

Recursive functions

5!= 5 * 4 * 3 * 2 * 1

)!5!= 5 * (4

4!= 4 * 3 * 2 *1

(1!)*2

(2!)*3

(3!) *4

1=1

Please type a number: 5


5! = 120

// factorial calculator
>#include <iostream.h
)long factorial (long a
{
)if (a > 1
;))return (a * factorial (a-1
else
;)return (1
}
)( int main
{
;long number
;" cout << "Please type a number:
;cin >> number
;)cout << number << "! = " << factorial (number
;return 0
}

factorial

number

factorial

Compiler_x@yahoo.com

16

!3

!3

) !(4* 3

!1
)!( 2 * 1
!2
!2

)!(3*2

)factorial (5) = 5 * factorial (4

)factorial (4) = 4 * factorial(3

)factorial (3)= 3 * factorial(2

) factorial (2) = 2 * factorial (1

factorial (1) =1

// factorial calculator
>#include <iostream.h
)int power (int a,int b
{
)if (b<=1
;return a
else
;)))return (a * power(a,(b-1
}
)( int main
{
;int x=2,y=3
;)cout<<power(x,y
;return 0
}

( b

b=1

2^3

Compiler_x@yahoo.com

17
2 ^3 = 2 * 2 * 2 = 8

)power(2,3)=2 * power(2,2

)power(2,2)= 2 * power (2,1

power(2,1)=2

) (prototype

Declaring functions

// declaring functions prototypes


>#include <iostream.h
)void odd (int a
{
;"if ((a%2)!=0) cout << "Number is odd.\n
;)else even (a
}
)void even (int a
{
;"if ((a%2)==0) cout << "Number is even.\n
;)else odd (a
}
)( int main
{
;int i
{ do
;" )cout << "Type a number: (0 to exit
;cin >> i
;)odd (i
;)} while (i!=0
;return 0
}

Compiler_x@yahoo.com

18

) ( %

ood

) Number is odd.

even

odd

) ctrl+F9

debug

Run

...

even

.. even

100/100

..

odd

compiler

even

) even

even

)(Int main
{
;x=4
;int x
;cout<<x
return 0
}

..

void

even

..

++

) ; (

Compiler_x@yahoo.com

19
Type a number (0 to exit): 9
Number is odd.
Type a number (0 to exit): 6
Number is even.
Type a number (0 to exit): 1030
Number is even.
Type a number (0 to exit): 0
Number is even.

// declaring functions prototypes


>#include <iostream.h
;)void odd (int a
;)void even (int a
)( int main
{
;int i
{ do
;" )cout << "Type a number: (0 to exit
;cin >> i
;)odd (i
;)} while (i!=0
;return 0
}
)void odd (int a
{
;"if ((a%2)!=0) cout << "Number is odd.\n
;)else even (a
}
)void even (int a
{
;"if ((a%2)==0) cout << "Number is even.\n
;)else odd (a
}

even

viod

odd

odd even

void

*
:

;)Int add (int a, int b

;) Int add (int , int

*
.

Compiler_x@yahoo.com

20

2006
Compiler_x@ yahoo.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy