slide1: the content of functons
slide2: Introduction to function
slide3:function advantages
slide4 -5: types of functions
slide6: elements of user defined functions
1. INTRO DUCTION TO FUNCTIONS
TYPES OF FUNCTIONS
ELEM ENTS O F USER DEFINED FUNCTIO NS
TYPES O N THE BA SIS O F A RG UM ENTS A ND
RETURN VA LUES
METHODS OF CA LLING A FUNCTION
Functions
2. Introduction to
Function
Block of statements that perform the particular task.
Enables modular programming.
Main() is the driver function. Has pre defined
prototype.
Same function can be accessed from different places
within a program.
Once a function execution is completed , control
return to the place from where the function was
called.
4. Types of
Functions
Library (Built In) Functions:
They are written in the header files.
To use them appropriate header files should be included.
Header Files Functions Defined
stdio.h Printf(), scanf(), getchar(), putchar(),
gets(), puts(), fopen(), fclose()
conio.h Clrscr(), getch()
Ctype.h Toupper(), tolower(), isalpha()
Math.h Pow(), sqrt(), cos(), log()
Stdlib.h Rand(), exit()
String.h Strlen(), strcpy(), strupr()
6. Elements of User defined
functions
Function Prototype Function Call
Function arguments and parameters
Function Definitions
7. Function
prototype
It specify the type of value that is to be return
from the function and that is to be passed to the
function.
It is defined in the beginning before the function
call is made.
Syntax:
return-type name-of-function(list of arguments);
Example
Void sum(int, int);
8. Function
Call
A function can be called by specifying name and list
of arguments enclosed in parenthesis and separated
by comma.
If there is no arguments empty parenthesis are place
after function name.
If function return a value, function call is written as
assignment statement as:
A=sum(x,y);
9. Function arguments and
parameters
Arguments are also called actual parameters.
Arguments are written within parenthesis at the time
of function call.
Parameters are also called formal parameters.
These are written within parenthesis at the time of
function definition.
10. Function
Definition
It is the independent program module.
It is written to specify the particular task that is to be
performed by the function.
The first line of the function is called function
declarator and rest line inside { } is called function
body
11.
12. Return
statement
It is the last statement of the function that return
certain values.
It return certain types of values to the place from
where the function was invoked.
Syntax:
return(variable-name or constant);
13. #include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;
/* Function return type is integer so we are returning
* an integer value, the sum of the passed numbers.
*/
return sum;
}
int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);
/* Calling the function here, the function return type
* is integer so we need an integer variable to hold the
* returned value of this function.
*/
int res = addition(var1, var2);
printf ("Output: %d", res);
return 0;
14. Categories of
function
Function with no arguments and no return
Function with arguments but no return
Function with no arguments and return
Function with arguments and return
15. Function with no argument and no
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
sum(); //function call
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("sum is%d :", x+y);
}
16. Function with argument and no
return
#include<stdio.h>
int sum(int a, int b); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
sum(x,y); //function call
return 0;
}
int sum(int a, int b) //function defination
{
printf("sum is%d :", a+b);
}
17. Function with no argument and
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
int a;
a=sum();
printf("the sum of the numbers you entered is
%d",a);
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
return(x+y);
}
18. Function with argument and
return
#include<stdio.h>
int sum(int,int); // function prototype
int main()
{
int a,x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
a=sum(x,y);
printf("the sum of the numbers you entered is %d",a);
return 0;
}
int sum(int x, int y) //function defination
{
return(x+y);
}
20. Call by value
Copies the value of actual parameters into formal
parameters.
During execution whatever changes are made in
the formal parameters are not reflected back in
the actual parameters.
21.
22. Call by Reference
Reference(address) of the original variable is passed.
Function does not create its own copy, it refers to the
original values by reference.
Functions works with the original data and changes
are made in the original data.
23. #include<stdio.h>
int swap(int*,int*); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("x=%d and y=%d before calling functionn",x,y);
swap(&x,&y);
printf("x=%d and y=%d after calling function",x,y);
return 0;
}
int swap(int* x, int* y) //function defination
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
25. Block scope
A local scope or block is collective program statements put in and
declared within a function or block (a specific region enclosed with
curly braces) and variables lying inside such blocks are termed as
local variables.
#include <stdio.h>
int main ()
{
/* local variable definition and initialization */ int x,y,z;
/* actual initialization */ x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %dn", x, y, z);
return 0;
}
26. Function Scope:
A Function scope begins at the opening of the
function and ends with the closing of it. Function
scope is applicable to labels only. A label declared
is used as a target to go to the statement and both
goto and label statement must be in the same
function.
27. Program scope:
After defining a local variable, the system or the compiler won't be
initializing any value to it. You have to initialize it by yourself. It is
considered good programming practice to initialize variables before
using. Whereas in contrast, global variables get initialized
automatically by the compiler as and when defined. Here's how
based on datatype; global variables are defined.
Life time
Place of declaration
Name conflict
28. // C program to illustrate the global scope
#include <stdio.h>
// Global variable
int global = 5;
// global variable accessed from within a function
void display()
{
printf("%dn", global);
}
// main function
int main()
{
printf("Before change within main: ");
display();
// changing value of global variable from main function
printf("After change within main: ");
global = 10;
display();
}
29. File Scope:
These variables are usually declared outside of all of
the functions and blocks, at the top of the program and
can be accessed from any portion of the program.
When a global variable is accessible until the end of the
file, the variable is said to have file scope. To allow a
variable to have file scope, declare that variable with the
static keyword before specifying its data type.
Static int x =10;
31. Auto Storage Class
auto comes by default with all local variables as its
storage class. The keyword auto is used to define
this storage class explicitly
Syntax: auto datatype vname;
int roll; // contains auto by default
is the same as:
auto int roll;
32. register Storage Class
This storage class is implemented for classifying local
variables whose value needs to be saved in a CPU
register in place of RAM (Random Access Memory). This
is implemented when you want your variable the
maximum size equivalent to the size of the register. It
uses the keyword register.
Syntax: register datatype vname;
register int counter;
Register variables are used when implementing looping
in counter variables to make program execution fast.
Register variables work faster than variables stored in
RAM (primary memory).
&
33. static storage class
This storage class uses static variables that are used
popularly for writing programs in C language. Static
variables preserve the value of a variable even when
the scope limit exceeds. Static storage class has its
scope local to the function in which it is defined.
On the other hand, global static variables can be accessed
in any part of your program. The default value assigned is
'0' by the C compiler. The keyword used to define this
storage class is static.
Example:
static int var = 6
34. extern Storage class
The extern storage class is used to feature a variable to be used from
within different blocks of the same program. Mainly, a value is set to
that variable which is in a different block or function and can be
overwritten or altered from within another block as well.
#include <stdio.h>
int val;
extern void funcExtern();
main()
{
val = 10;
funcExtern();
}
35. Recursion
Recursion can be defined as the technique of
replicating or doing again an activity in a self-similar
way calling itself again and again, and the process
continues till specific condition reaches.
Every recursive solution has two major cases
• Base case
• Recursive case
36. Recursion and its types
Direct recursion
Indirect recursion
Tail recursion
Linear and tree recursion
37. Recursive functions can be classified on the
basis of :
a.) If the functions call itself directly or indirectly –
Direct / Indirect
b.) If an operation is pending at each recursive
call – Tail Recursive/ Not
c.) based on the structure of the function calling
pattern – Linear / Tree
38. Direct Recursion:
If a function explicitly calls itself it is called directly
recursive.
When the method invokes itself it is direct.
int testfunc(int num)
{ if (num == 0)
return 0;
else
return (testfunc(num - 1));}
Here, the function ‘testfunc’ calls itself for all positive
values of num.
39. Indirect Recursion
This occurs when the function invokes other method
which again causes the original function to be called
again.
If a method ‘X’ , calls method ‘Y’, which calls method
‘Z’ which again leads to ‘X’ being invoked is called
indirect recursive or mutually recursive as well.
int testfunc1(int num)
{ if (num == 0)
return 0;
else
return (testfunc2(num - 1));
}
int testfunc2(int num2)
{ return testfunc1(num2 - 1);}
40. Tail / Bottom Recursion
A function is said to be tail-recursive, if no
operations are pending when the recursive
function returns to its caller.
Such functions, immediately return the return
value from the calling function.
It is an efficient method as compared to others, as
the stack space required is less and even
compute overhead will get reduced.
Recollect the previously discussed example,
factorial of a number. We had written it in non tail
recursive way, as after call operation is still
pending.
41. Linear and Tree Recursion
Depending on the structure the recursive function
calls take, or grows it can be either linear or non
linear.
It is linearly recursive when, the pending operations
do not involve another recursive call to the function.
Our Factorial recursive function is linearly recursive
as it only involves multiplying the returned values
and no further calls to function.
Tree recursion is when, pending operations involve
another recursive call to function.
For example – Fibonacci series, the pending
operations have recursive call to the fib() recursive
function to compute the results.