43
Introduction to Programming Lecture 6

Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Embed Size (px)

DESCRIPTION

Functions

Citation preview

Page 1: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Introduction to Programming

Lecture 6

Page 2: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Functions– Call by value– Call by reference

Today's Lecture Includes

Page 3: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Functions

Page 4: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Laboratory Stool

Page 5: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Constructing a laboratory Stool

Page 6: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Constructing a laboratory Stool

• Task: Making a stool– Subtask:

• Make a seat• Make legs for the stool• Assemble them

Page 7: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• What are functions?• How are they defined ?• How are they declared ?• What values are passed to functions ?• What values do functions return ?

What we will study today …

Page 8: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

FunctionFunction name{

Body of the function}

Page 9: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Function

Two types of functions: 1. Functions that return a value2. Functions that do not return a value

Page 10: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

return-value-type function-name( argument-list ){

declarations and statements}

Function

Page 11: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

return-value-type function-name( argument--type-list) ;

main ( ){

:}

Declaration of Function

Page 12: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Exampleint function-name ( int , int , double ) ;

void main ( ) {

….}

Page 13: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

int function-name ( int i , double j ){

…}

Definition of Function

Page 14: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

int square ( int ) ;

Return Type of Function

int square ( int i )int square ( int i ){{

return ( i * i ) ;return ( i * i ) ;}}

Declaration

Definition

Page 15: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

int x ;x = square ( i ) ;

Function Call

Page 16: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

double raiseToPow ( double x , int power ){

double result ;int i ;result = 1.0 ;for ( i = 1 ; i <= power ; i ++ ) // braces first{

result * = x ; // result = result *x}return ( result ) ;

}

Example: Function to calculate integer power ( Xn )

Page 17: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

include < iostream.h >void main ( ) {

double x ;int i ;cout << “ Please enter the number “ ;cin >> x ;cout << “ Please enter the integer power that you want this number raised to “ ;cin >> i ;cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ;

}

Code to Call the raisetopow Function

Page 18: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Call By Value

Page 19: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Calling function

Called function

Page 20: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Area of the Ring

Outer Circle

Inner Circle

____ Area of Inner CircleArea of Outer Circle = Area of the Ring

Page 21: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

double circleArea ( double radius ){ return ( 3.1415926 * radius * radius ) ;}

Example: Function to calculate the area of a circle

Page 22: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

main ( ){

:ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ;

}

Calculating ringArea without using Function

Page 23: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Exercises1. Modify the raise to power function so

that it can handle negative power of x, zero and positive power of x.

2. For the area of ring function put in error checking mechanism.

Page 24: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• We used functions for breaking complex problems into smaller pieces, which is a top-down structured approach.

• Each function should be a small module, self contained and it should solve a well defined problem.

• Variable names and function names should be self explanatory.• Always comment your code

What we have studied so far?

Page 25: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

#include <iostream.h>

Header Files

Page 26: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

int functionName ( int , int );

Prototype

Return valueAssignment List with data type

Page 27: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

double pi = 3.1415926;

Using Header Files

It is better to define this value in a header fileIt is better to define this value in a header file

Then simply by including the header file in the Then simply by including the header file in the program this value is defined and it has a program this value is defined and it has a meaningful namemeaningful name

Page 28: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• #define pi 3.1415926• Name can be used inside a program exactly

like a variable• It cannot be used as a variable

CircleArea = pi * radius * radiusCircleArea = pi * radius * radius

Circumference = 2 * pi * radiusCircumference = 2 * pi * radius

#define

Page 29: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Identifier is any name user creates in his/her program

• Functions are also identifiers

• Labels are also identifiers

Scope of Identifiers

Page 30: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Scope means visibility

• A variable declared inside a block has visibility within that block only

• Variables defined within the function has a scope that is function wide

Scope of Identifiers

Page 31: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Examplevoid functionName ( ) {

{int i ; }

…..}

Page 32: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Do not create variables with same name inside blocks, inside functions or inside bigger blocks

• Try to use separate variable names to avoid confusion

• Reuse of variables is valid

Identifiers Important Points

Page 33: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

# include < iostream.h > int i ;

File Scope

Global variable

Page 34: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Can be used anywhere in program• Can cause logical problems if same variable

name is used in local variable declarations

For good programming

• Try to minimize the use of global variables• Try to use local variables as far as possible

Global Variable

Page 35: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

• Global ScopeAnything identified or declared outside of any function is visible to all functions in that file

• Function level scopeDeclaring variables inside a function can be used in the whole function

• Block level scopeVariables or integers declared inside block are used inside block

Visibility of Identifiers

Page 36: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

for ( int i = 0 ; i < 10 ; i++ )

• It is block level scope declared in for loop

• When for is finished “ i ” no longer exists

Example: Block Scope

Page 37: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

#include < iostream.h >int i ; void f ( void ) ;main ( ){

i = 10 ;cout<< “ within main i = “ << i ;f ( ) ;

}

Example: Global Scope

Page 38: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

void f ( void ){

cout<< “ Inside function f , i =“ << i ;i = 20 ;

}

Example: Global Scope

Page 39: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

#include <iostream.h >int f ( int ) ;main ( ){

int i = 10 ;cout << “In main i = " << i ;f ( i ) ;cout << " Back in main, i = " << i ;

}

Example: Call by Value

s

Page 40: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

int f ( int i ){

cout << "In function f , i = " << i ;i *= 2 ;cout << "In function f , i is now = “ << i ;return i ;

}

Example: Call by Value

Page 41: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

double square ( double x ) {

return x * x ;}main ( ){

double number = 123.456 ;cout << “ The square of “ << number << “ is “<< square ( number ) ; cout << “ The current value of “ << number << “is “ << number ;

}

Example : Square of a Number

Page 42: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

#include < math.h >double sqrt ( double );

log10 , pow ( xy ) , sin , cos , tan …

Math.h

Page 43: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…

Today we studied

• Functions• Variable Scope