Está en la página 1de 27

Class 3

Functions

Introduction
A function is a self-contained block of statements that perform a coherent task of some kind. Derived data types in C Classifications:
Library functions User defined functions

main() is an example of user defined function Need for user defined functions

Advantages
1. It facilitates top-down modular programming

Function main

Function italy

Function india

Example

Advantages..
2. The length of a source program can be reduced by using functions at appropriate places 3. It is easy to locate and isolate a faulty function for further investigations 4. A function may be used by many other programs. This means that a C programmer can build on what others have already done.

Characteristics of modular programming


1. Each module should do only one thing 2. Communication between modules is allowed only by a calling module 3. A module can be called by one and only one higher module 4. No communication can take place directly between modules that do not have calling-called relationship 5. All modules are designed as single entry, singleexit systems using control structures

Similarities between functions and variables in C


Both function names and variable names are considered as identifiers and therefore they must adhere to the rules for identifiers. Like variables, functions have types associated with them Like variables, function names and their types must be declared and defined before they are used in a program

Three Elements
1. Function Definition
is an independent program module that is specially written to implement the requirements of the function function invoking also known as function prototype

2. Function Call

3. Function declaration

Example

Points to be noted
Any C program contains at least one function . if a program contains only one function, it must be function main() In a C program if there are more than one functions present, then one of these functions must be main(), because program execution always begins with main() There is no limit on the number of functions that might be present in a C program

Points to be noted
Each function in a program is called in the sequence specified by the function calls in main() After each function has done its thing, control returns to main(). When main() runs out of function calls, the program ends

The General form of a function in C


type-specifier function_name (argument list) argument declaration; { local variable declarations; function statement(s); return (expression); }

The General form of a function in C


type-specifier function_name (argument list) argument declaration; Data type of the { value which the local variable declarations; function will return function statement(s); return (expression); }

Example
Default type of a function is int

The General form of a function in C


type-specifier function_name (argument list) argument declaration; { Naming rules local variable declarations; is same as that of variable function statement(s); names return (expression); }

The General form of a function in C


type-specifier function_name (argument list) argument declaration; { Valid variable local variable declarations; names separated by function statement(s); commas. return (expression); }
Examples: power(x,n) mul(a,b,c) Examples: power(int x, int n) mul(int a,int b,float c)

The General form of a function in C


type-specifier function_name (argument list) argument declaration; { local variable declarations; function statement(s); return (expression); Returning value } to the calling
function

Example
calsum (x, y, z) int x, y, z; { int d; d =x+y+z; return(d); }
calsum (x, y, z) int x, y, z; { return(x+y+z); }

Format of return statement


Function can return one value per call
return (constant); return (variable); return (expression); return;

Function call
Actual Argument

Formal Argument

Important Note
Actual arguments can be an expression If the actual parameters are more than the formal parameters, the extra actual arguments will be discarded If the actual arguments are less than the formals, the unmatched formal arguments will be initialized to some garbage. Any mismatch in data types may also result in some garbage values

Function Declaration
A function declaration or function prototype consists of four parts
Function type (return type ) Function name Parameter list Terminating semicolon
Function-type function-name ( parameter list ) ;

Examples
1. int mul (int, int); int mul (int a, int b); mul (int a, int b); mul (int, int); 2. void mul (void );

Function Declaration
A prototype declaration may be placed in two places in a program
Above all the function (including main() ) Global prototype Inside a function definition Local prototype

Example

Global Prototype

Local Prototype

También podría gustarte