Functions Imran Rashid CTO at ManiWeber Technologies.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined 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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
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.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
© 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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
 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.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Predefined Functions Revisited
Functions and an Introduction to Recursion
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
CSC1201: Programming Language 2
User Defined Functions
Functions I Creating a programming with small logical units of code.
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.
Name: Rubaisha Rajpoot
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
Functions, Part 1 of 3 Topics Using Predefined Functions
CSC1201: Programming Language 2
Predefined Functions Revisited
CS 144 Advanced C++ Programming January 31 Class Meeting
CS1201: Programming Language 2
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

Functions Imran Rashid CTO at ManiWeber Technologies

Outline Function Function prototype (declaration) Function Call Function Definition Passing Arguments to Function Return Statement Pass by Reference Working of default arguments

Function 

Function What is Function? In programming, function refers to a segment that groups code to perform a specific task A function can be identified with small braces at the end () Depending on whether a function is predefined or created by programmer; there are two types of function: Library Function Library functions are the built-in function in C++ programming Programmer can use library function by invoking/calling function directly; they don't need to write it themselves Built-in functions often required a header file to be included

Function #include <iostream> #include <cmath> using namespace std; int main() { double num, squareRoot; cout << "Enter a number: "; cin >> num; // sqrt() is a library function squareRoot = sqrt(num); cout << "Square root of "<< num; cout<< " = " << squareRoot; return 0; }

Function In the last example, sqrt() library function is invoked/called to calculate the square root of a number Notice code #include <cmath> in the last program The function definition of sqrt()(body of that function) is present in the cmath header file You can use all functions defined in cmath when you include the content of file cmath Every valid C++ program has at least one function, that is, main() function Enter a number: 26 Square root of 26 = 5.09902

Function User-defined Function C++ allows programmer to define their own function A user-defined function groups code to perform a specific task and that group of code is given a name (identifier) When the function is invoked from any part of program, it all executes the codes defined in the body of function

Function When a program begins running, the system calls the main() function, that is, the system starts executing codes from main() function When control of the program reaches to function_name() inside main(), it moves to void function_name() and all codes inside void function_name() is executed Then, control of the program moves back to the main function where the code after the call to the function_name() is executed as shown in the last slide example

Function Enters two integers: 8 -4 Sum = 4 #include <iostream> using namespace std; // Function prototype (declaration) int add(int, int); int main() { int num1, num2, sum; cout<<"Enters two numbers to add: "; cin >> num1 >> num2; // Function call sum = add(num1, num2); cout << "Sum = " << sum; return 0; } // Function definition int add(int a, int b) { int add; add = a + b; // Return statement return add; } Enters two integers: 8 -4 Sum = 4

Function prototype (declaration)

Function prototype (declaration) If a user-defined function is defined after main() function, compiler will show error It is because compiler is unaware of user-defined function, types of argument passed to function and return type In C++, function prototype is a declaration of function without its body to give compiler information about user-defined function Function prototype in the below example is: int add(int, int);

Function prototype (declaration) You can see that, there is no body of function in prototype Also, there are only data type of arguments but no arguments You can also declare function prototype as below but it's not necessary to write arguments: It is not necessary to define prototype if user-defined function exists before main() function int add(int a, int b);

Function Call

Function Call To execute the codes of function body, the user-defined function needs to be invoked(called) In the last complete program (slide 9): add(num1,num2); Inside main() function calls the user-defined function The function returns an integer which is stored in variable add

Function Definition

// Function definition int add(int a, int b) { int add; add = a + b; The function itself is referred as function definition Function definition in the below program is: // Function definition int add(int a, int b) { int add; add = a + b; // Return statement return add; }

Passing Arguments to Function

Passing Arguments to Function In programming, argument (parameter) refers to the data which is passed to a function (function definition) while calling it In the last example, two variables, num1 and num2 are passed to function during function call These arguments are known as actual arguments The value of num1 and num2 are initialized to variables a and b respectively These arguments a and b are called formal arguments which receives values from actual arguments

Passing Arguments to Function

Passing Arguments to Function Notes on passing arguments The numbers of actual arguments and formals argument should be the same (Exception: Function Overloading) The type of first actual argument should match the type of first formal argument Similarly, type of second actual argument should match the type of second formal argument and so on You may call function a without passing any argument The number(s) of argument passed to a function depends on how programmer want to solve the problem In the last program, both arguments are of int type but it's not necessary to have both arguments of same type

Return Statement

Return Statement A function can return a single value to the calling program using return statement

Pass by Reference

Pass by Reference

Pass by Reference

Working of default arguments

Working of default arguments

Any ?