Presentation on Function in C Programming

Dhaka International University
1
Presentation Name: Function in C Language
Presented To
Mr. Tahzib Ul Islam
Assistant Professor of CSE
Dhaka International University
Presented By
Group: Quiet Fools
Name: Shuvongkor Barman Roll: 38
Name: Minhaj Uddin Roll: 22
Name: Mostofa Kamal Roll: 20
Batch: 59 (Eve)
Department: CSE
2
1. What is C function?
2.Types of C functions
3. Uses of C functions
4. Advantage of C functions
5. C function declaration, function call and definition
6. Simple Example Program for C Function
7. How to call C functions in a program?
8. Overview
Outline
3
1.What is C Function?
In short: A function is a group of statements that together
perform a task.
C functions are basic building blocks in a program.
Long Answer: A large C program is divided into basic
building blocks called C function. C function contains set of
instructions enclosed by “{ }” which performs specific
operation in a C program. Actually, Collection of these
functions creates a C program
Classification Of
Function
Library
function
User define
function
- main()
-printf()
-scanf()
-sqrt()
-getchar()
4A large program in c can be divided to many subprogram
The subprogram posses a self contain components and have well define
purpose.
The subprogram is called as a function
Basically a job of function is to do something
C program contain at least one function which is main().
2. Types of C functions
5
Standard Library Functions:
Library functions in C language are inbuilt functions which are grouped together and placed in a
common place called library. Each library function in C performs specific operation.
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions
are available for use. For example:
➤ The printf() is a standard library function to send formatted output to the screen (display
output on the screen). This function is defined in "stdio.h" header file.
➤ There are other numerous library functions defined under "stdio.h", such as scanf(), printf(),
getchar() etc. Once you include "stdio.h" in your program, all these functions are available for
use.
User-defined Functions:
As mentioned earlier, C allow programmers to define functions. Such functions created by
the user are called user-defined functions.
Depending upon the complexity and requirement of the program, you can create as many
user-defined functions as you want.
6
 C functions are used to avoid rewriting same logic/code again
and again in a program.
 There is no limit in calling C functions to make use of same
functionality wherever required.
 We can call functions any number of times in a program and
from any place in a program.
 A large C program can easily be tracked when it is divided into
functions.
 The core concept of C functions are, re-usability, dividing a big
task into small pieces to achieve the functionality and to
improve understandability of very large C programs.
3. Uses of C functions:
It is much easier to write a structured program where a large
program can be divided into a smaller, simpler task.
Allowing the code to be called many times
Easier to read and update
It is easier to debug a structured program where there error is
easy to find and fix
7
8
C functions aspects syntax
function definition
Return_type function_name
(arguments list)
{ Body of function; }
function call function_name (arguments list);
function declaration
return_type function_name
(argument list);
There are 3 aspects in each C function. They are,
 Function declaration or prototype – this informs compiler about the
function name, function parameters and return value’s data type.
 Function call - this calls the actual function
 Function definition – this contains all the statements to be executed.
5. C function declaration, function call and function definition:
9 As you know, functions should be declared and defined before calling in a C program.
 In the below program, function “square” is called from main function.
 The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function and
multiplied value “p” is returned to main function from function “square”.
6. Simple Example Program for C Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "nEnter some number for finding square n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "nSquare of the given number %f is %f",m,n );
}
float square ( float x ) // function definition
{
float p ;
p = x * x ;
return ( p ) ;
}
Enter some number for finding square 2
Square of the given number 2.000000 is
4.000000
Output:
Enter some number for finding square 4
Square of the given number 2.000000 is
16.000000
10#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
/* local variable definition */
int a = 200;
int b = 800;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %dn", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
We have kept max() along with main() and
compiled the source code. While running the
final executable, it would produce the
following result −
Max value is : 800
Output:
1: #include <stdio.h>
2:
3: long cube(long x);
4:
5: long input, answer;
6:
7: int main( void )
8: {
9: printf(“Enter an integer value: “);
10: scanf(“%d”, &input);
11: answer = cube(input);
12: printf(“nThe cube of %ld is %ld.n”, input, answer);
13:
14: return 0;
15: }
16:
17: long cube(long x)
18: {
19: long x_cubed;
20:
21: x_cubed = x * x * x;
22: return x_cubed;
23: }
 Function names is
cube
 Variable that are
requires is long
 The variable to be
passed on is X(has
single arguments)—
value can be passed
to function so it can
perform the specific
task. It is called
Output
Enter an integer value: 4
The cube of 4 is 64.
Return data type
Arguments/formal parameter
Actual parameters
11
127. How To Call C Functions In A Program?
There are two ways that a C function can be called from a program.
They are,
1.Call by value
2.Call by reference
1. Call By Value:
•In call by value method, the value of the variable is passed to the function as
parameter.
•The value of the actual parameter can not be modified by formal parameter.
•Different Memory is allocated for both actual and formal parameters. Because,
value of actual parameter is copied to formal parameter.
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d nand n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" nvalues after swap m = %dn and n = %d", a, b);
}
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22
 In this program, the
values of the
variables “m” and “n”
are passed to the
function “swap”.
 These values are
copied to formal
parameters “a” and
“b” in swap function
and used.
Example Program For C Function (Using Call By Value):
14
2. Call by reference:
•In call by reference method, the
address of the variable is passed
to the function as parameter.
•The value of the actual parameter
can be modified by formal
parameter.
•Same memory is used for both
actual and formal parameters since
only address is used by both
parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf("values before swap m = %d n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("n values after swap a = %d nand b = %d", *a, *b);
}
Example Program For C Function
(Using Call By Reference):
•In this program, the address of the
variables “m” and “n” are passed to
the function “swap”.
•These values are not copied to
formal parameters “a” and “b” in
swap function.
•Because, they are just holding the
address of those variables.
•This address is used to access and
change the values of the variables.
values before swap m = 22
and n = 44
values after swap a = 44
and b = 22
Output:
15Overview
C functions are basic building blocks in every C program.
There are 2 types of functions in C. They are, 1. Library functions 2. User defined functions
Key points to remember while writing functions in C language:
 All C programs contain main() function which is mandatory.
 main() function is the function from where every C program is started to execute.
 Name of the function is unique in a C program.
 C Functions can be invoked from anywhere within a C program.
 There can any number of functions be created in a program. There is no limit on this.
 There is no limit in calling C functions in a program.
 All functions are called in sequence manner specified in main() function.
 One function can be called within another function.
 C functions can be called with or without arguments/parameters. These arguments are nothing but
inputs to the functions.
16
 C functions may or may not return values to calling functions. These values are
nothing but output of the functions.
 When a function completes its task, program control is returned to the function
from where it is called.
 There can be functions within functions.
 Before calling and defining a function, we have to declare function prototype in
order to inform the compiler about the function name, function parameters and
return value type.
 C function can return only one value to the calling function.
 When return data type of a function is “void”, then, it won’t return any values
 When return data type of a function is other than void such as “int, float,
double”, it returns value to the calling function.
 main() program comes to an end when there is no functions or commands to
execute.
17
Thank You
Created and Presented by
1 de 17

Recomendados

Functions in C por
Functions in CFunctions in C
Functions in CKamal Acharya
25.2K visualizações22 slides
Functions in c por
Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K visualizações42 slides
Function in C program por
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.5K visualizações23 slides
Strings in C por
Strings in CStrings in C
Strings in CKamal Acharya
24K visualizações22 slides
File handling in c por
File handling in cFile handling in c
File handling in cDavid Livingston J
28.4K visualizações34 slides
C language ppt por
C language pptC language ppt
C language pptĞäùråv Júñêjå
194.7K visualizações26 slides

Mais conteúdo relacionado

Mais procurados

Pointers in C Programming por
Pointers in C ProgrammingPointers in C Programming
Pointers in C ProgrammingJasleen Kaur (Chandigarh University)
34.9K visualizações26 slides
Basics of c++ Programming Language por
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
47.8K visualizações78 slides
Pointer in c por
Pointer in cPointer in c
Pointer in clavanya marichamy
15.3K visualizações14 slides
Unit 3. Input and Output por
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and OutputAshim Lamichhane
11.4K visualizações31 slides
Basics of C programming por
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
165.1K visualizações39 slides
C functions por
C functionsC functions
C functionsUniversity of Potsdam
9.9K visualizações32 slides

Mais procurados(20)

Basics of c++ Programming Language por Ahmad Idrees
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees47.8K visualizações
Pointer in c por lavanya marichamy
Pointer in cPointer in c
Pointer in c
lavanya marichamy15.3K visualizações
Unit 3. Input and Output por Ashim Lamichhane
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane11.4K visualizações
Basics of C programming por avikdhupar
Basics of C programmingBasics of C programming
Basics of C programming
avikdhupar165.1K visualizações
RECURSION IN C por v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14K visualizações
Control statements in c por Sathish Narayanan
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan16.7K visualizações
Function overloading(c++) por Ritika Sharma
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma14.6K visualizações
Structure of a C program por David Livingston J
Structure of a C programStructure of a C program
Structure of a C program
David Livingston J22.3K visualizações
Strings in python por Prabhakaran V M
Strings in pythonStrings in python
Strings in python
Prabhakaran V M18.9K visualizações
Arrays por SARITHA REDDY
ArraysArrays
Arrays
SARITHA REDDY26.1K visualizações
Break and continue por Frijo Francis
Break and continueBreak and continue
Break and continue
Frijo Francis6.4K visualizações
Control Flow Statements por Tarun Sharma
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma8.4K visualizações
friend function(c++) por Ritika Sharma
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma17.6K visualizações
User defined functions in C por Harendra Singh
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh17.1K visualizações
Data types in C por Tarun Sharma
Data types in CData types in C
Data types in C
Tarun Sharma34K visualizações
Programming in c Arrays por janani thirupathi
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi23.7K visualizações
Operators in C Programming por programming9
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming937.7K visualizações
C if else por Ritwik Das
C if elseC if else
C if else
Ritwik Das13.8K visualizações

Similar a Presentation on Function in C Programming

CH.4FUNCTIONS IN C_FYBSC(CS).pptx por
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
29 visualizações31 slides
cp Module4(1) por
cp Module4(1)cp Module4(1)
cp Module4(1)Amarjith C K
529 visualizações47 slides
unit_2 (1).pptx por
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptxJVenkateshGoud
12 visualizações56 slides
unit_2.pptx por
unit_2.pptxunit_2.pptx
unit_2.pptxVenkatesh Goud
8 visualizações61 slides
USER DEFINED FUNCTIONS IN C.pdf por
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
103 visualizações33 slides
Function in c por
Function in cFunction in c
Function in csavitamhaske
1.3K visualizações18 slides

Similar a Presentation on Function in C Programming(20)

CH.4FUNCTIONS IN C_FYBSC(CS).pptx por SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 visualizações
cp Module4(1) por Amarjith C K
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K529 visualizações
unit_2 (1).pptx por JVenkateshGoud
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud12 visualizações
unit_2.pptx por Venkatesh Goud
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud8 visualizações
USER DEFINED FUNCTIONS IN C.pdf por BoomBoomers
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers103 visualizações
Function in c por savitamhaske
Function in cFunction in c
Function in c
savitamhaske1.3K visualizações
Unit-III.pptx por Mehul Desai
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai3 visualizações
Unit 3 (1) por Sowri Rajan
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan17 visualizações
Functionincprogram por Sampath Kumar
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar44 visualizações
Lecture 1_Functions in C.pptx por KhurramKhan173
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan1734 visualizações
C function por thirumalaikumar3
C functionC function
C function
thirumalaikumar3493 visualizações
Programming Fundamentals Functions in C and types por imtiazalijoono
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono510 visualizações
unit3 part2 pcds function notes.pdf por JAVVAJI VENKATA RAO
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO7 visualizações
Function C programming por Appili Vamsi Krishna
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna11.6K visualizações
Presentation on function por Abu Zaman
Presentation on functionPresentation on function
Presentation on function
Abu Zaman11.2K visualizações
Function in c program por umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.3K visualizações
User Defined Functions in C Language por Infinity Tech Solutions
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
Infinity Tech Solutions8.4K visualizações
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements... por RSathyaPriyaCSEKIOT
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT8 visualizações
Functions struct&union por UMA PARAMESWARI
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI115 visualizações
Unit iii por SHIKHA GAUTAM
Unit iiiUnit iii
Unit iii
SHIKHA GAUTAM187 visualizações

Último

Saikat Chakraborty Java Oracle Certificate.pdf por
Saikat Chakraborty Java Oracle Certificate.pdfSaikat Chakraborty Java Oracle Certificate.pdf
Saikat Chakraborty Java Oracle Certificate.pdfSaikatChakraborty787148
15 visualizações1 slide
_MAKRIADI-FOTEINI_diploma thesis.pptx por
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptxfotinimakriadi
8 visualizações32 slides
fakenews_DBDA_Mar23.pptx por
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptxdeepmitra8
14 visualizações34 slides
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... por
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...Anowar Hossain
13 visualizações34 slides
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptxlwang78
53 visualizações19 slides
CHEMICAL KINETICS.pdf por
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdfAguedaGutirrez
12 visualizações337 slides

Último(20)

Saikat Chakraborty Java Oracle Certificate.pdf por SaikatChakraborty787148
Saikat Chakraborty Java Oracle Certificate.pdfSaikat Chakraborty Java Oracle Certificate.pdf
Saikat Chakraborty Java Oracle Certificate.pdf
SaikatChakraborty78714815 visualizações
_MAKRIADI-FOTEINI_diploma thesis.pptx por fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 visualizações
fakenews_DBDA_Mar23.pptx por deepmitra8
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptx
deepmitra814 visualizações
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... por Anowar Hossain
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
Anowar Hossain13 visualizações
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang7853 visualizações
CHEMICAL KINETICS.pdf por AguedaGutirrez
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdf
AguedaGutirrez12 visualizações
Instrumentation & Control Lab Manual.pdf por NTU Faisalabad
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdf
NTU Faisalabad 5 visualizações
NEW SUPPLIERS SUPPLIES (copie).pdf por georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou15 visualizações
Machine Element II Course outline.pdf por odatadese1
Machine Element II Course outline.pdfMachine Element II Course outline.pdf
Machine Element II Course outline.pdf
odatadese19 visualizações
Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla637 visualizações
DevOps-ITverse-2023-IIT-DU.pptx por Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain9 visualizações
Codes and Conventions.pptx por IsabellaGraceAnkers
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptx
IsabellaGraceAnkers8 visualizações
SUMIT SQL PROJECT SUPERSTORE 1.pptx por Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 13 visualizações
Introduction to CAD-CAM.pptx por suyogpatil49
Introduction to CAD-CAM.pptxIntroduction to CAD-CAM.pptx
Introduction to CAD-CAM.pptx
suyogpatil495 visualizações
START Newsletter 3 por Start Project
START Newsletter 3START Newsletter 3
START Newsletter 3
Start Project5 visualizações
SPICE PARK DEC2023 (6,625 SPICE Models) por Tsuyoshi Horigome
SPICE PARK DEC2023 (6,625 SPICE Models) SPICE PARK DEC2023 (6,625 SPICE Models)
SPICE PARK DEC2023 (6,625 SPICE Models)
Tsuyoshi Horigome24 visualizações
zincalume water storage tank design.pdf por 3D LABS
zincalume water storage tank design.pdfzincalume water storage tank design.pdf
zincalume water storage tank design.pdf
3D LABS5 visualizações
Control Systems Feedback.pdf por LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 visualizações

Presentation on Function in C Programming

  • 1. Dhaka International University 1 Presentation Name: Function in C Language Presented To Mr. Tahzib Ul Islam Assistant Professor of CSE Dhaka International University Presented By Group: Quiet Fools Name: Shuvongkor Barman Roll: 38 Name: Minhaj Uddin Roll: 22 Name: Mostofa Kamal Roll: 20 Batch: 59 (Eve) Department: CSE
  • 2. 2 1. What is C function? 2.Types of C functions 3. Uses of C functions 4. Advantage of C functions 5. C function declaration, function call and definition 6. Simple Example Program for C Function 7. How to call C functions in a program? 8. Overview Outline
  • 3. 3 1.What is C Function? In short: A function is a group of statements that together perform a task. C functions are basic building blocks in a program. Long Answer: A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program
  • 4. Classification Of Function Library function User define function - main() -printf() -scanf() -sqrt() -getchar() 4A large program in c can be divided to many subprogram The subprogram posses a self contain components and have well define purpose. The subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main(). 2. Types of C functions
  • 5. 5 Standard Library Functions: Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library. Each library function in C performs specific operation. The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc. These functions are defined in the header file. When you include the header file, these functions are available for use. For example: ➤ The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file. ➤ There are other numerous library functions defined under "stdio.h", such as scanf(), printf(), getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use. User-defined Functions: As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions. Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.
  • 6. 6  C functions are used to avoid rewriting same logic/code again and again in a program.  There is no limit in calling C functions to make use of same functionality wherever required.  We can call functions any number of times in a program and from any place in a program.  A large C program can easily be tracked when it is divided into functions.  The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs. 3. Uses of C functions:
  • 7. It is much easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix 7
  • 8. 8 C functions aspects syntax function definition Return_type function_name (arguments list) { Body of function; } function call function_name (arguments list); function declaration return_type function_name (argument list); There are 3 aspects in each C function. They are,  Function declaration or prototype – this informs compiler about the function name, function parameters and return value’s data type.  Function call - this calls the actual function  Function definition – this contains all the statements to be executed. 5. C function declaration, function call and function definition:
  • 9. 9 As you know, functions should be declared and defined before calling in a C program.  In the below program, function “square” is called from main function.  The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function and multiplied value “p” is returned to main function from function “square”. 6. Simple Example Program for C Function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include<stdio.h> // function prototype, also called function declaration float square ( float x ); // main function, program starts from here int main( ) { float m, n ; printf ( "nEnter some number for finding square n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "nSquare of the given number %f is %f",m,n ); } float square ( float x ) // function definition { float p ; p = x * x ; return ( p ) ; } Enter some number for finding square 2 Square of the given number 2.000000 is 4.000000 Output: Enter some number for finding square 4 Square of the given number 2.000000 is 16.000000
  • 10. 10#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 200; int b = 800; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %dn", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } We have kept max() along with main() and compiled the source code. While running the final executable, it would produce the following result − Max value is : 800 Output:
  • 11. 1: #include <stdio.h> 2: 3: long cube(long x); 4: 5: long input, answer; 6: 7: int main( void ) 8: { 9: printf(“Enter an integer value: “); 10: scanf(“%d”, &input); 11: answer = cube(input); 12: printf(“nThe cube of %ld is %ld.n”, input, answer); 13: 14: return 0; 15: } 16: 17: long cube(long x) 18: { 19: long x_cubed; 20: 21: x_cubed = x * x * x; 22: return x_cubed; 23: }  Function names is cube  Variable that are requires is long  The variable to be passed on is X(has single arguments)— value can be passed to function so it can perform the specific task. It is called Output Enter an integer value: 4 The cube of 4 is 64. Return data type Arguments/formal parameter Actual parameters 11
  • 12. 127. How To Call C Functions In A Program? There are two ways that a C function can be called from a program. They are, 1.Call by value 2.Call by reference 1. Call By Value: •In call by value method, the value of the variable is passed to the function as parameter. •The value of the actual parameter can not be modified by formal parameter. •Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
  • 13. 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include<stdio.h> // function prototype, also called function declaration void swap(int a, int b); int main() { int m = 22, n = 44; // calling swap function by value printf(" values before swap m = %d nand n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); } values before swap m = 22 and n = 44 values after swap m = 44 and n = 22  In this program, the values of the variables “m” and “n” are passed to the function “swap”.  These values are copied to formal parameters “a” and “b” in swap function and used. Example Program For C Function (Using Call By Value):
  • 14. 14 2. Call by reference: •In call by reference method, the address of the variable is passed to the function as parameter. •The value of the actual parameter can be modified by formal parameter. •Same memory is used for both actual and formal parameters since only address is used by both parameters 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include<stdio.h> // function prototype, also called function declaration void swap(int *a, int *b); int main() { int m = 22, n = 44; // calling swap function by reference printf("values before swap m = %d n and n = %d",m,n); swap(&m, &n); } void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; printf("n values after swap a = %d nand b = %d", *a, *b); } Example Program For C Function (Using Call By Reference): •In this program, the address of the variables “m” and “n” are passed to the function “swap”. •These values are not copied to formal parameters “a” and “b” in swap function. •Because, they are just holding the address of those variables. •This address is used to access and change the values of the variables. values before swap m = 22 and n = 44 values after swap a = 44 and b = 22 Output:
  • 15. 15Overview C functions are basic building blocks in every C program. There are 2 types of functions in C. They are, 1. Library functions 2. User defined functions Key points to remember while writing functions in C language:  All C programs contain main() function which is mandatory.  main() function is the function from where every C program is started to execute.  Name of the function is unique in a C program.  C Functions can be invoked from anywhere within a C program.  There can any number of functions be created in a program. There is no limit on this.  There is no limit in calling C functions in a program.  All functions are called in sequence manner specified in main() function.  One function can be called within another function.  C functions can be called with or without arguments/parameters. These arguments are nothing but inputs to the functions.
  • 16. 16  C functions may or may not return values to calling functions. These values are nothing but output of the functions.  When a function completes its task, program control is returned to the function from where it is called.  There can be functions within functions.  Before calling and defining a function, we have to declare function prototype in order to inform the compiler about the function name, function parameters and return value type.  C function can return only one value to the calling function.  When return data type of a function is “void”, then, it won’t return any values  When return data type of a function is other than void such as “int, float, double”, it returns value to the calling function.  main() program comes to an end when there is no functions or commands to execute.
  • 17. 17 Thank You Created and Presented by