WELCOME…
FUNCTIONS
 A function is a block of statements that performs a specific task.
OR
 We can divide a large problem into the basic building bocks known as function.
 Advantage of functions in C:
• By using functions, we can avoid rewriting same logic/code again and again in a program.
• We can call C functions any number of times in a program and from any place in a program.
• We can track a large C program easily when it is divided into multiple functions.
• Reusability is the main achievement of C functions.
• To improve the readability of the code.
• Debugging the code would be easier if you use function, as errors are easy to be traced.
FUNCTIONS
 Disadvantages:
There are very few disadvantages of using functions in C:
Complexity of the program increases.
Execution speed decreases.
It requires a programmer must be expert in programming.
 Example For Function:
main
Sum( )
Print’s The sum of two num program
Function call a
100
b
200
Print’s Enter two num
Reads a & b values
Print’s The sum is (a+b)
10 8
Print’s The sum is (18) END
Functions
Pre Defined User Defined
 Pre Defined Functions:
 Standard library functions are known as built in functions.
 Functions such as printf(), scanf(), puts(), gets(),etc… are the standard library functions.
 The above mentioned functions are already mentioned in the header files.
 We just call them whenever there is a need to use them.
Function Declaration :
Syntax:
Function Call:
Syntax:
Function Definition:
Syntax:
return_type function_name(argument list);
function_name(argument list);
return_type function_name(argument list)
{
function body;
}
User Defined Functions:
 The functions created by user is known as user defined functions.
Call By Value Call By Reference
 Call By Value and Call By Reference
1) In call by value, the value of
actual parameters is copied into
the formal parameters.
2) The changes made in the
formal parameters does not
reflect the actual parameters.
3) In call by value method only
primitive datatypes are passed.
1) In call by reference, the
address of the variable is passed
into the function call as actual
parameters.
2) The changes made in the
formal parameters reflected in
the actual parameters.
3) In call by reference only non-
primitive datatypes are passed.
 Example For Call By Value:
main
Print’s Before fun call x=100
x=100
Change(x)
Function call
Print’s Before adding inside the function num= 100(as num = 100)
num = 100+100 As num = 100;
Now num = 200;
Print’s After adding inside the function num= 200(as num = 200)
Outside the function
After func call x=100; END
 Example For Call By Reference:
main
x=10 y=11
Print’s Values before swap: x= 10, y=11;
Swap(&x,&y);
Function call
300
temp
10
x
100
y 11
200
Passing the address of the x and y
*x=(10)
*x=*y *x = 11
*y = temp *y = 10
Print’s Values after swap: x= 11,y=10; END
Functions

Functions

  • 1.
  • 2.
  • 3.
     A functionis a block of statements that performs a specific task. OR  We can divide a large problem into the basic building bocks known as function.  Advantage of functions in C: • By using functions, we can avoid rewriting same logic/code again and again in a program. • We can call C functions any number of times in a program and from any place in a program. • We can track a large C program easily when it is divided into multiple functions. • Reusability is the main achievement of C functions. • To improve the readability of the code. • Debugging the code would be easier if you use function, as errors are easy to be traced. FUNCTIONS
  • 4.
     Disadvantages: There arevery few disadvantages of using functions in C: Complexity of the program increases. Execution speed decreases. It requires a programmer must be expert in programming.
  • 5.
     Example ForFunction:
  • 6.
    main Sum( ) Print’s Thesum of two num program Function call a 100 b 200 Print’s Enter two num Reads a & b values Print’s The sum is (a+b) 10 8 Print’s The sum is (18) END
  • 7.
    Functions Pre Defined UserDefined  Pre Defined Functions:  Standard library functions are known as built in functions.  Functions such as printf(), scanf(), puts(), gets(),etc… are the standard library functions.  The above mentioned functions are already mentioned in the header files.  We just call them whenever there is a need to use them.
  • 8.
    Function Declaration : Syntax: FunctionCall: Syntax: Function Definition: Syntax: return_type function_name(argument list); function_name(argument list); return_type function_name(argument list) { function body; } User Defined Functions:  The functions created by user is known as user defined functions.
  • 9.
    Call By ValueCall By Reference  Call By Value and Call By Reference 1) In call by value, the value of actual parameters is copied into the formal parameters. 2) The changes made in the formal parameters does not reflect the actual parameters. 3) In call by value method only primitive datatypes are passed. 1) In call by reference, the address of the variable is passed into the function call as actual parameters. 2) The changes made in the formal parameters reflected in the actual parameters. 3) In call by reference only non- primitive datatypes are passed.
  • 10.
     Example ForCall By Value:
  • 11.
    main Print’s Before funcall x=100 x=100 Change(x) Function call Print’s Before adding inside the function num= 100(as num = 100) num = 100+100 As num = 100; Now num = 200; Print’s After adding inside the function num= 200(as num = 200) Outside the function After func call x=100; END
  • 12.
     Example ForCall By Reference:
  • 13.
    main x=10 y=11 Print’s Valuesbefore swap: x= 10, y=11; Swap(&x,&y); Function call 300 temp 10 x 100 y 11 200 Passing the address of the x and y *x=(10) *x=*y *x = 11 *y = temp *y = 10 Print’s Values after swap: x= 11,y=10; END