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

C FUNCTIONS

The document explains the concept of functions in C programming, detailing their purpose in organizing code into manageable subprograms. It distinguishes between library/system functions and user-defined functions, outlining the components of function declaration, definition, and call. Additionally, it classifies functions based on data flow and provides examples for each type, along with exercises for practical understanding.

Uploaded by

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

C FUNCTIONS

The document explains the concept of functions in C programming, detailing their purpose in organizing code into manageable subprograms. It distinguishes between library/system functions and user-defined functions, outlining the components of function declaration, definition, and call. Additionally, it classifies functions based on data flow and provides examples for each type, along with exercises for practical understanding.

Uploaded by

pevahcomputers
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FUNCTION

A function is a block of codes or sub program with a set of


statements that perform some specific task when it is called.

Functions are used to divide a larger program into smaller


subprograms such that program becomes easy to understand and easy to
implement.

Any ‘C’ program contain at least one function i.e main().

There are two types of functions


1. Library/system function

These are function whose definition is defined by the system. they


can’t be modified, they can only be read and used.

Library system functions are defined inside the header files like
stdio.h, conio.h, math.h, string.h etc.

For example, the funtions printf() and scanf() are defined in the
header file called stdio.h.

2. User defined function

The user defined functions defined by the user according to its


requirement.

Every function in C has the following...


 Function Declaration (Function Prototype)
 Function Definition
 Function Call

Function Declaration

The function declaration is also called as function prototype. The


function declaration tells the compiler about function name,
datatype of the return value and parameters.

The function declaration is performed before main function or inside


main function or inside any other function.

Function declaration syntax

returnType functionName(parametersList);

The functionName is a user defined name used to identify the


function uniquely in the program.

returnType specifies the datatype of the value which is sent as a


return value from the function definiton.
The parametersList is the data values that are sent to the function
definition.

Function Definition

The function definition also known as body of the function provides


the actual code of that function.

Function definition syntax

returnType functionName(parametersList)
{

Actual code...

Function Call

The function call tells the compiler when to execute the function
definition.
When a function call is executed, the execution control jumps to the
function definition where the actual code gets executed and returns
to the same functions call once the execution completes. The
function call is performed inside main function or inside any other
function or inside the function itself.

Function call syntax

functionName(parameters);

For example, consider the following program in which we create a


fucntion called addition with two paramenters and a return value.

#include <stdio.h>

void main(){
int num1, num2, result ;
int addition(int,int) ; // function declaration

printf("Enter any two integer numbers : \n") ;


scanf("%d %d", &num1, &num2);

result = addition(num1, num2) ; // function call

printf("SUM = %d", result);


getch() ;
}
int addition(int a, int b) // function definition
{
return a+b ;
}
Classification of functions

Based on the data flow between the calling function and called
function, functions are classified as follows
 Function without Parameters and without Return value
 Function with Parameters and without Return value
 Function without Parameters and with Return value
 Function with Parameters and with Return value

Function without Parameters and without Return value

In this type of functions there is no data transfer between calling


function and called function. Simply the execution control jumps
from calling function to called function and executes called
function, and finally comes back to the calling function.

For example, consider the following program.

#include <stdio.h>
void main(){
void addition() ; // function declaration

addition() ; // function call

getch() ;//just to prevent the screen from dissapearing


}
void addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers :\n") ;
scanf("%d%d", &num1, &num2);
printf("Sum = %d", num1+num2 ) ;
}

Function with Parameters and without Return value

In this type of functions there is data transfer from calling


function to called function (parameters) but there is no data
transfer from called function to calling function (return value).
The execution control jumps from calling function to called function
along with the parameters and executes called function, and finally
comes back to the calling function.

For example, consider the following program.

#include <stdio.h>

void main(){
int num1, num2 ;
void addition(int, int) ; // function declaration

printf("Enter any two integer numbers :\n") ;


scanf("%d%d", &num1, &num2);

addition(num1, num2) ; // function call

getch() ;
}
void addition(int a, int b) // function definition
{
printf("Sum = %d", a+b ) ;
}

Function without Parameters and with Return value

In this type of functions there is no data transfer from calling


function to called function (parameters) but there is data transfer
from called function to calling function (return value). The
execution control jumps from calling function to called function and
executes called function, and finally comes back to the calling
function along with a return value.

For example, consider the following program.

#include <stdio.h>

void main(){
int result ;
int addition() ; // function declaration

result = addition() ; // function call


printf("Sum = %d", result) ;
getch() ;
}
int addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : \n") ;
scanf("%d%d", &num1, &num2);
return (num1+num2) ;

Function with Parameters and with Return value

In this type of functions there is data transfer from calling


function to called function (parameters) and also from called
function to calling function (return value). The execution control
jumps from calling function to called function along with parameters
and executes called function, and finally comes back to the calling
function along with a return value.

For example, consider the following program.

#include <stdio.h>

void main(){
int num1, num2, result ;
int addition(int, int) ; // function declaration

printf("Enter any two integer numbers : \n") ;


scanf("%d%d", &num1, &num2);

result = addition(num1, num2) ; // function call


printf("Sum = %d", result) ;
getch() ;
}
int addition(int a, int b) // function definition
{
return (a+b) ;
}

Exercise

1. Using functions getArea() and getPerimeter(), write a C program


to computer and return the area and perimeter of a rectangle.

2. Using a function, write a C program to capture the age of a


person and display a message ‘You are Young’ if the age is below
18 and a message ‘You are old’ is the age is greater than or
equal to 18.

3. Using a function swap(), write a c program to swap two integer


numbers.

You might also like