Skip to main content

concept and example of function in c

v
/*
Types of functions
1) Predefined standard library functions:-
Standard library functions are also known as built-in functions.
 Functions such as puts(), gets(), printf(), scanf() etc are
  standard library functions

For example, printf() function is defined in <stdio.h>
 header file so in order to use the printf() function,
  we need to include the <stdio.h> header file in our
   program using #include <stdio.h>.

2) User Defined functions
The functions that we create in a program are known
 as user defined functions or in other words you can 
 say that a function created by user is known as
 user defined function.

Now we will learn how to create user defined functions and how to use them in C Programming

Syntax of a function

return_type function_name (argument list)
{
    Set of statements – Block of code
}

return_type: Return type can be of any data type such as int, double, char, void, short etc.

function_name:- It can be anything, however it is advised to have a meaningful name for the 
functions so that it would be easy to understand the purpose of function just by seeing it,s name.

argument list:- Argument list contains variables names along with their data types. 
These arguments are kind of inputs for the function. For example – A function which 
is used to add two integer variables, will be having two integer argument.

Block of code:- Set of C statements, which will be executed whenever a call will 
be made to the function.

Lets take an example :– Suppose you want to create a function to add two integer variables.

Let’s split the problem so that it would be easy to understand –
Function will add the two numbers so it should have some meaningful name like sum,
 addition, etc. For example lets take the name addition for this function.

return_type addition(argument list)
This function addition adds two integer variables, which means I need two integer 
variable as input, lets provide two integer parameters in the function signature. 
The function signature would be :–

return_type addition(int num1, int num2)
The result of the sum of two integers would be integer only. Hence function should 
return an integer value – I got my return type – It would be integer –

int addition(int num1, int num2);

Example1: Creating a user defined function addition()
Function with argument and with return value
*/
#include<stdio.h>
int addition (int a,int b)
{
    return a+b;
}
int main(int argcchar const *argv[])
{
    int a,b,c;
    printf("Enter first number\n");
    scanf("%d",&a);
    printf("Enter second number\n");
    scanf("%d",&b);
    c=addition(a,b);
    printf("The addition of given two number is %d",c);
    
    return 0;
}


/*Example2: Creating a void user defined function that doesn,t return anything
Function with argument and with no return value
*/
#include <stdio.h>
//function return type is void and it doesn't have parameters
void introduction()
{
    printf("Hi\n");
    printf("My name is satyam\n");
    printf("How are you?");
    //There is no return statement inside this function, since itsreturn type is void
     
}

int main()
{
     //calling function
     introduction();
     return 0;
}

// Another examples of with argument ang with no return value
 #include<stdio.h>
 int takenumber()
     {
         int a;
         printf("Enter a number\n");
         scanf("%d",&a); 
         return a;
     }
     int main()
     {
         int c;
         c=takenumber();
         printf("The number you entered ia %d",c);
         return 0;
     }
     
/*Actual parameters:- The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.

For example:-
*/
#include <stdio.h>
int sum(int aint b)
{
     int c=a+b;
     return c;
}

int main(
{
    int var1 =10;
    int var2 = 20;
    int var3 = sum(var1var2);
    printf("%d"var3);

    return 0;
}
 /*In the above example variable a and b are the formal parameters
 (or formal arguments). Variable var1 and var2 are the actual 
 arguments (or actual parameters). The actual parameters can
 also be the values. Like sum(10, 20), here 10 and 20 are actual parameters.

 Example of Function call by Value
 As mentioned above, in the call by value the actual arguments are  
 copied to the formal arguments, hence any operation performed by
 function on arguments doesn’t affect actual parameters.
*/
#include <stdio.h>
int increment(int var)
{
    var = var+1;
    return var;
}

int main()
{
   int num1=20;
   int num2 = increment(num1);
   printf("num1 value is: %d"num1);
   printf("\nnum2 value is: %d"num2);

   return 0;
}

//Example 2: Function Call by Reference – Swapping numbers
#include<stdio.h>
void swapnum ( int *var1int *var2 )
{
   int a ;
   a = *var1 ;
   *var1 =*var2 ;
   *var2 = a ;
}
int main( )
{
   int num1 = 35num2 = 45 ;
   printf("Before swapping:");
   printf("\nnum1 value is %d"num1);
   printf("\nnum2 value is %d"num2);

   /*calling swap function*/
   swapnum( &num1, &num2 );

   printf("\nAfter swapping:");
   printf("\nnum1 value is %d"num1);
   printf("\nnum2 value is %d"num2);
   return 0;
}

/*Different aspects of function calling
A function may or may not accept any argument. 
It may or may not return any value. Based on these facts,
 There are four different aspects of function calls.

1.function without arguments and without return value
2.function without arguments and with return value
3.function with arguments and without return value
4.function with arguments and with return value*/

//Example for Function without argument and without return value
//Example 1

#include<stdio.h>  
void printName();  
void main ()  
{  
    printf("Hello ");  
    printName();  
}  
void printName()  
{  
    printf("Javatpoint");  
}  

//Example 2

#include<stdio.h>  
void sum();  
void main()  
{  
    printf("\nGoing to calculate the sum of two numbers:\n");  
    sum();  
}  
void sum()  
{  
    int a,b;   
    printf("\nEnter two numbers\n");  
    scanf("%d %d",&a,&b);   
    printf("The sum is %d",a+b);  
}  

//Example for Function without argument and with return value
//Example 1

#include<stdio.h>  
int sum();  
void main()  
{  
    int result;   
    printf("\nGoing to calculate the sum of two numbers:");  
    result = sum();  
    printf("%d",result);  
}  
int sum()  
{  
    int a,b;   
    printf("\nEnter two numbers\n");  
    scanf("%d %d",&a,&b);  
    return a+b;   
}  

//Example 2: program to calculate the area of the square

#include<stdio.h>  
int sum();  
void main()  
{  
    printf("Going to calculate the area of the square\n");  
    float area = square (); 
    printf("The area of the square: %f\n",area);  
}  
int square()  
{  
    float side;  
    printf("Enter the length of the side in meters: \n");  
    scanf("%f",&side);  
    return side * side;  
}  


//Example for Function with argument and without return value
//Example 1

#include<stdio.h>  
void sum(intint);  
void main()  
{  
    int a,b,result;   
    printf("Going to calculate the sum of two numbers:\n");  
    printf("Enter two numbers:\n");  
    scanf("%d %d",&a,&b);  
    sum(a,b);  
}  
void sum(int aint b)  
{  
    printf("The sum is %d",a+b);      
}  

//Example 2: program to calculate the average of five numbers.

#include<stdio.h>  
void average(intintintintint);  
void main()  
{  
    int a,b,c,d,e;   
    printf("Going to calculate the average of five numbers:\n");  
    printf("Enter five numbers:\n");  
    scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);  
    average(a,b,c,d,e);  
}  
void average(int aint bint cint dint e)  
{  
    float avg;   
    avg = (a+b+c+d+e)/5;   
    printf("The average of given five numbers : %f",avg);  

//Example for Function with argument and with return value
//Example 1

#include<stdio.h>  
int sum(intint);  
void main()  
{  
    int a,b,result;   
    printf("Going to calculate the sum of two numbers:\n");  
    printf("Enter two numbers:\n");  
    scanf("%d %d",&a,&b);  
    result = sum(a,b);  
    printf("The sum is : %d",result);  
}  
int sum(int aint b)  
{  
    return a+b;  
}  


//Example 2: Program to check whether a number is even or odd

#include<stdio.h>  
int even_odd(int);  
void main()  
{  
 int n,flag=0;  
 printf("Going to check whether a number is even or odd\n");  
 printf("Enter the number:\n ");  
 scanf("%d",&n);  
 flag = even_odd(n);  
 if(flag == 0)  
 {  
    printf("The number is odd\n");  
 }  
 else   
 {  
    printf("The number is even\n");  
 }  
}  
int even_odd(int n)  
{  
    if(n%2 == 0)  
    {  
        return 1;  
    }  
    else   
    {  
        return 0;  
    }  
}  



Comments