Вы находитесь на странице: 1из 9

1. What is C function?

A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by { program. } which performs specific operation in a C program. Actually, Collection of these functions creates a C

2. Uses of C functions:
C functions are used to avoid rewriting same logic code again and again in a program. !here is no limit in calling C functions to make use of same functionality wherever re"uired. #e can call functions any number of times in a program and from any place in a program. A large C program can easily be tracked when it is divided into functions. !he core concept of C functions are, re$usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.

3. C function declaration, function call and function definition:


!here are % aspects in each C function. !hey are,

&unction declaration or prototype $ !his informs compiler about the function name, function

parameters and return value's data type.

PNO 1

&unction call ( !his calls the actual function &unction definition ( !his contains all the

statements to be e)ecuted. C function aspects function definition function call function declaration

S.no * 0 %

syntax return+type function+name , arguments list { .ody of function/ } function+name , arguments list -/ return+type function+name , argument list -/

return_type function_name (argument declaration) { //local declarations //statements return (expression); } return-type 1pecifies the type of value that a function returns using the return statement. 2t can be any valid data type. 2f no data type is specified the function is assumed to return an integer result. function-name 3ust follow same rules of variable names in C. 4o two functions have the same name in a C program.

PNO 2

ar ument declaration 2s a comma$separated list of variables that receive the values of the argument when function is called. 2f there are no argument declaration the bracket consists of keyword void. A C function name is used three times in a program *. for function declaration 0. in a function call %. for function definition. !U"C#$%" &'C()*)#$%" +%*, -*%#%#.-' !he A412 C standard e)pands the concept of forward function declaration. !his e)panded declaration is called a function prototype. A function prototype performs two special tasks. *. &irst it identifies the return type of the function so that the compile can generate the correct code for the return data. 0. 1econd, it specifies the type and number of arguments used by the function. !he general form of the prototype is

Function Prototype:

arameter names can be omitted from the function prototype

return_type function_name (type1 name1, type2 name2,..., typen namen); !eturn type and parameter types must be pro"ided in the prototype Semi-colon indicates that this is only the function prototype, and that its definition will be found elsewhere

&igure5 %.% &unction 6rototype "ote: !he prototype normally goes near the top of the program and must appear before any call is made to the function.

PNO 3

#/' !U"C#$%" C)(( A function call is a postfi) e)pression. !he operand in a function is the function name. !he operator is the parameter lists ,7-, which contain the actual parameters. 'xample: 0oid main +, 1 sum +a, 2,3 4 #hen the compiler encounters the function call ,the control is transferred to the function sum,-.!he functions is e)ecuted line by line and outputs the sum of the two numbers and returns to main when the last statement in the following has e)ecuted and conceptually, the function's ending } is encountered. !U"C#$%" &'!$"$#$%" !he function definition contains the code for a function. 2t is made up of two parts5 !he function header and the function body, the compound statement is must.

&igure5 %.8 &unction 9efinition &unction header consists of three parts5 the return type, the function name, and the formal parameter list. function body contains local declarations and function statement. PNO 4

:ariables can be declared inside function body. &unction cannot be defined inside another function.

1imple e)ample program for C function5


As you know, functions should be declared and defined before calling in a C program. 2n the below program, function s"uare is called from main function. !he value of m is passed as argument to the function s"uare. !his value is multiplied by itself in this function and multiplied value p is returned to main function from function s"uare. 5include6stdio.h7 88 function prototype, also called function declaration float s9uare + float x ,3 88 main function, pro ram starts from here int main+ , 1 float m, n 3 printf + :;n'nter some num2er for findin s9uare ;n:,3 scanf + :<f:, =m , 3 88 function call n > s9uare + m , 3 printf + :;nS9uare of the i0en num2er <f is <f:,m,n ,3 4 float s9uare + float x , 88 function definition 1 float p 3 p>x?x3 PNO 5

return + p , 3 4

;utput5
<nter some number for finding s"uare 0 1"uare of the given number 0.====== is 8.======

8. >ow to call C functions in a program?


!here are two ways that a C function can be called from a program. !hey are, *. Call by value 0. Call by reference

*. Call by value5
2n call by value method, the value of the variable is passed to the function as parameter. !he value of the actual parameter can not be modified by formal parameter. 9ifferent 3emory is allocated for both actual and formal parameters. .ecause, value of actual parameter is copied to formal parameter. "ote:

Actual parameter ( !his is the argument which is used in function call. &ormal parameter ( !his is the argument which is used in function definition

<)ample program for C function ,using call by value-5


PNO 6

2n this program, the values of the variables m and n are passed to the function swap. !hese values are copied to formal parameters a and b in swap function and used. 5include6stdio.h7 88 function prototype, also called function declaration 0oid s@ap+int a, int 2,3 int main+, 1 int m > 22, n > AA3 88 callin s@ap function 2y 0alue printf+: 0alues 2efore s@ap m > <d ;nand n > <d:, m, n,3 s@ap+m, n,3 4 0oid s@ap+int a, int 2, 1 int tmp3 tmp > a3 a > 23 2 > tmp3 printf+: ;n0alues after s@ap m > <d;n and n > <d:, a, 2,3 4

Output:
values before swap m @ 00 and n @ 88 values after swap m @ 88 and n @ 00

2. Call by reference:

PNO 7

2n call by reference method, the address of the variable is passed to the function as parameter. !he value of the actual parameter can be modified by formal parameter. 1ame memory is used for both actual and formal parameters since only address is used by both parameters.

Example program for C function (using call by reference):


2n this program, the address of the variables m and n are passed to the function swap. !hese values are not copied to formal parameters a and b in swap function. .ecause, they are Aust holding the address of those variables. !his address is used to access and change the values of the variables. 5include6stdio.h7 88 function prototype, also called function declaration 0oid s@ap+int ?a, int ?2,3 int main+, 1 int m > 22, n > AA3 88 callin s@ap function 2y reference printf+:0alues 2efore s@ap m > <d ;n and n > <d:,m,n,3 s@ap+=m, =n,3 4 0oid s@ap+int ?a, int ?2, 1 int tmp3 tmp > ?a3 ?a > ?23 ?2 > tmp3 printf+:;n 0alues after s@ap a > <d ;nand 2 > <d:, ?a, ?2,3 4

PNO 8

Output:
values before swap m @ 00 and n @ 88 values after swap a @ 88 and b @ 00

PNO 9

Вам также может понравиться