Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
59 views

Functions: Function Introduction Function by Value Function by Reference Function Overloading

This document gives an understanding to the reader about the importance and usage of functions in programming.

Uploaded by

CBAKhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Functions: Function Introduction Function by Value Function by Reference Function Overloading

This document gives an understanding to the reader about the importance and usage of functions in programming.

Uploaded by

CBAKhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions

Function Introduction
Function by Value
Function by Reference
Function Overloading
Function Introduction

2 Bilal A. Khan - How to Program 12/07/21


Function Introduction
Why Functions?
• Segregation of tasks
• Provides the program a modular structure
• Can separate different functionalities of the
program
• Can produce different modules based on the
functionality
• Benefit
– Reusability of code
– Formation of code repository
– Easy to debug
3 Bilal A. Khan - How to Program 12/07/21
Function Introduction
Old Concept
Calculate the area of shapes )(main

wxh

b × h½

πr2

L2

4 Bilal A. Khan - How to Program 12/07/21


Function Introduction
Concept with Functions
)(main

wxh
Function (rectangle)
Function (circle)
Function (triangle) πr2
Function (square)

b × h½

L2

5 Bilal A. Khan - How to Program 12/07/21


Function Introduction
Syntax
main()
{
rectangle(a,b);
circle(a,b);
triangle(a,b);
square(a,b);
}

rectangle(int w, int h)
{
Rect = w * h
}
.
.
.
square(int x, int y)
{
// Code to calculate the square goes here
6 Bilal A. Khan - How to Program 12/07/21
}
Function by Value

7 Bilal A. Khan - How to Program 12/07/21


Functions by Value
int main()
{
int a=5,b=4;
rectangle(a,b);
return 0;
}
int rectangle(int w, int h)
{
int rect;
rectangle ( a5 ,, b4 );
rect = w * h;
return rect;
rectangle
rectangle (( w
5 , 4h ););
}

8 Bilal A. Khan - How to Program 12/07/21


Function by Reference

9 Bilal A. Khan - How to Program 12/07/21


Function by Reference
Understanding Address Space
Int a=5;
Float y=9.7;
Int b=4;
Memory Address
0 . 0
1 . .
2 . .
3
a 5 34,566
4
y 9.7 34,568
.
b 4 34,569
.
. .
.
. .
.
. .
655,359 655,359
10 655,360 Bilal A. Khan - How to Program 655,360 12/07/21
Function by Reference
Syntax
int main()
{
int a=5,b=4;
rectangle(a,b);
return 0;
}
int rectangle(int &w, int &h)
34,56 34,56
{
6 7
int rect;
rectangle
rectangle (( a5 ,, b4 ););
rect = w * h;
return rect;
rectangle
rectangle
( 34,566,
( w ,34,567
h ); );
}

11 Bilal A. Khan - How to Program 12/07/21


Function Overloading

12 Bilal A. Khan - How to Program 12/07/21


Function Overloading
int rectangle(int w, int h) int rectangle(int w, int h)
{
int rect;
rect = w * h;
return rect;
int main() }
{
int a=5,b=4;
float n=5.4,m=6.9
rectangle(a,b); float rectangle(float w, float h)
rectangle(n,m); {
return 0; float rect;
} rect = w * h;
return rect;
}
For int For float
rectangle ( a5 , b4 ); rectangle ( a5.4
, b, );
6.9 );
Result = 37.26
rectangle
rectangle (( w
5 , 4h );); rectangle ( w
5 .4,
, h 6.9
); );
13 Bilal A. Khan - How to Program 12/07/21
Thank You

14 Bilal A. Khan - How to Program 12/07/21

You might also like