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,

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Spring Semester 2013 Lecture 5
Chapter Five Functions
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 5 Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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 
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
CPS120: Introduction to Computer Science Functions.
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.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Learners Support Publications Functions in C++
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
What is a function? Functions are nothing but sub-programs of a program. Is a part of a program which performs a particular task as desired by the programmer.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
C# Programming Methods.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 7: Function.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 5 Functions DDC 2133 Programming II.
Function There are two types of Function User Defined Function
Module 4 Functions – function definition and function prototype.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Functions CIS 40 – Introduction to Programming in Python
Method.
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Tejalal Choudhary “C Programming from Scratch” Function & its types
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions, Part 1 of 3 Topics Using Predefined Functions
FUNCTION CSC128.
Chapter 6 Methods.
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction To Programming
In C Programming Language
FUNCTION.
Functions Imran Rashid CTO at ManiWeber Technologies.
Unit-1 Introduction to Java
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 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, which is main(), and all the most trivial programs can define additional functions.

NEED OF A FUNCTION  MAKE PROGRAM HANDLING EASIER AS ONLY A SMALL PART OF THE PROGRAM IS DEALT WITH AT A TIME  REDUCE THE PROGRAM SIZE (BY REDUCING THE REPEATITION OF THE SAME CODING )  MAKES THE PROGRAM MORE READABLE AND UNDERSTANDABLE.

TYPES OF FUNCTIONS TWO TYPES  BUILT-IN FUCTIONS(ALSO CALLED LIBRARY FUNCTIONS)  USER-DEFINED FUNCTIONS

BUILT-IN FUNCTIONS  THESE FUNCTIONS ARE THE PART OF THE C++ LIBRARY.  CAN BE DIRECTLY CALLED  EXAMPLE- clrscr(), exit(), strlen() etc.

User defined functions  THESE FUNCTIONS ARE CREATED BY YOU (THE PROGRAMMER)  FOR THIS FIRST YOU HAVE TO DECLARE A FUNCTION BY GIVING ITS NAME (FUNCTION NAME), ITS PARAMETERS(THE VALUES IT ACCEPTS), ITS RETURN TYPE ( TYPE OF VALUE RETURN BY THE FUNCTION)

FUNCTION DECLARATION RETURNTYPE FUNCTIONNAME(PARAMETERLIST) { FUNCTION BODY } (ALSO CALLED AS FUNCTION PROTOTYPING)

A C++ function definition consists of a function header and a function body. Here are all the parts of a function: Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body: The function body contains a collection of statements that define what the function does.

EXAMPLE OF USER DEFINED FUNCTION

Following is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum between the two: int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }