Spring Semester 2013 Lecture 5

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Programming In C++ Spring Semester 2013 Practice 1-3 Lectures Programming In C++, Lecture 3 By Umer Rana.
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
An Introduction to Programming with C++ Fifth Edition
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
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.
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.
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.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 CS161 Introduction to Computer Science Topic #9.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
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.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
User defined functions
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
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.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
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.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Deitel- C:How to Program (5ed)
CSCI 161: Introduction to Programming Function
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Tejalal Choudhary “C Programming from Scratch” Function & its types
Functions Declarations CSCI 230
Chapter 4 void 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.
Introduction to Classes and Objects
Classes and Objects.
Chapter 9: Value-Returning Functions
In C Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Presentation transcript:

Spring Semester 2013 Lecture 5 Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By Umer Rana Umer Rana

Function What is function? Programming In C++, Lecture 5 By Umer Rana

Function To avoid having to write the same code over and over. A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable. There are two types of function in C Built in function User-Defined function. Programming In C++, Lecture 5 By Umer Rana

return_type function_name (type1 arg1,type2 arg2,..,typen argn) Function Definitions A function definition has two principal components: Function Header Body of the Function Function Header: The first line of function definition is called function header. In consists of three parts: the data type of return value Function name (optionally) a set of arguments separated by commas and enclosed in parenthesis. return_type  function_name (type1 arg1,type2 arg2,..,typen argn) Programming In C++, Lecture 5 By Umer Rana

return_type function_name (Arguments/void) Function Definitions Body of the Function Variable declaration and the program logic are implement in the function body. return_type  function_name (Arguments/void) { Body of the function. } Programming In C++, Lecture 5 By Umer Rana Umer Rana

Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void – indicates that the function returns nothing Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter .Or the variables who receive the argument Programming In C++, Lecture 5 By Umer Rana

Function Definitions Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements } Declarations and statements: function body (block) Variables can be declared inside blocks (Local Variables) Functions can not be defined inside other functions Returning control If nothing returned return; or, until reaches right brace If something returned return expression; Programming In C++, Lecture 5 By Umer Rana

Function Return statement A function may or may not return a value. A ‘return’ statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a general expression, a pointer to function, or a function call. If a function does not return a value, the return type in the function definition and declaration is specified as void. Programming In C++, Lecture 5 By Umer Rana

Function Passing Arguments Arguments can be passed to a function by two methods, They are:- pass by value pass by reference Pass by value Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value. Programming In C++, Lecture 5 By Umer Rana

Function Passing Arguments Pass by reference When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don't need to return anything to calling function. We discuss in detail later in later chapters… Programming In C++, Lecture 5 By Umer Rana

Function int add(int p,int q) { return p+q; //Body of the function } A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.  For Example: int a; a= add(5,10); The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred. Programming In C++, Lecture 5 By Umer Rana

Function Prototype The Function Prototype means tells the compiler the name of the function, the data type the function returns, and the number and data of the function’s arguments. It is declare before the main function and ends with semicolon. A prototype declares a function. A function call executes a function. A function definition is the function itself. Programming In C++, Lecture 5 By Umer Rana

Calling Function Function Call is a mechanism that is used to invoke a function to perform a specific task. Function call can be invoked at any point in the program. Function calling statement terminate my semicolon (;). The following condition must be satisfied for function call: The number of arguments in the function calls and function declaration must be same. The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement. Programming In C++, Lecture 5 By Umer Rana

Function #include <stdio.h> int add(int p, int q); void main(void) { int total; printf(“Add two numbers.\n");    total=add(10,20); printf(“Total of two numbers is %d“,total);   }   int add(int p,int q)        {       return p+q;          //Body of the function       } Programming In C++, Lecture 5 By Umer Rana

Function #include <stdio.h> int add(int p, int q); void main(void) { int total,num1=10, num2=20; printf(“Add total of num1 & num2.\n");    total=add(num1,num2); printf(“Total of two numbers is %d“,total);   }   int add(int p,int q)        {       return p+q;          //Body of the function       } Programming In C++, Lecture 5 By Umer Rana

Function #include <stdio.h> void my_function(); void main(void) { printf("Main function.\n");   my_function();   printf("Back in function main.\n");   }   void my_function() printf(“This is BSCS-Group A.\n"); } Programming In C++, Lecture 5 By Umer Rana

Function int add(int p,int q); // function prototype void display(int p, int q, int r); // function prototype void main()         {         int a,b,c;         clrscr();         printf("Enter two numbers\n");         scanf("%d%d",&a,&b);         c=add(a,b);          display(a,b,c);          getch();                } int add(int p,int q)                 {         return(p+q);         } void display(int p, int q, int r)         {         printf("Sum of %d and %d is %d",p,q,r);         } Programming In C++, Lecture 5 By Umer Rana

Recursion Recursive functions are those functions, which call itself within that function. Recursion is the process of repeating items in a self-similar way. Programming In C++, Lecture 5 By Umer Rana

Recursion int sum(int n); int main() { int sum(int n) { int n; int num,input; printf("Enter a integer:\n"); scanf("%d",&num); input=sum(num); printf("sum=%d",add); } int sum(int n) { int n; if(n<10) n=sum(n+1); else return n; } Programming In C++, Lecture 5 By Umer Rana