Function in c

A function is self-contained group or block of statement to perform a specific operation. In Every C program by default function is main function. You can create multiple function for multiple logics. How you create function it's up to you.

Different part of functions

To write function in C language you have to follow 3 rules.

Function Declaration

A function declaration tells the compiler about a function's name, return type, and parameters.

Function Definition

Function definition contain logics of the program. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.

Example of function definition:


    return_type function_name(parameter optional)
    {
    body of statement
    }
	

Return Type

A function may return a value or may not. It depend on function data type. Whether you are using int or void. Here void data type return nothing and int data type return 0.

Function Name

This is the actual name of function. Like writing main it is a function name.

Parameters

Parameter are optional. It's up to you whether it required or not.

Function are two types in c language.

Predefine function:

Predefine function also called inbuilt function. Which are reserved in program.

Predefine function Use of Predefine function
printf(); It is use to display or print the output to the console.
scanf(); It is use to scan or accept the input from keywords.
clrscr(); Use to clear the output screen.
getch(); It holds the output screen until a key is pressed.

Program of Predefine function

	
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    clrscr();
    
    printf("Welcome to Campuslife");
    
    getch();
    }
    

Different way to write the printf() function

Below is the format of printf() function. Only double quote is mandatory in printf() function. Rest thing is optional or based on program requirement.

	
    printf("control string with format specification",argument1, argument2....argument n);
     

In below example Hello is a message and it will show on the console screen when you execute your program.

Example 1: predefine function with control string.

	
    printf("Hello"); //Output will show as Hello.
    

Example 2: predefine function with format specification.

	
    int a = 10;
    printf("%d",a); //Output will show as 10.
    

Example 3: predefine function with control string, with format specification and with argument.

	
    int a = 10;
    printf("The result is %d",a); //Output will show as The result is 10
    

The result is

In Example 3 (The result is) is a control string.

%d

In Example 3 (%d) is a placeholder nothing but format specification of integer value. To display the value we need to hold the some place for that.

"a"

In Example 3 "a" variable is an argument.

Different format specification for data to show on screen.

Data type format specification/place holder
short/int %d
long %ld
float %f
double %lf
char %c
string %s
unsigned %u
octal %o
hexadecimal %x

User Define Function

User define function are those function which is define by the user not by the program. In below example we have created two function that are define by our self.

Example of user define function:

	
    //user define function 1
    void sum()
    {
    int a=10,b=20,c;
    c=a+b;
    printf("Result is %d",c);
    }
    
    //calling the user define function in main function.
    void main()
    {
    clrscr();
    sum();
    gethc();
    }
    

Post Your Comment