Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

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)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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 (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
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 Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Fucntions in C++ Malik Jahan Khan
Introduction to Programming
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 7: User-Defined Functions II
Class and Objects UNIT II.
Computer Skills2 for Scientific Colleges
Predefined Functions Revisited
Variables A piece of memory set aside to store data
Chapter 5 Functions.
FUNCTIONS IN C++.
User-Defined Functions
Programming fundamentals 2 Chapter 2:Pointer
Programming fundamentals 2 Chapter 2:Function
User-defined Functions
Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name.
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.
User-defined Functions
Counting Loops.
FUNCTION CSC128.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 7: User-Defined Functions II
Local, Global Variables, and Scope
The Function Prototype
Fundamental Programming
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Repetition Statements (Loops) - 2
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
HNDIT11034 More Operators.
Programming fundamentals 2 Chapter 3:Pointer
Functions Chapter No. 5.
Presentation transcript:

Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters) { Action_ body ; }

The Function Definition Function are of 4 types: 1- A function that doesn’t return a value without argument -Return_DT is void -Action_body doesn’t contain return statement. Syntax: void function-name ( ) { local declarations // this is optional executable statements }

Example: function that doesn’t return a value without arguments void print_O ( ) { cout << “ ** “ << endl; cout << “ * * “ << endl; cout << “ ** “ << endl; }

The Function Definition 2- A function that doesn’t return a value with arguments -Return_DT is void -Action_body doesn’t contain return statement. Syntax: void function-name(argument_list) { local declarations // this is optional executable statements }

Example: function that doesn’t return a value with arguments void print_box (int n) { cout << “* * * * * * *\n” ; cout << “* “ << n << “ * \n” ; cout << “* * * * * * *\n” ; }

The Function Definition 3- A function that returns a result of any type without arguments -Return_DT is any type -Action_body contains return statement. Syntax: <any type> function-name( ) { local declarations // this is optional executable statements }

Example: function that return a value without arguments int sum () { int x=5, y=7 ; return x+y ; }

The Function Definition 4- A function that returns a result of any type with arguments -Return_DT is any type -Action_body contains return statement. Syntax: <any type> function-name(argument_list ) { local declarations // this is optional executable statements }

Example: function that return a value with arguments int Rect_area (int L, int W) { int a ; a = L * W ; return a ; }

Calling (invoking) Function: a) The call to a Function that does not return a value is given in the following syntax: F_name (Actual parameters); e.g. draw_circle ( ); e.g. sum(4,7); b) The call to a Function that returns a value is given as follows: - The name of the functio is given within the output statement e.g cout<<sum(x,y); - The name of the Function is given within the assignment statement e.g. result = sum (x, y );

The Call to a function program Figure Function draw_circle void main(){ ------- draw_circle ( ); ------ } void draw_circle ( ) { ------ } call return

Example #include <iostream.h> int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl; Function Definition Calling the function in an expression like cout<<, condition, assignment statements

Example #include <iostream.h> int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl;

Example #include <iostream.h> int Sum (int A, int B) { return (A+B); } void main() { int N1, N2, S; cout<<"\n Please Enter N1 and N2: "; cin>>N1>>N2; S = Sum(N1,N2); cout<<"\nSum= "<<S<<endl;

Example #include <iostream.h> bool Positive (int Num) { if (Num > 0) return true; else return false; } void main() { int Number; cout<<"\nEnter Number: "; cin>> Number; if (Positive(Number)) cout<<"\n the number is positive"; cout<<"\n the number is negative"; cout<<endl;

Example #include <iostream.h> float Area (int R) { return (3.14 * R * R ); } void main() { int Radius; cout<<"Enter the Redius: "; cin>>Radius; cout<<"\nCircle Area is: "<<Area(Radius); cout<<endl;

Example #include <iostream.h> long Power(int Base, int Exp) { int M=1; for(int i=1; i<=Exp; i++) M*=Base; return M; } void main() { int B, E; cout<<"\nEnter Base: "; cin>>B; cout<<"\nEnter Exponent: "; cin>>E; cout<<"\n Result= "<<Power(B,E); cout<<endl;

Example #include <iostream.h> long Fact (int Num) { int F = 1, i = Num; while (i>=1){ F *= i; i--; } return F; } void main() { int Number; cout<<"Enter an integer number: "; cin>>Number; cout<<endl<<Number<<"!= "<<Fact(Number); cout<<endl;

Void Returned Data Type #include <iostream.h> void Print(char Ch, int n) { for (int i=1; i<=n; i++) cout<<Ch; cout<<endl; } void main() { char Sym; int Number; cout<<"\nEnter the Symbol: "; cin>>Sym; cout<<"\nHow many times: "; cin>>Number; Print(Sym,Number); No Return Statement

Example #include <iostream.h> int Mul(int V1, int V2) { return V1 * V2; } void Result() { cout<<"\n5 x 9 = "<<Mul(5,9); cout<<"\n4 x 7 = "<<Mul(4,7); cout<<"\n6 x 4 = "<<Mul(6,4)<<endl; } void main() { Result() ; }

The Function Prototype Like other identifiers in C++, function must be declared before it can be referenced. To declare a function, we can insert a function prototype before the main function. The function prototype provides all information that the C++ compiler needs to know to translate calls to the function correctly. A function prototype tells the compiler the - data type of the function - the function name - information about the arguments that the function expects. Examples: void draw_circle ( ); int m ( ) ; void print_box (int) ; int Rect_area (int , int);

Function Prototype #include <iostream.h> int Mul(int, int); int Add(int, int); void Show(); void main() { Show(); } int Mul(int X, int Y) { return X*Y; } int Add(int X, int Y) { return X+Y; } void Show() { int A=10, B=20; cout<<Add(A,B)<<'\t'<<Mul(A,B)<<endl; } Function Prototype contains only data types But may contain identifiers.

Scope of Variables (1) Global variables: - Those variables that are declared before the main function. - These are visible from any point of the code, inside and outside any function. (2) Local variables: - These are declared inside a block or a function. - The scope of local variables is limited to the same nesting level in which they are declared.

Example of Local and Global Variables // File: global.cpp #include <iostream.h> int x = 7 ; // global variables int fun1 (int ); // function prototype void main ( ) { int z ; // local variable in main cout << “The global variable: “ << x ; z = fun1 ( 5 ) ; // calling add function cout << “ The result is “ << z << endl ; } int fun1 ( int a ) { int r ; // local variable in fun1 r = a * a * a ; return r ; }

Functions and Passing Parameters The mechanisms of passing parameters: (1) Call by value: - During the function call, the value of the argument is found and passed to the function. - Any modification inside the function does not affect the argument. (2) Call by reference: - During the function call, the address of the variable is passed to the function. - Any modification made on the parameter inside the function will have effect in the passed variable outside it.

Difference between Function Definitions for Call by Value and Call by Reference For call by value, we declare the arguments of the function as usual. Example: int func1 ( int , int ); // function prototype For call by reference, the type of the argument is followed by the symbol (&) to specify that the variable has to be passed by reference. void func2 ( int & , int & ); // function prototype

Call by value When calling, the value of actual parameter will be copied to the formal parameter. #include <iostream.h> void Increment(int); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int X) { ++X; }

Call By reference When calling, the reference formal parameter will be an alternative name to the actual parameter. #include <iostream.h> void Increment(int&); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }

Call By reference When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }

Example 5: Call by Value // File: calls1.cpp #include <iostream.h> // function prototype: the arguments to be passed by value int add (int , int ); void main ( ) { int x, y , z ; // local variables in main cout << “ Enter two integers: “ ; cin >> x >> y ; cout << “ x = “ << x << “ y = “ << y << endl; z = add ( x , y ) ; // calling add function cout << “ The result is “ << z ; cout <<“After call “<< “ x = “ << x << “ y = “ << y << endl; } int add ( int a , int b ) { return a + b ; }

Execution of Example 5 The user will be prompted to enter two integers. The user enters, for example, 10 and 20 that are saved in x and y respectively. When function add is called, the value of x and y are passed to the function. The function add takes the values 10 and 20 and links them to a and b respectively. The function will return the result which is 30. The output: Enter two integers: 10 20 x = 10 y = 20 The result is 30 After call x = 10 y = 20

Example 6: Call by Reference // File: calls2.cpp #include <iostream.h> // function prototype: the arguments to be passed by reference void duplicate (int & , int & , int & ); void main ( ) { int x, y , z ; // local variables in main cout << “ Enter three integers: “ ; cin >> x >> y >> z ; cout << “ Before call: “ << “ x = “ << x << “ y = “ << y << “ z = “ << z <<endl; duplicate ( x , y , z ) ; // calling duplicate function cout <<“ After call: << “ x = “ << x << “ y = “ << y << “ z = “ << z <<endl; } void duplicate ( int & a , int & b , int & c ) { a *= 2 ; b *= 2 ; c *= 2 ; }

Execution of Example 6 The user will be prompted to enter three integers. The user enters, for example, 10, 20, and 30 that are saved in x, y, and z respectively. When function duplicate is called, the addresses of x, y, and z are passed to the function. The addresses of x, y, and z are linked to the parameters of the function a, b, and c. The function will duplicate each parameter and save the result in the same parameter. Thus, a becomes 20 hence x becomes 20 also b becomes 40 hence x becomes 40 also c becomes 60 hence x becomes 60 also After the call we see that the values of x, y, and z are changed. The output: Enter three integers: 10 20 30 Before call: x = 10 y = 20 z = 30 After call: x = 20 y = 40 z = 60

Recursion Function call itself #include <iostream.h> int Fact (int N) { if (N<=1) return 1; else return N * Fact(N-1); } void main() { cout<<Fact(5)<<endl;

Example #include <iostream.h> int Zap (int N) { int Z; if (N<=1) Z=1; else Z= Zap(N-1) + Zap(N-3); return Z; } void main() { cout<<Zap(5)<<endl;

Array as parameter -Array name is pointer (call by reference) #include <iostream.h> void Increment (int a[]) { for (int i=0; i<5; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }

Array as parameter -Array element (call by value) #include <iostream.h> void Increment (int a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Increment(b[1]); cout<<b[1]<<endl; }