Functions
Functions
Dr. S Jahangeer
Assistant Professor (Grade-II)
SCOPE, VIT, Chennai
1
Agenda
• About
• Advantages
• Broad Categorization
• Steps Required for Developing a
Function
• Component
• Types of function
2
Function
3
Advantage
s
4
Advantages of Function
5
Types of Function
6
Steps Required for Developing a
Function
• Function Declaration
• Function Call
• Function Definition or Function Body
7
Function Declaration
8
Function Call
9
Function Definition
10
function: main()
• main() :
– defined by
– called by
– declared by
11
Few Predefined function: clrscr()
• clrscr()
– Declared inside header files
– Called by programmer
– Defined in library files
12
Difference between header files and
library files
13
Difference between header files and
library files
14
Lifecycle of a C
Program
15
16
Component of function Declaration
• Syntax
<return_type> <fun_name>
(<arguments>);
– Return type
– Function name
– Function Argument, parameters
17
function Definition and Calling
• Syntax
<return_type> <fun_name>
(<arguments>)
{
//line of statements
}
• Syntax
<fun_name> (<arguments>);
18
Types of function
19
Built-in Example of: No return type No
Arguments
void main()
{
printf(“Hello C”);
}
20
Built-in Example of: With return type
No Arguments
• int main()
{
printf(“Hello C”);
return 0;
}
21
Built-in Example of: With return type
With Arguments
void main()
{
int x;
char str[10];
printf(“Enter a String”);
scanf(“%s”,str);
x=strlen(str);
printf(“String length is: %s”,str);
}
22
Built-in Example of: No return type
With Arguments
• void main()
{
int x;
char str[10];
printf(“Enter a number”);
scanf(“%d”,x);
printf(“Enter a String”);
fflush(stdin);
scanf(“%s”,str);
printf(“String is: %s and number is %d”,str,x);
}
23
With return type with argument
#include<stdio.h>
float average(int x, int y, int z)
#include<conio.h> {
float average(int,int,int); float x1;
Void main() x1=float(x+y+z)/3;
{ return x1;
int a,b,c; }
float d;
clrscr();
printf(“Enter 3 integers”);
scanf(“%d %d %d”,&a,&b,&c);
d=average(a,b,c);
printf(“avg is: %f”,d);
getch();
}
24
With return type with argument
#include<stdio.h>
float average(int x, int y, int z)
#include<conio.h> {
float average(int,int,int); float x1;
Void main() x1=float(x+y+z)/3;
{ return x1;
int a,b,c; }
float d;
clrscr();
printf(“Enter 3 integers”);
scanf(“%d %d %d”,&a,&b,&c);
d=average(a,b,c);
printf(“avg is: %f”,d);
getch();
}
25
To do
26
To do
27
No return type with argument
29
With return type No Argument
31
No return type No Argument
33
To do