Dr. Khizar Hayat Associate Prof. of Computer Science

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.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Chapter 5 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions g g Data Flow g Scope local global part 4 part 4.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Variables and memory addresses
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
User-Defined Functions (cont’d) - Reference Parameters.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
User Defined Functions
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 7: User-Defined Functions II
Class and Objects UNIT II.
LESSON 2 Basic of C++.
CSC1201: Programming Language 2
Introduction to C++ Systems Programming.
A Lecture for the c++ Course
School of EECS, Peking University
Chapter 5 Functions.
Function There are two types of Function User Defined Function
Programming Fundamentals Lecture #7 Functions
Global & Local Identifiers
Addis Ababa Institute of Technology Yared Semu May 2012
Static Data Member and Functions
User-defined Functions
CSC1201: Programming Language 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)
Value returning Functions
Introduction to Programming - 3
FUNCTION CSC128.
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Programming – 4 Operators
The Function Prototype
Passing Arrays to functions
CSC1201: Programming Language 2
CSC1201: Programming Language 2
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Control Structures Selection
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Introduction to Algorithms and Programming COMP151
Dr. Khizar Hayat Associate Prof. of Computer Science
Programming Fundamental
Functions Chapter No. 5.
Scope Rules.
Presentation transcript:

Dr. Khizar Hayat Associate Prof. of Computer Science Functions 2 Dr. Khizar Hayat Associate Prof. of Computer Science

Function Call A function is called/invoked/executed inside some other function. A function call consists of the function name and the values (called arguments) to be assigned to its corresponding parameters in the same order as in the declarator. In most cases, if the function has a return type, you need to assign the function call to a variable of the returning data type in the calling function. Called function vs. calling function. If a function is called inside it’s own function, it is called recursion

Function Call #include<iostream> using namespace std; // function declaration int max(int num1, int num2); /*function definition may be here and even definition without declaration is enough, i.e. code on slide 10*/ int main () { // local variable declaration: int a = 100, b = 200, ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; } //function definition may be here, i.e. code on slide 10

Function Call In a function call, one can pass arguments to a function in two ways: Pass by Value Pass by reference Note: some authors do not differentiate between an argument and a parameter and use them interchangeably. In such a situation: Formal argument (or formal parameter) means what we have been referring the parameter, i.e. those in the actual definition of function. Actual argument (or actual parameter) means the argument, i.e. the variable names we pass in the function call.

Pass by value The actual values of the arguments is copied to their corresponding parameters in the the function. Any change to the parameters do not affect the arguments in the calling function All the function call examples done till now are pass-by-value examples.

Pass by value - Example #include<iostream> using namespace std; void repchar (char, int); int main () { repchar(‘-’,45); repchar(‘*’,25); return 0; } void repchar (char ch, int n) for (int j=0;j<n;j++) cout<<ch; cout<<endl;

Pass by value – Example (another version) #include<iostream> using namespace std; void repchar (char, int); int main () { char chin; int nin; cout<<“Enter a character”<<endl; cin>>chin; cout<<“Enter the number of times to repeat it”<<endl; cin>>nin; repchar(chin,nin); return 0; } void repchar (char ch, int n) for (int j=0;j<n;j++) cout<<ch; cout<<endl;

Pass by reference Rather than passing the value or variable name, one can pass a variable’s reference (address) to a function as argument. C++ provides a special type of variable called a reference variable which is an alias for another variable If a reference variable is passed as an argument to a function, any change made to it will actually be performed on the variable whose alias it is. Hence no copy of the variable is created. For the declaration of reference variables you must place the address operator ‘&’ (ampersand) before its name. Useful in cases when you want more than one outputs from a function – remember, you cannot return more than 1 value from a function

Pass by reference – Example #include<iostream> using namespace std; int squarebyvalue(int); void squarebyref (int &); int main () { int x=2,y,z=3; cout<<“Before square by value call, x is ”<<x<<endl; y=squarebyvalue(x); cout<<“After square by value call, x is ”<<x<<endl; cout<<“After square by value call, y is ”<<y<<endl; cout<<“Before square by reference call, z is ”<<z<<endl; squarebyref(z); cout<<“After square by reference call, z is ”<<z<<endl; return 0; } int squarebyvalue(int a) return a*a; void squarebyref (int &b) b*=b;

Library or Pre-defined Functions Please see the table on page 12 of notes for math.h or cmath.h library functions. The conio.h library getch() to get a character from the user getche() to get a character from the user but with echo clrscr() to clear the screen

Scope of a variable All the variables that we intend to use in a program must be declared first with a valid identifier (name) and data type. A variable may either be global or local in scope. A global variable is declared outside all the functions of the program while a local variable is declared inside a block of code (e.g. function) where needed. A global variable is visible (known) from all parts of the program that come after it’s declaration, whereas a local variable is limited to only its delimiters {}, i.e. ‘{‘ just before it’s declaration and it’s corresponding ‘}’.

Scope of a variable: Two examples

Scope of a variable - revisited #include<iostream> using namespace std; void fun(); int temp=0; int main () { int x=15; cout<<“The value of x is ”<<x<<endl; temp=temp+x; cout<<“The value of temp in main() ”<<temp<<endl; fun(); return 0; } void fun() int a=25;//local variable to fun() cout<<“The value of a is ”<<a<<endl; temp=temp+a; cout<<“The value of temp in fun() ”<<temp<<endl;

Scope of a variable - revisited #include<iostream> using namespace std; void fun(); int temp=0; int main () { int x=15; cout<<“The value of x is ”<<x<<endl; temp=temp+x; cout<<“The value of temp in main() ”<<temp<<endl; fun(); return 0; } void fun() int a=25;//local variable to fun() cout<<“The value of a is ”<<a<<endl; temp=temp+a; cout<<“The value of temp in fun() ”<<temp<<endl; //For output see the notes on page 15

The unary scope resolution operator (::) Used to access a global variable when a local variable of the same name is in the scope Cannot be used to access a local variable of the same name in the outer block

The unary scope resolution operator (::) #include<iostream> using namespace std; void fun(); int temp=100; int main () { int temp=45, a=10; cout<<“The value of local variable temp in main() ”<<temp<<endl; if (a>0) int number=10; cout<<“The value of temp in if statement inside main ”<<temp<<endl; cout<<“The value of global variable temp is ”<<::temp<<endl; } cout<<“The value of temp in main() ”<<temp<<endl; temp*=2; ::temp+=100; return 0; //For output see the notes on page 16

cout<<"c = "<<c<<endl; ::c*=2; #include<iostream.h> void fun(int); void fun(int *); //void fun2(int &) int c=2; int main() { int a=1; int c=-9; c=c/3; cout<<"c = "<<c<<endl; fun(c); fun(::c); if(a>0) int c=1; } c--; c=::c+3; cout<<" c = "<<::c<<endl; fun(&c); /*fun2(c); cout<<"c = "<<c<<endl;*/ return(0); void fun(int x) { int c; c=x*x*2; cout<<"c = "<<c<<endl; ::c*=2; cout<<"c = "<<::c<<endl; }   void fun(int *y) c=*y++; /*void fun2(int &y) y=c+1; }*/