Download presentation
Presentation is loading. Please wait.
Published byKristian Garry Bailey Modified over 9 years ago
1
Introduction to Programming Lecture 6
2
Functions – Call by value – Call by reference Today's Lecture Includes
3
Functions
4
Laboratory Stool
5
Constructing a laboratory Stool
6
Task: Making a stool – Subtask: Make a seat Make legs for the stool Assemble them
7
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 …
8
Function Function name { Body of the function }
9
Function Two types of functions: 1.Functions that return a value 2.Functions that do not return a value
10
return-value-type function-name( argument-list ) { declarations and statements } Function
11
return-value-type function-name( argument--type-list) ; main ( ) { : } Declaration of Function
12
Example int function-name ( int, int, double ) ; void main ( ) { …. }
13
int function-name ( int i, double j ) { … } Definition of Function
14
int square ( int ) ; Return Type of Function int square ( int i ) { return ( i * i ) ; } Declaration Definition
15
int x ; x = square ( i ) ; Function Call
16
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 ( X n )
17
include 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
18
Call By Value
19
Calling function Called function
20
Area of the Ring ____ Area of Inner Circle Area of Outer Circle = Area of the Ring
21
double circleArea ( double radius ) { return ( 3.1415926 * radius * radius ) ; } Example: Function to calculate the area of a circle
22
main ( ) { : ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ; } Calculating ringArea without using Function
23
Exercises 1.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.
24
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?
25
#include Header Files
26
int functionName ( int, int ); Prototype Return value Assignment List with data type
27
double pi = 3.1415926; Using Header Files It is better to define this value in a header file It is better to define this value in a header file Then simply by including the header file in the program this value is defined and it has a meaningful name Then simply by including the header file in the program this value is defined and it has a meaningful name
28
#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 * radius Circumference = 2 * pi * radius #define
29
Identifier is any name user creates in his/her program Functions are also identifiers Labels are also identifiers Scope of Identifiers
30
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
31
Example void functionName ( ) { int i ; } ….. }
32
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
33
# include int i ; File Scope Global variable
34
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
35
Global Scope Anything identified or declared outside of any function is visible to all functions in that file Function level scope Declaring variables inside a function can be used in the whole function Block level scope Variables or integers declared inside block are used inside block Visibility of Identifiers
36
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
37
#include int i ; void f ( void ) ; main ( ) { i = 10 ; cout<< “ within main i = “ << i ; f ( ) ; } Example: Global Scope
38
void f ( void ) { cout<< “ Inside function f, i =“ << i ; i = 20 ; } Example: Global Scope
39
#include 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
40
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
41
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
42
#include double sqrt ( double ); log 10, pow ( x y ), sin, cos, tan … Math.h
43
Today we studied Functions Variable Scope
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.