Presentation is loading. Please wait.

Presentation is loading. Please wait.

User defined functions

Similar presentations


Presentation on theme: "User defined functions"— Presentation transcript:

1 User defined functions
Lecture 7 User defined functions Department of Computer Science

2 In C language a large program can be divided into a
series of a Individual related programs called modules called as “functions”. A function is also sometimes called as a subprogram that carries out some specific and well defined tasks.

3 Need for user defined functions
When the program size is large/ big , the task of debugging, testing and maintaining becomes difficult. i.e. if a program is divided into functional parts, then each part may be independently coded and later combined into a single unit, these subprograms are called as functions which are much easier to understand debug and test.

4 Subprocess Box Rectangle shaped, with bars on the sides
One line in, One line out Used to include a predefined process Subprocess

5 Elements of user defined functions
The 3 elements that are related to functions are 1.Function definition 2. Function call 3. Function declaration The function definition is an independent program module that is specially written to implement the requirements of the function. In order to use this function we need to invoke it at a required place in the program and is known as function call. The program that calls the function is referred to as the calling program or calling function. The calling program should declare any function that is to be used later in the program and is referred to as function declaration or function prototype.

6 Definition of functions
Function definition also referred to as function implementation shall possess the following elements: function name function type list of parameters local variable declarations function statements a return statement function header function body

7 General format of a function definition
function_type function name(parameter list) { local variable declaration; executable statement1; executable statement2; ………………………… return statement; }

8 Return values and their types
The return statement can take up one of the 2 forms return; or return (expression); Note 1: The plain return does not return any value. if(error) Note 2: The 2nd form or return with the expression returns value of the expression int sum(int x, int y) { int add; add=x+y; return(add); } return( x+y);

9 Example 1: addition of 2 numbers
start Input a,b Values of a, b int add(int m,int n ) sum=add (a,b) sum=m+n; Output sum return(sum) Value of sum returned Stop

10 sum=add ( a, b) add ( int m, int n)
Actual arguments m & n get their values from a & b add ( int m, int n) Formal arguments

11 /* To find the sum of 2 numbers using functions*/
#include<stdio.h> #include<conio.h> void main( ) { int a,b,sum; int add(int a, int b); printf(“\n enter the 2 numbers\n”); scanf(%d%d”,&a,&b); sum=add ( a, b); printf(“\n the sum of 2 numbers is :”,sum); } int add( int m, int n) { int sum; sum=m+n; return(sum); }

12 Categories of Functions
A function depending on whether arguments are present or not and whether a value is returned or not, may belong to one of the following Categories: Functions with no arguments and no return values Functions with arguments and no return values Functions with arguments and one return value Functions with no arguments but return a value Functions that return multiple values

13 function 1 ( ) { …………………….. function 2 ( ) ……………………... }
Functions with no arguments and no return values control function 1 ( ) { …………………….. function 2 ( ) ……………………... } function 2 ( ) { …………………….. …… …………………….. ……………………... } No input No output control

14 Functions with no arguments and no return values
When a function has no arguments, it does not receive any data from the calling function, similarly when it does not return a value, the calling function does not receive any data from the called function In effect, there is no data transfer between the calling function and the called function, the dotted lines indicate that there is only a transfer of control but not data

15 function 1 ( ) { …………………….. function 2 (a ) ……………………... }
Functions with arguments and no return values function 1 ( ) { …………………….. function 2 (a ) ……………………... } function 2 (f ) { …………………….. …… …………………….. ……………………... } Values of arguments No return value

16 Functions with arguments and no return values
Arguments matching between the function call and the called function main ( ) { ………………… function 1( a1, a2, a3 ………am); } function 1( f1, f2, f3 ……… fn) The function call should have matching arguments. 2. If the actual arguments are more than the formal arguments, then the extra actual arguments are discarded. 3. If the actual arguments are less than the formal arguments, the unmatched formal arguments are initialized to some garbage values. 4. A mismatch in the data type will also result in passing garbage values. Function call actual arguments Called Function formal arguments

17 function 1 ( ) { …………………….. function 2 (a ) ……………………... }
Functions with arguments and one return value function 1 ( ) { …………………….. function 2 (a ) ……………………... } function 2 (f ) { …………………….. …… …………………….. return (e); } Values of arguments Function result

18


Download ppt "User defined functions"

Similar presentations


Ads by Google