1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.

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.
Ch. 8 Functions.
The Assembly Language Level
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
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 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
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++ Function 1. Function allow to structure programs in segment of code to perform individual tasks. In C++, a function is a group of statements that.
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.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
#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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
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.
C++ Functions A bit of review (things we’ve covered so far)
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Functions and Program Structure CSE 2031 Fall June 2016.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Instructions for test_function
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.
C Functions Pepper.
Suppose we want to print out the word MISSISSIPPI in big letters.
FUNCTIONS In C++.
Functions and an Introduction to Recursion
Classes & Objects.
Chapter 5 Functions.
Chapter 5 Conclusion CIS 61.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Pointers and Pointer-Based Strings
Chapter 5 Function Basics
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.
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
Pointers and Pointer-Based Strings
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions Chapter No. 5.
Presentation transcript:

1

2

FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

DEFINITION: A function is a group of statement that together perform a task and reduce a complexity of a program. 4

Functions are building blocks of C and C++ and the place where all activity occurs. The general form of functions is ret-type function-name(parameter list) { body of the function } while declaring parameters data type should be same. 5

FUNCTION DECLARATION FUNCTION CALL FUNCTION DEFINITION 6

7

1.Call by value 2.Call by reference 8

9

10

11

In C we use functions that form a block of program and makes the program easier to understand. In C++ an important feature is supported called as inline functions, that are commonly used with classes. They make the function to be expanded in line. 12

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. We can define macro using #define. 13

14

15

A significant amount of overhead is generated by calling and return mechanism of function. While calling the function arguments are pushed onto the stack and saved on various registers and restore when function returns, this will take more time to run. If we expand a function code inline then function call produce faster run times. 16

1.Inline function process is similar to using a macro. 2.Inline is actually just a request, not a command. 3.By marking it as inline, you can put a function definition in a header file. 17

Function can be made as inline as per programmer need. Some useful recommendation are mentioned below- 1. Use inline function when performance is needed. 2. Use inline function over macros. 3. Prefer to use inline keyword outside the class with the function definition to hide implementation details. 18

int sum(int a, int b) { return a + b; } void print_sum() { int r = sum(5,6); printf("%d\n", r); } 19

If you declare your function as inline: inline int sum(int a, int b) { return a + b; } The compiler will replace the actual function call to the actual body of your function, thus, the resulting binary will have something like: void print sum() { int r = 5 + 6; printf("%d\n", r); } 20

#include using namespace std; inline int sqr (int x) //defined function as inlined { int y; y = x * x; return y; } int main() { int a =3, b; // declaration of variables b = sqr(a); //function call cout <<b; return 0; } 21

#include using namespace std; inline int Max(int x, int y) //defined function as inlined { return (x > y)? x : y; } int main( ) // Main function for the program { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } 22

When the above code is compiled and executed, it produces the following result : Max (20,10): 20 Max (0,200): 200 Max (100,1010):

In many places we create the functions for small work/functionality which contain simple and less number of executable instruction. Imagine their calling overhead each time they are being called by callers. 24

When a normal function call instruction is encountered, the program stores the memory address of the instructions immediately following the function call statement, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function codes, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function. Too much run time overhead. 25

The C++ inline function provides an alternative. With inline keyword, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program. 26

If the function code is large. If the function is recursive function. If the function contains static variables. For function returning values, if a loop, a switch, or a goto exists 27

28