0% found this document useful (0 votes)
16 views13 pages

23D21A0413&457 - PPS - Assignment 1

The document provides an overview of functions in C programming, detailing their structure, including function declaration, definition, and calls. It highlights the importance of functions for code reusability and modularity, and distinguishes between library functions and user-defined functions. Additionally, it lists common C header files that contain various library functions.
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)
16 views13 pages

23D21A0413&457 - PPS - Assignment 1

The document provides an overview of functions in C programming, detailing their structure, including function declaration, definition, and calls. It highlights the importance of functions for code reusability and modularity, and distinguishes between library functions and user-defined functions. Additionally, it lists common C header files that contain various library functions.
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/ 13

FUNCTIONS IN C

INTRODUCTION :
A function in C is a set of statements that when called
perform some specific task. It is the basic building block
of a C program that provides modularity and code
reusability, having certain meanings and performing
certain operations.
Syntax for a function:
Return-type function-name (Argument)
Here, the return-type specifies the data type of the value that the
function will return.
FUNCTION ASPECTS:
The aspects of function can be divided into 3 aspects:
1.Function Declaration
2.Function Definition
3.Function Calls
1. Function Declarations
In a function declaration, we must provide the function
name, its return type, and the number and type of
its arguments. A function declaration tells the compiler
that there is a function with the given name defined
somewhere else in the program.
int myFunction()
int main()
{
printf(“Hi I’m in main”)

myFunction(); // call the function


}
int myFunction()
{
printf("I just got executed!");
}
2.Function Definition
The function definition consists of actual statements
which are executed when the function is called (i.e.
when the program control comes to the function).A
C function is generally defined and declared in a
single step because the function definition always
starts with the function declaration so we do not
need to declare it explicitly.
3.Function Call
A function call is a statement that instructs the compiler to execute the
function.
We use the function name and arguments in the function call.
•Uses of function in C:-
•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.
There are different ways of writing a function in C;
•1.Function name with return type with
argument
Eg; #include<stdio.h>
int 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);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
2.Function name with return type and without
argument
#include<stdio.h>
Ex: int sum();
void main()
{
int result;
printf("Going to calculate the
sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
3.Function name without return type with argument
• Ex: #include<stdio.h>
void sum(int a, int b);
void main()
{
int a,b,result;
printf("Going to calculate the sum of tw
o numbers:");
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("The sum is %d",a+b);
}
4.Function name without return type without argument
• Ex: #include<stdio.h
>
void printName()
;
void main ()
{
printf("Hello ")
;
printName();
}
void printName()

{
printf("JESSIE")
;
}
TYPES OF FUNCTIONS in C

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy