0% found this document useful (0 votes)
9 views15 pages

Functionsinc 200305065757

The document provides an overview of functions in C programming, explaining their purpose and classification into library functions and user-defined functions. It details the syntax for defining and calling functions, including function prototypes and various categories of user-defined functions based on their return types and arguments. Examples are included to illustrate different types of functions, such as those with no return value and no arguments, and those that accept arguments but do not return a value.
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)
9 views15 pages

Functionsinc 200305065757

The document provides an overview of functions in C programming, explaining their purpose and classification into library functions and user-defined functions. It details the syntax for defining and calling functions, including function prototypes and various categories of user-defined functions based on their return types and arguments. Examples are included to illustrate different types of functions, such as those with no return value and no arguments, and those that accept arguments but do not return a value.
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/ 15

Functions in C

Dr.(Mrs.) J.Kalavathi. M.Sc.,Ph.D.,


Assistant Professor
Department of Information Technology
V.V.Vanniaperumal College for Women,
Virudhunagar.
Functions
A function is a block of code that performs a specific task.

There are many situations where we might need to write

same line of code for more than once in a program.


C functions can be classified into two categories,

Library Functions
User-defined Functions
Types of Functions
Types of Functions

Predefined standard library functions – such

as puts(), gets(), printf(), scanf() etc – These are the


functions which already have a definition in header files (.h
files like stdio.h), so we just call them whenever there is a
need to use them.
User Defined functions – The functions that we create in a

program are known as user defined functions.


Definition of Functions
Syntax of a Function
return_type: Return type can be of any data type such as int,

double, char, void, short etc. Don’t worry you will understand
these terms better once you go through the examples below.
function_name: It can be anything, however it is advised to

have a meaningful name for the functions so that it would be


easy to understand the purpose of function just by seeing it’s
name.
argument list: Argument list contains variables

names along with their data types. These arguments


are kind of inputs for the function. For example – A
function which is used to add two integer variables,
will be having two integer argument.
Body: Set of C statements, which will be executed

whenever a call will be made to the function.


Function Calls
 To call a function, you simply need to pass the required parameters

along with the function name, and if the function returns a value,

then you can store the returned value.


#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void introduction()
{
printf("Hi\n");
printf("How are you?");
/* There is no return statement inside this function, since its * return type
is void */
}
int main()
{
/*calling function*/
introduction();
return 0;
}
Function Declaration
A function prototype is simply the declaration of

a function that specifies function's name,

parameters and return type. It doesn't contain

function body.

A function prototype gives information to the

compiler that the function may later be used in

the program.
Syntax of function prototype

returnType functionName(type1

argument1, type2

argument2, ...);

Example:
int addNumbers(int a, int b);
Category of Functions
user defined function are further categorized
in four categories.
Function with no return and no argument
Function with no return but arguments
Function with return but no argument
Function with return and arguments
Function with no return no argument
In this method, We won’t pass any arguments to the function

while defining, declaring or calling the function.


This type of functions in C will not return any value when

we call the function from main() or any sub function.


Syntax

void function_name()
{
// Function body
}
Function with no return no argument
Example
#include<stdio.h>
void Addition(); // Function Declaration
void main()
{
printf("\n ............. \n");
Addition(); // Function Calling
}
void Addition() // Function Definition
{
int Sum, a = 10, b = 20;
Sum = a + b;
printf("\n Sum of a = %d and b = %d is = %d",
a, b, Sum);
}
Function with no return but with arguments
 Function with no return but with arguments, does
not return a value but accepts arguments as input.
 For this type of function you must define function

return type as void.


Syntax

void function_name(type arg1, type arg2, ...)

// Function body

}
Function with no return but with arguments
#include<stdio.h>
void Addition(int, int);
void main()
{
int a, b;
printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);
Addition(a, b); //Calling the function with dynamic values
}
void Addition(int a, int b)
{
int Sum;
Sum = a + b;
printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);
}

You might also like