FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Spring Semester 2013 Lecture 5
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Functions Modules in C++ are called functions and classes
Functions in C Computer Programming(1)- 1090CS Manesh T
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
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.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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,
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
User defined functions
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according.
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Chapter 7: Function.
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
FUNCTIONS.
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
User Defined Functions
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.
Lec8.
CS149D Elements of Computer Science
In C Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Presentation transcript:

FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.

Types Functions User-Defined Functions Pre-Defined Functions

Pre-Defined Functions The pre-defined functions or library functions are built-in functions. The user can use the functions, but cannot modify the function. Example: sqrt()

User-Defined Functions The functions defined by the user for their requirement are called user-defined functions. Whenever it is needed, The user can modify the function. Example: sum(a,b)

Advantage of User-Defined Functions The length of the source program can be reduced. It is easy to locate error. It avoid coding of repeated instructions.

Elements of User-Defined Function Function declaration Function definition Function call

Function Syntax datatype function_name (parameters list) { local variable declaration; ………………………… body of the function; ………………………… return(expression); }

How Function Works Once a function is called the control passes to the called function. The working of calling function is temporarily stopped. When the execution of called function is completed then the control return back to the calling function and execute the next statement.

Parameters Actual Parameter These are the parameters transferred from the calling function to the called function. Formal Parameter These are the parameters which is used in the called function.

return Statement The return statement may or may not send some values to the calling function. Syntax: return; (or) return(expression);

Function Prototypes Function with no arguments and no return values. Function with arguments and no return values. Function with arguments and return values. Function with no arguments and with return values.

Function with no arguments and no return values Here no data transfer take place between the calling function and the called function. These functions act independently, i.e. they get input and display output in the same block.

Example #include void main() //calling function { void add(void); add(); } void add()//called function { int a,b,c; printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; printf("\nSum is:%d",c); }

Output Enter two number:3 4 Sum is:7

Function with arguments and no return values Here data transfer take place between the calling function and the called function. It is a one way data communication, i.e. the called program receives data from calling program but it does not return any value to the calling program.

Example #include void main() { int a,b; void add(int,int); printf("\nEnter two number:"); scanf("%d%d",&a,&b); add(a,b); } void add(int x,int y) //function with arguments { int z; z=x+y; printf("\nSum is:%d",z); }

Output Enter two number:2 4 Sum is:6

Example #include void main() { int a,b; void add(int a,int b); printf("\nEnter two number:"); scanf("%d%d",&a,&b); add(a,b); } void add(int x,int y) //function with arguments { int z; z=x+y; printf("\nSum is:%d",z); }

Output Enter two number:2 4 Sum is:6

Function with arguments and return values Here data transfer take place between the calling function and the called function as well as between called function and calling function. It is a two way data communication, i.e. the called program receives data from calling program and it return some value to the calling program.

Example #include void main() { int a,b,c; int add(int,int); printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=add(a,b); printf("\nSum is:%d",c); } int add(int x,int y) { int z; z=x+y; return(z); }

Output Enter two number:6 7 Sum is:13

Function with no arguments and with return values Here data transfer take place between the called function and the calling function. It is a one way data communication, i.e. the called program does not receives data from calling program but it return some value to the calling program.

#include void main() { int add(),d; d=add(); printf("\nSum is:%d",d); } int add() //function wit no argument { int a,b,c; printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; return(c); }

Output Enter two number:5 8 Sum is:13