-Neelima Singh PGT(CS) KV Sec-3 Rohini

Slides:



Advertisements
Similar presentations
Chapter 5 Functions.
Advertisements

Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
Function Topic 4.
Predefined Functions Revisited
FUNCTIONS IN C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
This technique is Called “Divide and Conquer”.
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
Functions Najah Alsubaie Kingdom of Saudi Arabia
User-Defined Functions
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
C++ PROGRAMMING SKILLS Part 3 User-Defined Functions
Presentation transcript:

-Neelima Singh PGT(CS) KV Sec-3 Rohini Functions Calling -Neelima Singh PGT(CS) KV Sec-3 Rohini

Contents Definition of functions Function calling Function definition Void function Remarks on function Local v/s Global variables Function call methods Concept of recursion Function overloading

Definition of functions A function is a subprogram that acts on data and often returns a value.

Function Calling Function Arguments can be: Constant sqrt(9); Functions called by writing functionName (argument); or functionName(argument1, argument2, …); Example cout << sqrt( 900.0 ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double Function Arguments can be: Constant sqrt(9); Variable sqrt(x); Expression sqrt( x*9 + y) ; sqrt( sqrt(x) ) ;

Function Calling Calling/invoking a function sqrt(x); Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments After finished, passes back result Output argument Function Name 3 cout<< sqrt(9); Parentheses used to enclose argument(s)

Functions Functions Local variables Parameters Modularize a program Software reusability Call function multiple times Local variables Known only in the function in which they are defined All variables declared in function definitions are local variables Parameters Local variables passed to function when called Provide outside information

Function Definition Function prototype Calling/invoking a function Tells compiler argument type and return type of function int square( int ); Function takes an int and returns an int Explained in more detail later Calling/invoking a function square(x); Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments After finished, passes back result

// Creating and using a programmer-defined function. #include <iostream.h> int square( int ); // function prototype int main() { // loop 10 times and calculate and output // square of x each time for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; // function call cout << endl; return 0; // indicates successful termination } // end main Function prototype: specifies data types of arguments and return values. square expects an int, and returns an int. Parentheses () cause function to be called. When done, it returns the result. // square function definition returns square of an integer int square( int y ) // y is a copy of argument to function { return y * y; // returns square of y as an int } // end function square Definition of square. y is a copy of the argument passed. Returns y * y, or y squared. 1 4 9 16 25 36 49 64 81 100

Function maximum takes 3 arguments (all double) and returns a double. } // end main // function maximum definition. x, y and z are parameters double maximum( double x, double y, double z ) { double max = x; // assume x is largest if ( y > max ) // if y is larger, max = y; // assign y to max if ( z > max ) // if z is larger, max = z; // assign z to max return max; // max is largest value } // end function maximum // Finding the maximum of three floating-point (real) numbers. #include <iostream.h> double maximum( double, double, double ); // function prototype int main() double number1, number2; double number3; cout << "Enter three real numbers: "; cin >> number1 >> number2 >> number3; // number1, number2 and number3 are arguments to the maximum function call cout << "Maximum is: " << maximum( number1, number2, number3 ) << endl; return 0; // indicates successful termination Function maximum takes 3 arguments (all double) and returns a double. Enter three real numbers: 99.32 37.3 27.1928 Maximum is: 99.32 Enter three real numbers: 1.1 3.333 2.22 Maximum is: 3.333

Function Prototypes Function prototype contains Function name Parameters (number and data type) Return type (void if returns nothing) Only needed if function definition after function call Prototype must match function definition Function prototype double maximum( double, double, double ); Definition double maximum( double x, double y, double z ) { … }

Remarks on Functions Local variables Parameters Known only in the function in which they are defined All variables declared inside a function are local variables Parameters Local variables passed to function when called (passing- parameters) Variables defined outside and before function main: Called global variables Can be accessible and used anywhere in the entire program

Local vs Global Variables #include<iostream.h> int x,y; //Global Variables int add2(int, int); //prototype main() { int s; x = 11; y = 22; cout << “global x=” << x << endl; cout << “Global y=” << y << endl; s = add2(x, y); cout << x << “+” << y << “=“ << s; cout<<endl; cout<<“\n---end of output---\n”; return 0; } int add2(int x1,int y1) { int x; //local variables x=44; cout << “\nLocal x=” << x << endl; return x1+y1; global x=11 global y=22 Local x=44 11+22=33 ---end of output---

Function Call Methods Call by value Call by reference A copy of the value is passed Call by reference The caller passes the address of the value Up to this point all the calls we have seen are call-by-value, a copy of the value (known) is passed from the caller-function to the called- function Any change to the copy does not affect the original value in the caller function Advantages, prevents side effect, resulting in reliable software

Call by value method of accessing a function. # include<iostream.h> int main() { int cube(int); int vol, side=7; vol=cube(side); cout<<vol; return 0; } int cube (int a) return a*a*a ;  

Function Call Methods Call By Reference We introduce reference-parameter, to perform call by reference. The caller gives the called function the ability to directly access the caller’s value, and to modify it. A reference parameter is an alias for it’s corresponding argument, it is stated in c++ by “flow the parameter’s type” in the function prototype by an ampersand(&) also in the function definition-header. Advantage: performance issue void function_name (type &);// prototype main() { ----- ------ } void function_name(type &parameter_name)

value of orig in function change is : 20 value after change :-20 Example # include<iostream.h> int main() { void change(int &); int orig=10; cout<<”origional value is “<<orig ; change(orig); cout<<”value after change( ) is over”<<orig ; return 0; }    void change(int &a) a=20; cout<<”value of orig in function change( ) is”” <<a; return; output:”  the original value is :10 value of orig in function change is : 20 value after change :-20

THANKS