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

Functions

1. The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. 2. There are two types of functions - predefined/standard library functions and user-defined functions. Examples of each are given. 3. Advantages of using functions include reusability of code and modularity. The key aspects of functions - declaration, call, and definition - are explained.

Uploaded by

Sridhar Chintu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Functions

1. The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. 2. There are two types of functions - predefined/standard library functions and user-defined functions. Examples of each are given. 3. Advantages of using functions include reusability of code and modularity. The key aspects of functions - declaration, call, and definition - are explained.

Uploaded by

Sridhar Chintu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SUBJECT: C TOPIC: Functions

WRITTEN BY: DR.SRINIVAS YADLAPATY


Qualification: Mtech - Computer Science(JNTU-Hyderabad),
PHD in Computer Science and Engineering
Mobile number: 9989601027
Mail id: [email protected]
Experience(16 years): Worked as Assistant Professor, Associate Professor in
several reputed engineering colleges(Avanthi, Sri Indu, SVIET, ST.Mary’s,
Brilliant, Siddhartha engineering colleges in Hyderabad), Guest faculty for
MBES Engineering college – Ambajogai - Maharashtra for java, Guest faculty
for RK Engineering college - Vijayawada for Java, Guest faculty for MVR
engineering college - Vijayawada for Java , Software Engineer in Grdemettle,
Senior Software Engineer in Yaaltech, Working for Harvest online, Mumbai
on Java and Python, Working for Infimind solutions, Bangalore on Python and
Java. Currently working for St Mary’s Engineering College, Hyderabad.
---------------------------------------------------------------------------------------------------------
Function:
Function will do a task(work).
A function is a block of code that performs a specific task.
A function is a block of code which only runs when it is called.
Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Functions can be:
Predefined functions(Built in functions/Standard library functions):
These functions are already defined(built in/ready made) by the vendor. They
are defined in the header files.
Examples: printf(), scanf(), pow(), sqrt(), strlen() etc.
Userdefined functions(Custom defined functions):
Programmer has to define these functions. You can also create functions as per
your need. Such functions created by the user are known as user-defined
functions.
Examples: add(), find(), display(), evaluate(), show() etc.
Advantage of functions in C:
There are the following advantages of C functions.
1. By using functions, we can avoid rewriting same logic/code again and
again in a program.
2. We can call C functions any number of times in a program and from
any place in a program.
3. We can track a large C program easily when it is divided into multiple
functions.
4. Reusability is the main achievement of C functions.
In functions 3 things are important:
1. Function declaration/ Function prototype/Function template/Function
model: It consists of the function name, return type, and parameters (if any).
2. Function call: When ever we call a function control transfers from function
call to function definition. A function can be called multiple times.
3. Function definition: Actual work is done here.
Note, function names are identifiers and should be unique.
User defined functions:
1. Functions without arguments and without return type
2. Functions without arguments but with return type
3. Functions with arguments but without return type
4. Functions with arguments and with return type
1. Functions without arguments and without return type:
Function declaration:
A function must be declared globally in a c program to tell the compiler
about the function name, function parameters, and return type.
Syntax:
void function_name();--->void means returning nothing(means not returning
anything)
Example:
void find();
Function call:
Function can be called from anywhere in the program. The parameter list must
not differ in function calling and function declaration. We must pass the same
number of arguments as it is declared in the function declaration.
Syntax:
function_name();
Example:
find();
Function definition:
It contains the actual statements which are to be executed. It is the most
important aspect to which the control comes when the function is called.
Syntax:
void function_name()
{
---
---
}
Example:
void find()
{
int a=4,b=5,sum;
sum=a+b;
printf("Sum=%d",sum);
}
Program:
// Functions without return type and without arguments
#include<stdio.h>
void add(); // Function declaration
void main()
{
clrscr();
add(); // Function call(When ever we call a function control transfers to the
// function definition)

getch();
}
// Function definition
void add()
{
int a=5,b=7,s;
s=a+b;
printf("SUM=%d",s);
}
Output:
Program:
// Functions without return type and without arguments
#include<stdio.h>
// Function declarations
void add();
void subtract();
void main()
{
clrscr();
// Function calls
add();
subtract();
add();
getch();
}
// Function definitions
void add()
{
int a=5,b=7,s;
s=a+b;
printf("SUM=%d\n",s);
}
void subtract()
{
int x=9,y=5;
printf("Diff=%d\n",x-y);
}
Output:

2. Functions without arguments but with return type:


Function declaration:
Syntax:
return_type function_name();
Example:
int find();
Function call:
Syntax:
function_name();
Example:
find();
Function definition:
Syntax:
return_type function_name()
{
---
---
}
Example:
int find()
{
int a=4,b=5,sum;
sum=a+b;
return(sum);--->returned to function call
// This is also correct: return sum;
}

or

int find()
{
int a=4,b=5;
return(a+b);--->returned to function call
// This is also correct: return a+b;
}
Program:
// Function with return type but without arguments
#include<stdio.h>
int add(); // Function declaration
void main()
{
int sum;
clrscr();
sum=add(); // Function call
printf("SUM=%d",sum);
getch();
}
int add() // Function definition
{
int x=8,y=5,s;
s=x+y;
return s; // This is also correct: return(s);
}
/*
This also works:
int add()
{
int x=8,y=5;
return(x+y);
}
*/
Output:
Program:
// Function with return type but without arguments
#include<stdio.h>
// Function declarations
int add();
float product();
void main()
{
clrscr();
printf("SUM=%d\n",add()); // Function call
printf("Product=%f",product()); Function call
getch();
}
// Function definitions
int add()
{
int x=8,y=5;
return(x+y); // This is also correct: return x+y;
}
float product()
{
float x,y;
printf("Enter x and y:");
scanf("%f%f",&x,&y);
return x*y;
}
Output:

3. Functions with arguments but without return type:


Function declaration:
Syntax:
void function_name(list of parameters);--->void means returning
nothing(means not returning anything)
Example:
void find(int x,int y);
Function call:
Syntax:
function_name(list of arguments);
Note: When we call a function any thing given in the brackets can be called as
arguments.
Example:
find(7,9);
Function definition:
Syntax:
void function_name(list of parameters)
{
---
---
}
Example:
void find(int x,int y)
{
int sum;
sum=x+y;
printf(“SUM=%d”,sum);
}

or
void find(int x,int y)
{
printf(“SUM=%d”,x+y);
}
Program:
// Functions without return type but with arguments
#include<stdio.h>
void add(int x,int y); // Function declaration
void main()
{
clrscr();
add(9,7); // Function call(When ever we call a function control transfers to
// the function definition)

getch();
}
// Function definition
void add(int x,int y) // 9 is pushed into x and 7 is passed into y
{
int s;
s=x+y;
printf("SUM=%d",s);
}
Output:
Program:
// Functions without return type but with arguments
#include<stdio.h>
// Function declarations
void add(int x,int y,int z);
void product(float x,float y);
void main()
{
int a=4,b=8,c=5;
float x,y;
clrscr();
// Function calls
add(a,b,c);
printf("Enter a,b:");
scanf("%f%f",&x,&y);
product(x,y);
getch();
}
// Function definitions
void add(int x,int y,int z)
{
int s;
s=x+y+z;
printf("SUM=%d\n",s);
}
void product(float x,float y)
{
printf("Diff=%f",x-y);
}
Output:

Program:
// Functions without return type but with arguments
#include<stdio.h>
void add(int x,int y,int z); // Function declaration
void main()
{
int a=4,b=8,c=5;
clrscr();
add(a,b,c); // Function call
getch();
}
// Function definitions
void add(int x,int y,int z)
{
int s;
s=x+y+z;
printf("SUM=%d\n",s);
}
Output:
4. Functions with arguments and with return type:
Function declaration:
Syntax:
return_type function_name(list of parameters);
Example:
int find(int x,int y);
Function call:
Syntax:
function_name(list of arguments);
Note: When we call a function any thing given in the brackets can be called as
arguments. At function declaration and function definition we call them as
parameters.
Example:
find(7,9);
Function definition:
Syntax:
int function_name(list of parameters)
{
---
---
}
Example:
int find(int x,int y)
{
int sum;
sum=x+y;
return(sum); // This is also correct: return sum;
}

or
int find(int x,int y)
{
return(x+y); // This is also correct: return x+y;
}
Program:
// Function with return type and with arguments
#include<stdio.h>
int add(int x,int y); // Function declaration
void main()
{
int x=20,y=10;
clrscr();
printf("SUM=%d",add(x,y)); // Function call(x and y are the arguments passed
// to function definition)

getch();
}
int add(int x,int y) // Function definition
{
return x+y;
}
Output:
Program:
// Function with return type and with arguments
#include<stdio.h>
#include<math.h>
// Function declarations
int add(int x,int y);
float sroot(float x);
void main()
{
int x=20,y=10;
float a=9.0;
clrscr();
printf("SUM=%d\n",add(x,y)); // Function call
printf("SQ.ROOT=%f",sroot(a)); // Function call
getch();
}
// Function definitions
int add(int x,int y)
{
return x+y;
}
float sroot(float x)
{
/*
This is also correct:
float sr;
sr=sqrt(x); // sqrt() is a predefined function that finds square root of a
number return sr;
*/
return sqrt(x);
}
Output:

You might also like