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

Post on 20-Jan-2018

222 views 0 download

description

Functions

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

Introduction to Programming

Lecture 6

• Functions– Call by value– Call by reference

Today's Lecture Includes

Functions

Laboratory Stool

Constructing a laboratory Stool

Constructing a laboratory Stool

• Task: Making a stool– Subtask:

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

• 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 …

FunctionFunction name{

Body of the function}

Function

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

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

declarations and statements}

Function

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

main ( ){

:}

Declaration of Function

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

void main ( ) {

….}

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

…}

Definition of Function

int square ( int ) ;

Return Type of Function

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

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

Declaration

Definition

int x ;x = square ( i ) ;

Function Call

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 )

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

Call By Value

Calling function

Called function

Area of the Ring

Outer Circle

Inner Circle

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

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

Example: Function to calculate the area of a circle

main ( ){

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

}

Calculating ringArea without using Function

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.

• 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?

#include <iostream.h>

Header Files

int functionName ( int , int );

Prototype

Return valueAssignment List with data type

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

• #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

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

• Functions are also identifiers

• Labels are also identifiers

Scope of Identifiers

• 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

Examplevoid functionName ( ) {

{int i ; }

…..}

• 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

# include < iostream.h > int i ;

File Scope

Global variable

• 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

• 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

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

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

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

}

Example: Global Scope

void f ( void ){

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

}

Example: Global Scope

#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

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

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

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

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

Math.h

Today we studied

• Functions• Variable Scope