Learners Support Publications www.lsp4you.com Functions in C++

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Spring Semester 2013 Lecture 5
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)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: User-Defined Functions I
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Learners Support Publications Classes and Objects.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
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.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 CS161 Introduction to Computer Science Topic #9.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Function Overloading and References
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
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 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Learners Support Publications Constructors and Destructors.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions.
User-Written Functions
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 7: User-Defined Functions II
FUNCTIONS In C++.
Functions and an Introduction to Recursion
User-Defined Functions
Classes and Objects.
Functions and an Introduction to Recursion
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Corresponds with Chapter 5
Presentation transcript:

Learners Support Publications Functions in C++

Learners Support Publications Objectives of this session Return types in main( ) Return types in main( ) Function Prototyping Function Prototyping Call by Reference & Call by Value Call by Reference & Call by Value Return by Reference Return by Reference Inline Functions Inline Functions Default Arguments Default Arguments Constant Arguments Constant Arguments Function Overloading Function Overloading

Learners Support Publications Introduction Dividing a program into functions. Dividing a program into functions. a major principle of top-down, structured programming. a major principle of top-down, structured programming. To reduce the size of the program. To reduce the size of the program. Code re-use. Code re-use. Like C++ operators, a C++ function can be overloaded to make it perform different tasks depending on the arguments passed to it. Like C++ operators, a C++ function can be overloaded to make it perform different tasks depending on the arguments passed to it.

Learners Support Publications Introduction void show( ); /* Function declaration */ void main( ) {---- show( ); /* Function call */ ----} void show( ) /* Function definition */ { /* Function body */ } continue…

Learners Support Publications The main( ) Function The main( ) returns a value of type int to the operating system by default. The main( ) returns a value of type int to the operating system by default. The functions that have a return value should use the return statement for termination. The functions that have a return value should use the return statement for termination. Use void main( ), if the function is not returning any value. Use void main( ), if the function is not returning any value.

Learners Support Publications Function Prototyping The prototype describes the function interface to the compiler by giving details such as: The prototype describes the function interface to the compiler by giving details such as: The number and type of arguments The number and type of arguments The type of return values. The type of return values. It is a template It is a template When the function is called, the compiler uses the template to ensure that proper arguments are passed, and the return value is treated correctly. When the function is called, the compiler uses the template to ensure that proper arguments are passed, and the return value is treated correctly.

Learners Support Publications Function Prototyping Function prototype is a declaration statement in the calling program. Function prototype is a declaration statement in the calling program. type function-name ( argument-list ) ; The argument-list contains the types and names of arguments that must be passed to the function. The argument-list contains the types and names of arguments that must be passed to the function. continue…

Learners Support Publications Function Prototyping Each argument variable must be declared independently inside the parentheses. Each argument variable must be declared independently inside the parentheses. float avg ( int x, int y) ; // correct float avg ( int x, y) ; // illegal In a function declaration, the names of the arguments are dummy variables and therefore they are optional. In a function declaration, the names of the arguments are dummy variables and therefore they are optional. continue…

Learners Support Publications Function Prototyping float avg ( int, int ) ; The variable names in the prototype just act as placeholders and, therefore, if names are used, they do not have to match the names used in the function call or function definition. void display( ); // function with an void display(void); // empty argument list. continue…

Learners Support Publications Call by Value A function call passes arguments by value. The called function creates a new set of variables and copies the values of arguments into them. The called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of values. The function does not have access to the actual variables in the calling program and can only work on the copies of values.

Learners Support Publications Call by Reference When we pass arguments by reference, the formal arguments in the called function become aliases to the actual arguments in the calling function. This means that when the function is working with its own arguments, it is actually working on the original data.

Learners Support Publications Inline Functions

Learners Support Publications Return by Reference A function can return a reference. int & max (int &x, int &y) { if (x > y) return x; else return y; }

Learners Support Publications Thank You