Functions Reasons Concepts Passing arguments to a function

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 10 More on Modular Programming and Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
C++ Tutorial Rob Jagnow. Overview Pointers Arrays and strings Parameter passing Class basics Constructors & destructors Class Hierarchy Virtual Functions.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
chap13 Chapter 13 Programming in the Large.
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.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
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.
C:\Temp\Templates 4 5 Use This Main Program 6.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Pointer to an Object Can define a pointer to an object:
Test 2 Review Outline.
User-Written Functions
Chapter 7: User-Defined Functions II
Chapter 6 CS 3370 – C++ Functions.
A bit of C programming Lecture 3 Uli Raich.
Lesson One – Creating a thread
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
FUNCTIONS In C++.
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
Passing Arguments to a Function
Programmazione I a.a. 2017/2018.
Pointer.
Pointers and References
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
C++ Tutorial Rob Jagnow
Observing how the machine acts
Chapter 9: Value-Returning Functions
Strings and Pointer Arrays
Dynamic Memory.
Functions Imran Rashid CTO at ManiWeber Technologies.
Programming in C Pointers and Arrays.
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
C Pointers Another ref:
Functions Chapter No. 5.
Presentation transcript:

Functions Reasons Concepts Passing arguments to a function Preset parameters Function returns Inline functions Static variables within a function

Reasons Segmenting program Easy to read, manage, and visualize Function reusability Repeating function codes, to reduce the amount of memory needs to run it

Concepts Declarations Definitions—function header and function body Calling Parameters Program 8.1, 8.2

Function Headers return_type function_name(parameter_list); Data return by return_type, e.g. double sine(double t); // prototype double d = sine(3.5); // calling void function_name(parameter_list); Data passing and return by parameter_list, e.g. void add(int x, int y, int &z); // prototype add(1, 2, k); cout << k; // 3, calling

Passing arguments to a Function Pass-by-value (program 8.3) Passing a pointer to a function Pass-by-reference

Passing a pointer Passing a pointer (Program 8.4) Passing an array as a function argument (Program 8.5) Using pointer notation when passing array (program 8.6) Constant pointer parameters Passing a multi-dimension array to a function (Program 8.7)

Pass-by-reference Using a reference parameter (Program 8.8) Pointer parameters v.s. reference parameters Using constant references

References vs Pointers In most situations, using a reference parameter is preferable to using a pointer An important difference between a pointer and a reference is that a pointer can be null, whereas a reference always refers to something

Arguments to main() int main(int argc, char* argv[]) { // code for main return 0; } Command line

Multiple Default Argument Values Program 8.9

Returning Values from a Function Returning a value Returning a pointer Never return the address of an automatic local variable from a function Returning a reference Never return a reference to an automatic local variable from a function

Returning a pointer Variables *longer(&value1, &value2) = 100; Local variables Global variables Static variables *longer(&value1, &value2) = 100; int* larger(int*a, int *b) { return *a>*b ? a:b; } // i.e. a =100 or b=100 Program 8.10

Returning a reference larger(value1, value2) = 50; int& larger(int& m, int& n) { return m > n ? m : n; }

Inline functions Very short function definitions More efficient and fast execution inline int larger(int m, int n) { return m > n ? m : n; }

Static Variables Static variable is created and remains in existence until the program terminates. One can carry over a value from one call of a function to the next. Program 8.11