0% found this document useful (0 votes)
5 views

Functions

The document provides an overview of functions in programming, including their definition, advantages, and types. It outlines the steps for developing a function, such as declaration, calling, and definition, and distinguishes between predefined and user-defined functions. Additionally, it includes examples and tasks for creating various functions in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions

The document provides an overview of functions in programming, including their definition, advantages, and types. It outlines the steps for developing a function, such as declaration, calling, and definition, and distinguishes between predefined and user-defined functions. Additionally, it includes examples and tasks for creating various functions in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Function

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

• Small Block of codes having a


particular name, followed by a pair of
parenthesis, designed to perform a
particular task.

3
Advantage
s

4
Advantages of Function

• Reduced lines of codes


• Easy Error detection and rectification
• Reusability

5
Types of Function

Pred • Already Present


efin
ed
Func
tion

User • Programmer Creates for


Defi particular task
ned
Func
tion

6
Steps Required for Developing a
Function

• Function Declaration
• Function Call
• Function Definition or Function Body

7
Function Declaration

• Giving the information about


Function to compiler

8
Function Call

• Invoking the function so that it starts


running is called function call

9
Function Definition

• Writing set of instruction inside a


curly braces of function to perform
actual task of function

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

• Header files contain function


declaration whereas library files
contains function definition
• Header files have extension .h
whereas library files have .lib
• Text file whereas library files are
machine code or binary codes

13
Difference between header files and
library files

• Located inside a folder Include


whereas library files are located
inside Lib
• They are added by programmer
whereas Library files are attached by
software called linker

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

• No return type no arguments


• With returntype no arguments
• No returntype with arguments
• With returntype with arguments

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

• WAP to create a function called


factorial which should accept an
integer as an argument and should
calculate and return its factorial.

26
To do

• WAP to create a function called


convert() which should accept a
character as argument and return it
by converting from lower case to
upper case and vice versa. If the
passed character is not an alphabet
then it should be returned as it is.

27
No return type with argument

#include<stdio.h> void average(int x, int y, int z)


#include<conio.h> {
void average(int,int,int); float t;
t=(float)(x+y+z)/3;
void main()
printf(“Avg is: %f”,t);
{ }
int a,b,c;
clrscr();
printf(“Enter 3 integers”);
scanf(“%d %d %d”,&a,&b,&c);
average(a,b,c);
getch();
}
28
To do

• WAP to create a function showTable(). This


function should accept 2 arguments representing
the numbers and number of terms and it should
print the table of first number upto the number
of terms given by the second number.
• WAP to create a function compare() that accept
three arguments from user. It should display the
largest element and 2nd largest element among
the numbers

29
With return type No Argument

#include<stdio.h> float average()


#include<conio.h> {
int a,b,c;
float average(void);
float d;
void main() printf(“Enter 3 Integer”);
{ scanf(“%d %d %d”,&a,&b,&c);
d=(float)(a+b+c)/3;
float d; return d;
clrscr(); }
d=average();
printf(“Average is %f”,d);
getch();
}
30
To do

• WAP to take the input of n numbers


in the function from the user and
compute the sum of total n numbers.

31
No return type No Argument

#include<stdio.h> void average()


#include<conio.h>{
void average(void); int a,b,c;
float d;
void main()
printf(“Enter 3 Integer”);
{ scanf(“%d %d
clrscr(); %d”,&a,&b,&c);
average(); d=(float)(a+b+c)/3;
printf(“Avg is %f”,d);
getch();
}
}
32
To do

• WAP to create a function compare()


that accept three arguments from
user. It should display the largest
element and 2nd largest element
among the numbers

33
To do

• WAP to create a function MaxMin()


that takes input of n numbers from
the user and displays the largest
element and smallest element
among the given numbers
• WAP to create a function sort() that
takes input of n numbers in a random
fashion from the user and displays
the sorted elements.
34
Conclusion and Few points

• Components and Types


• There is no limit on the number of functions
that might be present in a C program and A
function can be called any number of times
• Each function in a program is called in the
sequence specified by the function calls in
main( )
• If the value of a formal argument is changed
in the called function, the corresponding
change does not take place in the calling
function.
35
To do

• WAP to accept any year entered


through the keyboard. Write a
function to determine whether the
year is a leap year or not.
• In a program a +ve integer is
entered. Write a function to obtain
the prime factors of this number.
e.g. the prime factor of 9 is 3 and 3,
whereas prime factor of 35 is 5 and 7.
36

You might also like