Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

C Functions ❮ Edit Details

In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.

Advantage of functions in C

There are the following advantages of C functions.

  • By using functions, we can avoid rewriting same logic/code again and again in a program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C functions.
  • However, Function calling is always a overhead in a C program.

Function Aspects

There are three aspects of a C function.

  • Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.

  • Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

  • Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.
SN C function aspects Syntax
1 Function declaration return_type function_name (argument list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument list) {function body;}

The syntax of creating function in c language is given below:

return_type 

  1. function_name(data_type parameter...){  
  2. //code to be executed  
  3. }  

Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

C Function

Return Value

A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from the function.

Example without return value:

  1. void hello(){  
  2. printf("hello c");  
  3. }  

If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function.

Let's see a simple example of C function that returns int value from the function.

Example with return value:

  1. int get(){  
  2. return 10;  
  3. }  

In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.

  1. float get(){  
  2. return 10.2;  
  3. }  

Now, you need to call the function, to get the value of the function.

 

 

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.

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

Example for Function without argument and return value

Example 1

  1. #include<stdio.h>  
  2. void printName();  
  3. void main ()  
  4. {  
  5.     printf("Hello ");  
  6.     printName();  
  7. }  
  8. void printName()  
  9. {  
  10.     printf("Javatpoint");  
  11. }  
  12. Output

    Hello Javatpoint
    

    Example 2

  1. #include<stdio.h>  
  2. void sum();  
  3. void main()  
  4. {  
  5.     printf("\nGoing to calculate the sum of two numbers:");  
  6.     sum();  
  7. }  
  8. void sum()  
  9. {  
  10.     int a,b;   
  11.     printf("\nEnter two numbers");  
  12.     scanf("%d %d",&a,&b);   
  13.     printf("The sum is %d",a+b);  
  14. }  

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

Example for Function without argument and with return value

Example 1

  1. #include<stdio.h>  
  2. int sum();  
  3. void main()  
  4. {  
  5.     int result;   
  6.     printf("\nGoing to calculate the sum of two numbers:");  
  7.     result = sum();  
  8.     printf("%d",result);  
  9. }  
  10. int sum()  
  11. {  
  12.     int a,b;   
  13.     printf("\nEnter two numbers");  
  14.     scanf("%d %d",&a,&b);  
  15.     return a+b;   
  16. }  

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

Example 2: program to calculate the area of the square

  1. #include<stdio.h>  
  2. int sum();  
  3. void main()  
  4. {  
  5.     printf("Going to calculate the area of the square\n");  
  6.     float area = square();  
  7.     printf("The area of the square: %f\n",area);  
  8. }  
  9. int square()  
  10. {  
  11.     float side;  
  12.     printf("Enter the length of the side in meters: ");  
  13.     scanf("%f",&side);  
  14.     return side * side;  
  15. }  

Output

Going to calculate the area of the square 
Enter the length of the side in meters: 10 
The area of the square: 100.000000

Example for Function with argument and without return value

Example 1

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

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

Output

Going to calculate the averag
  1. #include<stdio.h>  
  2. void sum(int, int);  
  3. void main()  
  4. {  
  5.     int a,b,result;   
  6.     printf("\nGoing to calculate the sum of two numbers:");  
  7.     printf("\nEnter two numbers:");  
  8.     scanf("%d %d",&a,&b);  
  9.     sum(a,b);  
  10. }  
  11. void sum(int a, int b)  
  12. {  
  13.     printf("\nThe sum is %d",a+b);      
  14. }  
  15. #include<stdio.h>  
  16. void average(int, int, int, int, int);  
  17. void main()  
  18. {  
  19.     int a,b,c,d,e;   
  20.     printf("\nGoing to calculate the average of five numbers:");  
  21.     printf("\nEnter five numbers:");  
  22.     scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);  
  23.     average(a,b,c,d,e);  
  24. }  
  25. void average(int a, int b, int c, int d, int e)  
  26. {  
  27.     float avg;   
  28.     avg = (a+b+c+d+e)/5;   
  29.     printf("The average of given five numbers : %f",avg);  
  30. }  
e of five numbers:
Enter five numbers:10 
20
30
40
50
The average of given five numbers : 30.000000

Example for Function with argument and with return value

Example 1

#include<stdio.h>   int sum(int, int);   void main()  

Output

Going to calculate the sum of two numbers:
Enter two numbers:10
20 
The sum is : 30   

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("\nGoing to check whether a number is even or odd");    printf("\nEnter the number: ");  

Output

Going to check whether a number is even or odd
Enter the number: 100
The number is even

C Library Functions

Library functions are the inbuilt function in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. For example, printf is a library function used to print on the console. The library functions are created by the designers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. For example, To use the library functions such as printf/scanf we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output.

The list of mostly used header files is given in the following table.

SN Header file Description
1 stdio.h This is a standard input/output header file. It contains all the library functions regarding standard input/output.
2 conio.h This is a console input/output header file.
3 string.h It contains all string related library functions like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library functions like malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions like sqrt(), pow(), etc.
6 time.h This header file contains all the time-related functions.
7 ctype.h This header file contains all character handling functions.
8 stdarg.h Variable argument functions are defined in this header file.
9 signal.h All the signal handling functions are defined in this header file.
10 setjmp.h This file contains all the jump functions.
11 locale.h This file contains locale functions.
12 errno.h This file contains error handling functions.
13 assert.h

This file contains diagnostics functions.

 

 
 
  1.  scanf("%d",&n);  
  2.  flag = even_odd(n);  
  3.  if(flag == 0)  
  4.  {  
  5.     printf("\nThe number is odd");  
  6.  }  
  7.  else   
  8.  {  
  9.     printf("\nThe number is even");  
  10.  }  
  11. }  
  12. int even_odd(int n)  
  13. {  
  14.     if(n%2 == 0)  
  15.     {  
  16.         return 1;  
  17.     }  
  18.     else   
  19.     {  
  20.         return 0;  
  21.     }  
  22. }  
  23. {  
  24.     int a,b,result;   
  25.     printf("\nGoing to calculate the sum of two numbers:");  
  26.     printf("\nEnter two numbers:");  
  27.     scanf("%d %d",&a,&b);  
  28.     result = sum(a,b);  
  29.     printf("\nThe sum is : %d",result);  
  30. }  
  31. int sum(int a, int b)  
  32. {  
  33.     return a+b;  
  34. }