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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Modular Programming With Functions
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Storage Classes.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using 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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Learners Support Publications Classes and Objects.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
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.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
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.
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.
1 ICS103 Programming in C Lecture 8: Functions I.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 6 Modularity Using Functions
Functions Course conducted by: Md.Raihan ul Masood
Chapter 4 Function and Structure.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
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.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
In C Programming Language
In C Programming Language
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Storage Classes.
Presentation transcript:

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 function.  A function is defined to be a self-contained program which is written for the purpose of accomplishing some task.  Function can be categorize into two types: 1.Build-in functions 2.User-defined functions. Build-in functions are those which are already made available as part of C Library. They can be used by any programmer.scanf(),printf(),strlen(),scrcpy() are some of the built-in functions. A used-defined function is one which is written by a user to solve a problem.

Advantage of function 1.Minimization of memory space usage: The repetition of the same code to more consumption of memory space. This problem can be avoided by defining a separate function. 2.Improvization of overall organization of a program: The overall organization of a program is improved way of compartmentalization or modularization. A program is divided into a number of subprograms. Each subprogram is viewed as a separate compartment or a module. 3.Facilitation of team work:The projects will be divided into a number of modules and each module is assigned to a different programmer.The programmer implement their modules using functions,will be integrated together at the final stage. 4.Simplification of software engineering task like testing and debugging: Identifying the location of the errors and correcting them the flexibility provided by functions.

Syntax: return-type function-name(arguments) { local variables; statements; return(expression); } where, return-type -> refers to the data type of the value being returned by the function. function-name-the function referred to while using function. The function-name and arguments declaration enclosed within a pair of parentheses is usually referred to as the Function Header. The function header is followed by the local variables declaration part and a set of executable statements of the function enclosed within a pair of opening brace and closing brace referred to as the Function Body.

Function Header+Function Body=Function Definition A function which invokes another function is called calling function and function invoked is termed called function. function can be classified into three types: 1.Functions with no arguments and no return value 2.Functions with arguments and no return value 3.Functions with arguments and return value

Function with no arguments and no Return Value In function f() which does not require arguments to be passed, and does not return any value to its calling program. The function definition will have the following form: void f() { variables; statements; } The function f() does not require arguments and does not return any value back to its calling program, there is no data communication between the function f() and its calling function.

Example: #include void largest() { int a,b,c,l; printf(“Enter three numbers \n”); scanf(“d%d%d”,&a,&b,&c); l=a; if(b>l) l=b; if (c>l) l=c; printf(“Largest=%d\n”,l); Output: } void main() Largest=5 { largest(); }

Functions with arguments and no Return Value The f() which requires arguments to be passed, and does not does not return any value to its calling program. The function definition will have the following form: void f(arguments) { variables; statements; } The function f() requires arguments and does not return any value back to its calling program, there is one-way data communication from the calling function to the function f().

Example: #include void largest(int a,int b,int c) { int l; l=a; if(b>l) l=b; if(c>l) l=c; printf(“Largest=%d \n”,l); } void main() { int a,b,c; int x,y,z; printf(“Enter values of a,b and c \n”); scanf(“%d%d%d”,&a,&b,&c); largest(a,b,c); printf(Enter values of x,y and z \n”); scanf(“%d%d%d”,&x,&y,&z); largest(x,y,z); getch(); } Output: Enter the values of a,b and c Largest=5 Enter the values of x,y and z Largest=6

Functions with arguments and no Return Value The f() which requires arguments to be passed, and also returns a value to its calling program. The function definition will have the following form: void f(arguments) { variables; statements; return(Expression); } The function f() requires arguments and also returns a value back to its calling program, there is two-way data communication between the calling function and the function f().

Example: #include void largest(int a,int b,int c) { int largest; largest=a; if(b>largest) largest=b; if(c>largest) largest=c; return(largest); } void main() { int a,b,c,lar; int x,y,z; printf(“Enter values of a,b and c \n”); scanf(“%d%d%d”,&a,&b,&c); lar=largest(a,b,c); printf(“Largest=%d \n”,lar); printf(“Enter values of x,y and z \n”); scanf(“%d%d%d”,&x,&y,&z); lar=largest(x,y,z); printf(“Largest=%d\n”,lar); } Output: Enter the values of a,b and c Largest=5 Enter the values of x,y and z Largest=6

Recursive Functions  Recursion is basically a method a function calling itself. This function involved in the process is referred to as a recursive function. Example: /To find factorial number */ #include int fact(int) void main() { int number,f; printf(“Enter a number \n”); scanf(“%d”,&number); f=fact(number); printf(“fact=%d \n”,f); } int fact(int number) { int f; if(number==0) return(1); else f=number*fact(number-1); return f; } Output: Enter a number 4 factorial of 4=24 4!=4*3 =4*3*2 =4*3*2*1 =24

/* To generate fibbonaci series */ #include int fib(int) void main() { int n,I; printf(“Enter a number \n”); scanf(“%d”,&); for(i=1;i<=n;i++) printf(“%d \n”,fib(i)); } int fib(int n) { int f; if(n==1) return 1; else if(n==2) return 1; else f=fib(n-1)+fib(n-2); return f; } Enter a number 5 0 1s 1 2 3

Scope of Variables Scope of variable is define to the area of its existence in a program. Scope determines the “lifetime” of the variable id defined to the duration of time for which variable exists. Depending on where variables are declared, fall into two categories: 1.Local variable 2.Global Variable Local Variable  The variable declared within a function is called a local variable. The value of the local variables are valid only within the function are declared.  The local variables which are defined in one function have no relationship in another function.  Local variable are created when a function is invoked and they are destroyed when the function is exited.

Global Variable  This variables that are declared prior to all functions are called global variables and can bed used by all the modules in the program. These variables are retained till the life time of the program.  Two global variables can have same name.

Storage Classes  The storage class determines the part of member storage is allocated for an object and how long the storage allocation continues to exit.  Storage class tells us: 1) Where the variable is stored. 2) Initial value of the variable. 3) Scope of the variable. Scope specifies the part of the program which a variable is accessed. 4) Life of the variable. There are four types of storage classes: 1) Automatic storage class 2) Register storage class 3) Static storage class 4) External storage class

AUTOMATIC STORAGE CLASS In this automatic storage class. Variable is stored in memory. Default value is garbage value. Scope is local to the block. Life is, with in the block in which the variable is defined. Example 1: void main() { auto int i=10; printf(“%d”,i); } Output: 10 Example 2: void main() { auto int i; printf(“%d”,i); } Output: 1002

In example 1, i value is initialized to 10.So,output is 10. In example 2, i value is not initialised.So,compiler reads i value is a garbage value. REGISTER STORAGE CLASS Variable is stored in CPU registers. Default value is garbage value. Scope is local to the block. Life is, with in the block in which the variable is defined. We can not use register storage class for all types of variables. For example: register float f; register double d; register long l;

Example void main() { register int i=10; printf(“%d”,i); } Output 10 STATIC STORAGE CLASS Variable is stored in memory. Default value is zero. Scope is local to the block. Life is, value of the variable persists between different function calls.

Example void main() { add(); } add() { static int i=10; printf(“\n%d”,i); i+=1; } Output 10 11

EXTERNAL STORAGE CLASS Variable is stored in memory. Default value is zero. Scope is local to the block. Life is, as long as the program execution doesn’t come to an end. Note: In given example program,i declared in two places.One is above the main(),second is within main().Within main() i is used in within main() only.Before main() i is used to outof the main() functions.i.e, display() uses the i value as 10.

Example : int i=10; void main() { int i=2; printf(“%d”,i); display(); } display() { printf(“\n%d”,i); } Output: 2 10

NOTE: Static and auto storage classes both are different in the case of life. Remaining all r same. In the case of recursive function calls we use static storage class. Register storage classes are used in the case of frequently used variables. Because these are stored in CPU registers. Extern storage class for only those variables are used almost all functions in the program. This would avoid unnecessary passing of the variables.