3. Introduction
• The concept of functions is explained by
comparing it to real world scenario. Where a
team is assigned with the new task and the
team leader assigns the tasks to his
subordinates. This analogy is compared with
the concepts of functions.
4. What is function
• In information technology, the term
function has a number of meanings. In C
language and other programming, a
function is a named procedure that performs
a distinct service.
6. Categories of Functions
• Functions without, Parameters and Return
Value
• Functions with Parameters, without return
value
• Functions without Parameters, with return
value
• Functions with, Parameters and return value
8. Syntax-Functions without, parameters and Return value
void CallingFunction()
{
CalledFunction();
}
void CalledFunction()
{
Read input
Perform task
Retain the result
}
9. Example-Functions without, parameters and Return value
void main()
{
add();
}
void add()
{
int a,b, sum;
printf(“Enter two valuesn”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“Sum=%d”,sum);
}
10. Functions with parameters, without Return value
Parameters
He will take the
parameters and add,
but keep the result to
himself
11. Syntax-Functions with parameters , without Return value
void Calling()
{
CalledFunc (actual Parameters);
}
void CalledFunc( formal parameters)
{
Perform task using formal parameters
Retain the result
}
12. example-Functions with parameters , without Return value
void main()
{
int a,b, sum;
printf(“Enter two valuesn”);
scanf(“%d%d”,&a,&b);
add(a,b);
}
void add( int a, int b)
{
int sum;
sum=a+b;
printf(“Sum=%d”,sum);
}
13. Functions without parameters, with Return value
I will not give you any
input, you take on your
own, perform task and
return me the result
He will take the input from
user, add and send back the
result to the boss
14. Syntax-Functions without parameters , with Return value
void CallingFunction()
{
Take the result =CalledFunction();
} return _type CalledFunction()
{
Read input
Perform task
Reurn the result
}
15. Example--Functions without parameters , with Return value
void main()
{
int result;
result=add();
printf(“Sum=%d”,result);
}
int add()
{
int a,b, sum;
printf(“Enter two valuesn”)
scanf(“%d%d”,&a,&b);
sum=a+b;
return sum;
}
16. Functions with, parameters and Return value
Take these parameters, add
them and return me the
result
He will take the
parameters and add, and
return the result to the
boss
Parameters
17. Syntax-Functions with parameters , with Return value
void Calling()
{
Take_the_result = CalledFunc (actual Parameters);
}
return_type CalledFunc( formal parameters)
{
Perform task using formal parameters
Return the result
}
18. Example--Functions with parameters , with Return value
void main()
{
int a,b, result;
printf(“Enter two valuesn”);
scanf(“%d%d”,&a,&b);
result=add(a,b);
printf(“Sum=%d”,result);
} int add( int a, int b)
{
int sum;
sum=a+b;
return sum;
}
20. Syntax-of recursive functions
void Calling()
{
Take_the_result = CalledFunc (actual Parameters);
}
return_type CalledFunc( formal parameters)
{
Perform task using formal parameters
(call the called function)
Return the result
}
21. Example—RECURSIVE FUNCTIONS
void main()
{
int a,b, result;
printf(“Enter a numbern”);
scanf(“%d”,&a);
result=fact(a);
printf(“Factorial=%d”,result);
}
int fact( int a)
{
int f;
if(a==0) return 1;
else return a*fact(a-1);
}