Functions
Functions
Block of code to accomplish a computing task written once and called any number of
times
Advantages
● Code reusability
● Easier debugging
Categories
● Predefined (library) functions
○ printf(), scanf(), sqrt(), pow(), gets(), puts(), ceil(), floor() …..
User Defined Functions (UDF)
These are the functions created by programmers
There are three aspects of UDF
● Function declaration
○ Like variables, a function needs to be declared before its called
○ return_type function_name (parameter_ list);
● Function call
○ A function can be call from anywhere in a program. A parameter list
(optional) makes the functions more powerful
○ function_name (paramete_list)
● Function definition
○ Contains the instructions to be executed as per the function call.
○ Remember, only one entity can be returned at a time from a function
return_type function_name (argument list)
{
Function body ;
{
Function without argument and without return value
Example 1
#include <stdio.h>
void hello(); // function declaration
int main()
{
hello(); // function call
return 0;
}
void hello() // function definition
{
printf("Hello, welcome to functions ");
return;
}
Example 2
#include<stdio.h>
void add();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
add();
}
void add()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Function without argument and with return value
Example 1
#include<stdio.h>
int add();
void main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = add();
printf("%d",result);
}
int add()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Example 2
#include<stdio.h>
int square();
void main()
{
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %.2f\n",area);
}
int square()
{
float side;
printf("Enter the length of the side in metres: ");
scanf("%f",&side);
return side * side;
}
Example 2
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
// printing the value of a and b in main
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
// The value of actual parameters do not change by changing the
formal parameters //in call by value, a = 10, b = 20
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
// Formal parameters, a = 20, b = 10
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
● Call by Address
○ The address of the variable is passed into the function call as the actual
parameter.
○ The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is
passed.
Example 1
#include<stdio.h>
void change(int *num)
{
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Example 2
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
// printing the value of a and b in main
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
// The values of actual parameters do change in call by reference, a
=
10, b = 20
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// Formal parameters, a = 20, b = 10
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10