0% found this document useful (0 votes)
77 views

CSE101 Function

Uploaded by

Vedanth Pradhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

CSE101 Function

Uploaded by

Vedanth Pradhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Function

By
Pundreekaksha Sharma
Assistant Professor

©LPU CSE101 C Programming


C Functions
• 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.

©LPU CSE101 C Programming


Advantage of functions in C
• 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.

©LPU CSE101 C Programming


• 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.

©LPU CSE101 C Programming


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

©LPU CSE101 C Programming


The syntax of creating function in c language is
given below:
return_type function_name(data_type
parameter...)
{
//code to be executed
}

©LPU CSE101 C Programming


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.

©LPU CSE101 C Programming


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.

Example without return value:


void hello(){
   printf("hello c");  

Example of with return value:


int get(){  
return 10;  
}  

©LPU CSE101 C Programming


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

©LPU CSE101 C Programming


Example for Function without argument and
return value
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf(“Hello World");
}
©LPU CSE101 C Programming
Example for Function without argument and
with return value
#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");  
    scanf("%d %d",&a,&b);  
    return a+b;   

©LPU CSE101 C Programming


Example for Function with argument and
without return value
#include<stdio.h>  
void sum(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);  
    sum(a,b);  
}  
void sum(int a, int b)  
{  
    printf("\nThe sum is %d",a+b);      
}  

©LPU CSE101 C Programming


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.

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.

©LPU CSE101 C Programming


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.

©LPU CSE101 C Programming

You might also like