Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Functions

The document explains the concept of functions in programming, highlighting their advantages such as code reusability and easier debugging. It details the categories of functions, including predefined and user-defined functions, along with examples of different types of functions based on their arguments and return values. Additionally, it covers parameter passing techniques, specifically call by value and call by address, with accompanying code examples.

Uploaded by

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

Functions

The document explains the concept of functions in programming, highlighting their advantages such as code reusability and easier debugging. It details the categories of functions, including predefined and user-defined functions, along with examples of different types of functions based on their arguments and return values. Additionally, it covers parameter passing techniques, specifically call by value and call by address, with accompanying code examples.

Uploaded by

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

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

Function with argument and without return value


Example 1
#include<stdio.h>
void add(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
add(a,b);
}
void add(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Example 2
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
int a,b,c,d,e;
printf("\nGoing to calculate the average of five numbers:");
printf("\nEnter five numbers:");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five numbers : %.2f",avg);
}

Function with argument and with return value


Example 1
#include<stdio.h>
int add(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = add(a,b);
printf("\nThe sum is : %d",result);
}
int add(int a, int b)
{
return a+b;
}
Example 2
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("\nGoing to check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}

Parameter Passing Techniques


● Call by value
○ The value of the actual parameters is copied into the formal parameters
○ The value of the actual parameter can not be modified by formal
parameters
Example 1
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
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 value 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=100

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

You might also like