2. Functions
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(), and all the most trivial programs can
define additional functions.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
Defining a Function:
return_type function_name( parameter list )
{
body of the function
}
3. (a) C program is a collection of one or more functions.
Some important case
(b)A function gets called when the function name is followed by a semicolon. For example,
main( )
{
argentina( ) ;
}
(c)A function is defined when function name is followed by a pair of braces in which one or
more statements may be present. For example,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
4. (d) Any function can be called from any other function. Even main( ) can be called from other
functions. For example,
main( )
{
message( ) ;
}
message( )
{
printf ( "nCan't imagine life without C" ) ;
main( ) ;
}
(e)A function can be called any number of times. For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "nJewel Thief!!" ) ;
}
5. (f) The order in which the functions are defined in a program and the order in which they get
called need not necessarily be same. For example,
main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf ( "nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "nMary bought some butter" ) ;
}
Here, even though message1( ) is getting called before message2( ), still, message1( ) has been defined
after message2( ). However, it is advisable to define the functions in the same order in which they are called.
This makes the program easier to understand.
6. (g) A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect
of C functions later in this chapter.
(h) A function can be called from other function, but a function cannot be defined in another function.
Thus, the following program code would be wrong, since argentina( ) is being defined inside
another function, main( ).
main( )
{
printf ( "nI am in main" ) ;
argentina( )
{
printf ( "nI am in argentina" ) ;
}
}
7. Types of C functions
Library function User defined function
Library function 1. main()
2. printf()
3. scanf()
User defined function #include <stdio.h>
void function_name(){
................
................
}
int main(){
...........
...........
function_name();
...........
...........
}
8. Types of User-defined Functions in C Programming
1. Function with no arguments and no return value
2. Function with no arguments and return value
3. Function with arguments but no return value
4. Function with arguments and return value.
Function with no arguments and no return value.
/*C program to check whether a number entered by user is prime or not using function with no arguments
and no return value*/
#include <stdio.h>
void prime();
int main(){
prime(); //No argument is passed to prime().
return 0;
}
void prime(){
/* There is no return value to calling function main(). Hence, return type of prime() is void */
int num,i,flag=0;
printf("Enter positive integer enter to check:n");
scanf("%d",&num);
for(i=2;i<=num/2;++i){
if(num%i==0){
flag=1;
}
}
if (flag==1)
printf("%d is not prime",num);
else
printf("%d is prime",num);
}
9. Function with no arguments but return value
/*C program to check whether a number entered by user is prime or not using function
with no arguments but having return value */
#include <stdio.h>
int input();
int main(){
int num,i,flag = 0;
num=input(); /* No argument is passed to input() */
for(i=2; i<=num/2; ++i){
if(num%i==0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not prime",num);
else
printf("%d is prime", num);
return 0;
}
int input(){ /* Integer value is returned from input() to calling function */
int n;
printf("Enter positive integer to check:n");
scanf("%d",&n);
return n;
}
10. Function with arguments and no return value
/*Program to check whether a number entered by user is prime or
not using function with arguments and no return value */
#include <stdio.h>
void check_display(int n);
int main(){
int num;
printf("Enter positive enter to check:n");
scanf("%d",&num);
check_display(num); /* Argument num is passed to function. */
return 0;
}
void check_display(int n){
/* There is no return value to calling function. Hence, return type of
function is void. */
int i, flag = 0;
for(i=2; i<=n/2; ++i){
if(n%i==0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not prime",n);
else
printf("%d is prime", n);
}
11. Function with argument and a return value
/* Program to check whether a number entered by user is prime or not
using function with argument and return value */
#include <stdio.h>
int check(int n);
int main(){
int num,num_check=0;
printf("Enter positive enter to check:n");
scanf("%d",&num);
num_check=check(num); /* Argument num is passed to check() function. */
if(num_check==1)
printf("%d is not prime",num);
else
printf("%d is prime",num);
return 0;
}
int check(int n){
/* Integer value is returned from function check() */
int i;
for(i=2;i<=n/2;++i){
if(n%i==0)
return 1;
}
return 0;
}
13. Example:
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
15. #include<stdio.h>
void main()
{
int c;
c=max(2,3);
printf("n this print is in main() %d n",c);
}
/* function returning the max between two numbers
*/
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
printf("n I am in max() %d n",result);
return result;
}
Example:
16. Example of
a Function:
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %dn", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
19. Function Arguments
Call Type Description
Call by value This method copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
Call by reference This method copies the address of an argument
into the formal parameter. Inside the function, the
address is used to access the actual argument used
in the call. This means that changes made to the
parameter affect the argument.
20. call by value in C
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument
By default, C programming language uses call by value method to pass arguments.
In general, this means that code within a function cannot alter the arguments used
to call the function. Consider the function swap() definition as follows.
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
21. #include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0;
}
22. call by reference in C
The call by reference method of passing arguments to a function copies the
address of an argument into the formal parameter. Inside the function, the address is
used to access the actual argument used in the call. This means that changes made to
the parameter affect the passed argument.
/* function definition to swap the values */
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
return;
}
23. #include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0;
}