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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
Chapter Five Functions
An Introduction to Programming with C++ Fifth Edition
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
Introduction to Methods
Chapter 6: Functions.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 3 Getting Started with C++
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.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
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.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
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.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Chapter 9: Value-Returning Functions
Chapter 7: Function.
Chapter 6: User-Defined Functions I
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Functions I Creating a programming with small logical units of code.
Chapter 4 void Functions
6 Chapter Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Classes and Objects
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
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.
Classes and Objects Systems Programming.
Presentation transcript:

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.  Every C program must have a main function to indicate where the program has to begin its executions.

Advantages:-  By using functions we can develop an application in module format  By using functions we can keep track of what logic we are implementing  The basic purpose of functions is code reusability  When we are developing an application in module format, then easily we can debug the program

 A C program is a combination of functions  Execution process start from main() and ends with main() only  When we are working with functions, functions can be implemented randomly  From any function we call any other functions  When we are calling a function which is defined later, for avoiding the compilation error we required to go for forward declaration i.e function prototype is required

Concepts of functions  Function Declaration or Function Prototype.  Function Definition.  Function Body  Function Arguments.  Function Call.  Function Return Type.

Function Declaration  A function declaration can appear outside of all functions  A function declaration can also appear inside function main.  All functions know about other declarations and can call these functions Syntax: Return_type function_name (type,type,…); 1.Return_type specifies the data type of the value in the return statement. 2.A Function can return any data type; if there is no return value, the keyword void is placed before the function name. 3.The function declaration terminates with a semicolon 4.By default return type of function is int

Function Definition:  The function definition is similar to the function declaration but does not have the semicolon.  The first line of the function is called a function declarator. This is followed by the function body. It is composed of the statements that make up the function, delimited by braces.  The declarator and declaration must use the same function name, number of arguments, argument types, and the return type.  No function definition allowed within a function definition  If the functions are defined before they are called, then the declarations are unnecessary.

Function Call  A function is a dormant entity, which comes to life when a call is made to the function. A function call is specified by the function name followed by the values of the parameters enclosed within parentheses, terminated by a semicolon.  When the compiler encounters a function call, the control is transferred to the function. Then the function executed line by line. Eg:- f1(); f1(10); f1(10,20);

Function Return Type:  Function in C may or may not have return value  If a function does not return a value, the return type in the function definition and declaration is specified as void. Otherwise, the return type is specified as a valid data type. There may be multiple return statements.  By default return type is integer.

Syntax for creating a function return_type function_name(parameters) { statement_block; function body return statement; }

 According to the syntax specifying return type, parameters and return statement are optional  All the rules of variable declarations required to applied for function name also

In C programming language functions are classified into two types 1.Library Functions or Built-in Functions or Predefined Functions 2. User Defined Functions

1. Library Functions or Predefined Functions:-  They all are pre implemented set of functions which is available along with compiler  The implementation part of library functions are available in.LIB or.OBJ file .LIB or.OBJ files contain precompiled code  When we are working with predefined functions for avoiding the compilation error, we require to include header files into our application .h file doesn’t provide any implementation part of predefined function, it provides only forward declaration i.e prototype of the function

Limitations of Library functions or predefined funtions  All predefined Function contains limited task only  As a programmer we shouldn’t have any control over the predefined functions  It is not possible to alter or modify the behaviour of any predefined functions For eg:- printf(), clrscr(), scanf(), pow(), sqrt() etc

2.User Defined Functions:-  As per the client or project requirement what function we are implementing it is called as user defined functions.  All user defined functions are project specific functions only.  As a programmer we are having full control on user defined functions  It is possible to modify or alter the behaviour of any user defined functions if it required

In C programming language user defined functions are classified into 4 types:- 1. Function with no arguments and no return type 2. Function with arguments and no return type 3. Function with no argument and one return return value 4.Function with argument and one return type

. Qus:-What is the use of functions in C? Ans:-It is possible to code any program in main() function only, but the program become to large and complex and as a result the task of debugging, testing and maintaining becomes difficult, So to avoid this complex a program is divided into functional parts, by which we can independently code a program and later we can combined into a single unit. These independently coded programs are called as subprograms that are easier to understand,debug and test. In C such subprograms are called as Functions.

Previous year questions:- 1)Distinguish between user defined and built-in functions? 2)What is meant by function prototype? Give an example of function prototype 3)What are the commanly used input functions in C? How are they accessed? 4)What is function Parameter ? Explain different types of parameters in C functions.