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

C part 7 - Function

The document outlines the types of functions in C, distinguishing between library functions and user-defined functions. It explains the advantages of user-defined functions, such as program decomposition and code reuse, and provides examples of function prototypes, definitions, calls, and different argument passing methods (call by value and call by reference). Additionally, it covers recursion and its application in solving mathematical problems like calculating factorials.

Uploaded by

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

C part 7 - Function

The document outlines the types of functions in C, distinguishing between library functions and user-defined functions. It explains the advantages of user-defined functions, such as program decomposition and code reuse, and provides examples of function prototypes, definitions, calls, and different argument passing methods (call by value and call by reference). Additionally, it covers recursion and its application in solving mathematical problems like calculating factorials.

Uploaded by

soriat santo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Types of C functions

 Basically, there are two types of functions in C on basis


of whether it is defined by user or not.
➢ Library function
➢ User defined function
Advantages of user defined functions

 User defined functions helps to decompose the large


program into small segments
 makes programmer easy to understand, maintain.
 If repeated code occurs in a program.
 Function can be used to include those codes and execute
when needed by calling that function.
 Programmer working on large project can divide the
workload by making different functions.
Working of Function
Example of User defined function:
#include <stdio.h>
int add(int a, int b);
int main(){
int num1,num2,result;
printf("User defined function to add two numbers:\n");
printf("\nEnters two number:\n");
scanf("%d %d",&num1,&num2);
result=add(num1,num2);
printf("\nThe result is =%d\n",result);
return 0;
}
int add(int a,int b)
{
int add;
add=a+b;
return add;
}
Function Prototype
 The function prototype declares the input and output
parameters of the function.

 The function prototype has the following syntax:


<type> <function name>(<type list>);

 Example: A function that returns the sum of two


values of an integer is: int add(int a, int b);
Function Definition
A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}

For example: Definition of a function that computes the addition of two


integers:

int add(int a,int b)


{
int add;
add=a+b;
return add;
}
Function Definition (Con’d)
 The function definition can be placed anywhere in the
program after the function prototypes.

 If a function definition is placed in front of main(),


there is no need to include its function prototype
Function Call
 A function call has the following syntax:
<function name>(<argument list>)

Example: result=add(num1,num2);
 The result of a function call is a value of type
<Function type>
Arguments/Parameters
 one-to-one correspondence between the
arguments in a function call and the parameters
in the function definition.
int argument1;
double argument2;

result = functionname(argument1, argument2);

// function definition
Int functionname(int parameter1, double parameter2){
// Now the function can use the two parameters
// parameter1 = argument 1, parameter2 = argument2
Call by Value
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.
#include<stdio.h>
int swap(int , int);
main( )
{
int a = 10, b = 20 ;
printf ( "Before swapping:\na = %d b = %d", a, b ) ;
swap(a,b);
}
int swap( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "\nAfter swapping:\na = %d b = %d", x, y ) ;
}
Call by Reference
The call by reference method of passing arguments to a function
copies the address of an argument into the formal parameter.

#include <stdio.h>
void swap(int *a,int *b);
int main(){
int num1=500,num2=100;
swap(&num1,&num2);
printf("Number1 = %d\n",num1);
printf("Number2 = %d",num2);
return 0;
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
Recursion
 Recursion is a function which calls itself.
 Recursive function are very useful to solve many
mathematical problems like to calculate factorial of a
number, generating Fibonacci series, etc.
 But while using recursion, programmers need to be careful to
define an exit condition from the function, otherwise it will
go in infinite loop.

void recursion()
{
recursion(); // function calls itself
}
int main()
{
recursion();
}
Factorial using Recursion
#include<stdio.h>
int fact(int);
int main()
{
int a,f;
scanf("%d",&a);
f=fact(a);
printf("%d",f);
return 0;
}

int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}

You might also like