Programming Fundamental

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Computer Science 1620 Function Scope & Global Variables.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Functions g g Data Flow g Scope local global part 4 part 4.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
C++ Programming Lecture 12 Functions – Part IV
User-Defined Functions (cont’d) - Reference Parameters.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Review 1.
Introduction to Programming
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Programming Fundamental
Chapter 5 Functions.
FUNCTIONS IN C++.
Chapter 5 - Functions Outline 5.1 Introduction
CSC1201: Programming Language 2
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
Dr. Khizar Hayat Associate Prof. of Computer Science
Function “Inputs and Outputs”
Local, Global Variables, and Scope
The Function Prototype
CSC1201: Programming Language 2
Introduction to Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Introduction to Programming
Programming Fundamental
Programming Fundamental
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-12

Lecture outline Function: Call By Value Function Call By Reference Scope of Identifiers

Functions

Function Steps involve in functions: Declare a function Define a function Call a function

Calling function Called function

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

Calculating ringArea without using Function main ( ) { ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ; }

Example: Function to calculate the area of a ring (with Return Value)

Example: Function to calculate the area of a ring (Without Return a value)

Call By Value

Formal and Actual Parameters formal parameter — the identifier used in a method to stand for the value that is passed into the method by a caller. For example, amount is a formal parameter of process Deposit. actual parameter — the actual value that is passed into the method by a caller. For example, the 200 used when process Deposit is called is an actual parameter.

Call By Value #include <iostream.h > Void f ( int ) ; main ( ) { int i = 10 ; cout << “In main i = " << i ; f ( i ) ; cout << " Back in main, i = " << i ; } void f( int i ) { cout << "In function f , i = " << i ; i *= 2 ; cout << "In function f , i is now = “ << i ; }

int square( int ); main() { int a = 10; cout << a << " squared: " << square( a ) << endl; Getch(); } int square( int x ) return x * x;

Call Value

Call Value

Call By Value

Use of Function in IF Condition We can also use a function in the conditional statements like: if ( isEven ( number ) )

#include <iostream> #include <conio.h> using namespace std; int isEven(int); main ( ) { int number; cout << " Please enter the number: " ; cin>>number ; if ( isEven ( number ) ) cout << " The number entered is even " << endl; } else cout << " The number entered is odd " << endl; getch(); int isEven ( int number ) if ( 2 * ( number / 2 ) == number ) return 1; return 0;

Call By Reference

Reference &x Int j=2; Int &x=j‘; int &y; // Error: y must be initialized

Reference int main() { int x = 3; int &y = x; cout << "x = " << x << endl << "y = " << y << endl; y = 7; Getch(); }

CallByReference In ‘Call By Reference’, we are not passing the value itself but some form of reference or address. To understand this, you can think in terms of variables which are names of memory locations. We always access a variable by its name (which in fact is accessing a memory location), a variable name acts as an address of the memory location of the variable.

CallByReference we can’t pass the value. We have to pass the memory address of the value. This introduces a new mechanism which is achieved by using ‘&’ (ampersand) operator in C language. This ‘&’ operator is used to get the address of a variable.

Call by Reference Example void squareByReference( int & ); int main() { int z = 4; cout << "z = " << z << " before squareByReference" << endl; squareByReference( z ); cout << "z = " << z << " after squareByReference" << endl; getch(); } void squareByReference( int &numberRef ) numberRef *= numberRef;

Example

Scope of Identifiers 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

Example void functionName ( ) { int i ; } …..

Identifiers Important Points 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

File Scope # include < iostream.h > int i ; Global variable

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

Visibility of Identifiers 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

for ( int i = 0 ; i < 10 ; i++ ) Example: Block Scope 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: Global Scope using namespace std; int i ; void f () ; main() { i=10; cout<<"within main i ="<<i<<endl ; f() ; cout<<"after calling f() i ="<<i<<endl ; getch(); } void f () cout<< "Inside function f , i =" << i<<endl ; i=20 ;