What is a function?
A function is defined as self-contained block of statements that performs a particular specified job in a program.
C functions can be classified into two categories: user-defined and library functions.
Library Functions
These are the functions which are already written, compiled and placed in C Library and they are not required to be written by a programmer again. The functions name, its return type, their argument number and types have been already defined. We can use these functions as required by just calling them. For example: print f(), scan f() are examples, of library functions. the library functions are also known as built in function and they are defined within a particular header file.
User-defined Functions
These are the functions which are defined by user at the time of writing a program. The user has choice to choose its name, return type, arguments and their types. the function factorial() is user defined function.
Function with no Arguments and no return values
void function_ name() { /* body of function*/ }
Example
Write a program to illustrate the “functions with no arguments and no return values”
#include<stdio.h> #include<conio.h> void addition() { int a, b , sum; print f("Enter two numbers:"); scan f("%d%d", &a, &b); sum= a+b; print f(\n The sum is %d", sum); } int main() { addition(); getch(); return 0; } Output Enter two numbers: 45 90 The sum is 135
Functions with Arguments but no Return Value
void function_ name(arguments list) { body of function }
Example
Write a program to illustrate the “function with arguments but no return values”.
#include<stdio.h> #include<conio.h> void addition(int a, int b) { int sum; sum= a + b; print f ("The sum is %d", sum); } int main() { print f (Enter two numbers:\t"); scan f("%d%d", &a, &b); addition(a,b); getch(); return 0; } Output Enter two numbers: 50 40 The sum is 90
Function with arguments and return values
return_type function_name(arguments list) { body of the function;\ return value; }
Example
Write a program to illustrate the “functions with arguments and return values”.
#include<stdio.h> #include<conio.h> int addition(int a, int b) { int sum; sum = a + b; return sum; } int main () { int a ,b ,sum ; print f("Enter two numbers"); scan f("%d%d", &a, &b); sum= addition(a, b); print f("The sum is %d", sum); getch(); return 0; } Output Enter two numbers: 78 56 The sum is 134
Different Types of Function Calls
The arguments in function can be passed in two ways: pass arguments by value and pass arguments by address or reference or pointers.
Function call by value( or pass arguments by value)
When values of actual arguments are passed to a function as arguments, it is known as function call by value.
function_ name( value_ of_ arguments, value_ of_ arguments2,..);
Example
Write a program to illustrate function call by value.
#include<stdio.h> #include<conio.h> void swap(int, int); int main(0 { int a, b; a= 99; b=89; print f("Before function calling, a and b are:%d\t%d", a, b); swap(a, b); print f("After function calling, a and b are;: %d\%d", a,b); getch(); return 0; } void swap(int x, int y) { int temp; temp = x; x=y; y=temp; print f(" The values within functions are:%d\t%d, x,y); Output Before functions calling, a and b are: 99 89 The values within functions are: 89 99 After function calling, a and b are: 99 89
Function Call by Reference( Pass arguments by Address)
The address of a variable or arguments is passed to a function as arguments instead of actual value of variable. A function can be called by passing address in its arguments as:
function_ name(address_ of_ argument1, address_ of_ argument2,.....);
Example
Write a program to swap the value of two variables using call by reference.
#include<stdio.h> #include<coino.h> void swap (int *, int *); int main () { int a, b; a= 99; b= 89; print f("Before swap, a and b are: %d\t %d", a, b); getch(); return 0; } void swap(int *x, int *y) { int temp; temp= *x; *x= *y; *y= temp; } Output: Before swap, a and b are: 99 89 After swap, a and b are: 89 99
Recursive Function
A recursive function is one that calls to itself to solve a smaller version of its task until a final call which does not require a self call. Thus s function is known as recursive function if it calls to itself.
Example
Write a program to generate the following Fibonacci series using recursive method.
#include<stdio.h> #include<coino.h> int fib (int n) { if (n==1) return o; else if (n==2) return 1; else return(fib(n-1) + fib(n-2) ); } int main { int terms,i; print f("How many terms do we need?"); scan f("%d", &terms); for(i=1; i<=terms; i++) print f("%d" , fib(i)); getch (); return o; } Output How many terms do we need? 10 0 1 1 2 3 5 8 13 21 34