Function

 

A function is a self-contained block of statements that performs a specified task. The specified task is repeated each time that the program calls the function. Functions break large computing tasks into smaller ones. They work together to accomplish the goal of the whole program. Every program must contain one function named main() where the program always begin execution.

 

The following are the advantages of functions :

  1. The length of a source program can be reduced using functions at appropriate places.
  2. It is easy to locate and isolate a faulty function.
  3. A function may be used by many other programs.

 

FUNCTION DEFINITION

A function definition introduces a new function by declaring the type of value it returns, its parameters and specifying the statements that are executed when the function is called. The syntax of the function definition is :

return_type function_name(argument_list)

             argument_declarations ;

             {

                             local_variable_declarations ;

                             statement_1 ;

                             statement_2 ;

                             .               .               .

                             .               .               .

                             .               .               .

                             statement_n ;

                             return(expression) ;

             }

 

Where the return_type specifies the data type of the value to be returned by the function. The data type is assumed to be of type int by default if it is not specified.

The function_name is used to identify the function. The rules for naming a function are same as the variable names.

The argument_list and its associated argument_delclarations parts are optional. The argument list contains valid variable names separated by comma and the list must be surrounded by parenthesis. Semicolons are not allowed after the closing parenthesis. The argument variables receive values from the calling function, thus providing a means for data communication from the calling function to the called function. All argument variables must be declared for their types before the opening brace of the function body.

The declaration of local variables is required only when any local variables are used in the function.

A function can have any number of executable statements.

The return statement is the mechanism for returning a value to the calling function. This is also an optional statement. Its absence indicates that no value is being returned to the calling function.

 

 Library functions user defined functions

C functions can be classified in two categories: Library functions and User defined  functions. The difference between this two functions is, Library functions are already built in and we need not to write its code, whereas a User defined function has to be written to get it executed during the output. However, later the user defined functions can become a part of C library. In fact, this is one of the strength of C language.

 

Function prototype

 

Any C function returns n integer value by default. Whenever a call is made to a function, the compiler assumes that this function would return a value of type int. If we desire that a function should return a value other than an int, then it is necessary to mention the calling function in the called function, which is called as the function prototype.

Function prototype are usually written at the beginning of the program explicitly before all user defined functions including the main() function. The syntax is :

return_type function_name(dt1 arg1, dt2 arg2, . . . dtn argn) ;

Where return_type represents the data type of the value that is returned by the function and dt1, dt2, . . . dtn represents the data type of the arguments arg1, arg2, . . . argn. The data types of the actual arguments must confirm to the data types of the arguments with the prototype. For example :

            lonf fact(long num) ;

Here fact is the name of the function, long before the function name fact indicates that the function returns a value of type long. num inside the parenthesis is the parameter passed to the called function. long before the num indicates that it is of type long.

 

 

 

 

 

 

 

 

types of user defined function

 

 

 

For better understanding of arguments and return in functions, user-defined functions can be categorised as:

  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.

Let’s take an example to find whether a number is prime or not using above 4 categories of user defined functions.

 

 

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);

}

Function prime() is used for asking user a input, check for whether it is prime of not and display it accordingly. No argument is passed and returned form prime() function.

 

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;

num=input(); /* No argument is passed to input() */

for(i=2,flag=i;i<=num/2;++i,flag=i)

{

if(num%i==0)

{

printf(“%d is not prime”,num);

++flag;

break;

}

}

 

if(flag==i)

printf(“%d is prime”,num);

return 0;

}

 

int input()

{

/* Integer value is returned from input() to calling function */

int n;

printf(“Enter positive enter to check:\n”);

scanf(“%d”,&n); return n;

}

There is no argument passed to input() function But, the value of n is returned from input() to main() function.

 

 

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;

 

for(i=2,flag=i;i<=n/2;++i,flag=i)

{

if(n%i==0)

{

printf(“%d is not prime”,n);

++flag;

break;

}

}

if(flag==i)

printf(“%d is prime”,n);

}

 

Here, check_display() function is used for check whether it is prime or not and display it accordingly. Here, argument is passed to user-defined function but, value is not returned from it to calling function.

 

 

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 in 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;

}

 

Here, check() function is used for checking whether a number is prime or not. In this program, input from user is passed to function check() and integer value is returned from it. If input the number is prime, 0 is returned and if number is not prime, 1 is returned.

 

 

 

function with arrays

 

In C programming, a single array element or an entire array can be passed to a function. Also, both one-dimensional and multi-dimensional array can be passed to function as argument.

Passing One-dimensional Array In Function

 

#include <stdio.h>

void display(int a)

{

printf(“%d”,a);

}

 

int main()

{

int c[]={2,3,4};

display(c[2]); //Passing array element c[2] only.

return 0;

}

 

Output : 4

 

Single element of an array can be passed in similar manner as passing variable to a function.

 

 

Passing entire one-dimensional array to a function

While passing arrays to the argument, the name of the array is passed as an argument(,i.e, starting address of memory area is passed as argument).

#include <stdio.h>

float average(float a[]);

int main(){

float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};

avg=average(c); /* Only name of array is passed as argument. */ printf(“Average age=%.2f”,avg);

return 0;

}

float average(float a[])

{

int i; float avg, sum=0.0;

for(i=0;i<6;++i){ sum+=a[i]; } avg =(sum/6);

return avg;

}

Output : Average age = 27.08

 

 

function with structure.

 

Of course a sensible alternative to writing out the addition each time is to define a function to do the same job – but this raises the question of passing structures as parameters. Fortunately this isn’t a big problem. Most C compilers, will allow you to pass entire structures as parameters and return entire structures. As with all C parameters structures are passed by value and so if you want to allow a function to alter a parameter you have to remember to pass a pointer to a struct. Given that you can pass and return structs the function is fairly easy:

struct comp add(struct comp a , struct comp b)

{

struct comp c;

c.real=a.real+b.real;

c.imag=a.imag+ b.imag;

return c;

}

After you have defined the add function you can write a complex addition as:

x=add(y,z)

which isn’t too far from the x=y+z that you would really like to use. Finally notice that passing a struct by value might use up rather a lot of memory as a complete copy of the structure is made for the function.

 

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback