C FUNCTIONS
Prepared by
TEENA GEORGE(CSE)
Assistant Professor
Adi Shankara Institute of Engineering &
Technology, Kalady
CONTENTS
 Introduction to modular programming/functions
 Elements of functions
 Parameter Vs Argument
 Sample program
2
WHAT IS A FUNCTION ???
 A function is a block of code in C that performs a
specific task.
 Every C program has at least one function, which
is main(), and can define additional functions.
3
NEED FOR FUNCTIONS/BENEFITS OF
FUNCTIONS
 Modular Programming :
 process of subdividing a computer program into
separate sub-programs/modules.
 Easy to built, use and test
 Increase readability
 Reuse the code
 Easy to maintain
 Easy to debug
 The length of a source program can be reduced
4
TYPES OF C FUNCTION
 Standard library functions
 built-in functions in C programming to handle tasks
such as mathematical computations, I/O processing,
string handling etc
 For example, printf() is a standard library function to
send formatted output to the screen defined
in "stdio.h" header file
 Some other examples : scanf(), strlen(), getchar() ,
sqrt(), toupper()
 User defined function
 Functions that we define ourselves to do certain
specific task
5
ELEMENTS OF A FUNCTION
6
C FUNCTIONS
 FUNCTION DECLARATION OR FUNCTION
PROTOTYPING
 FUNCTION CALL
 FUNCTION DEFINITION
7
FUNCTION DECLARATION OR PROTOTYPE
 This statement informs compiler about the function
name, function parameters and return value’s data type.
 Prototype declaration always ends with semicolon.
 If function definition is written above the main function
then ,no need to write prototype declaration
8
FUNCTION DECLARATION OR PROTOTYPE
 Syntax:
 Example:
 int sum(int a, int b);
 int sum(int, int);
 void display();
returntype functioname(argtype name1,....,argtype namen);
9
FUNCTION CALL
 When a program calls a function, the program
control is transferred to the called function.
 A called function performs a defined task and when
its return statement is executed or when its
function-ending closing brace is reached, it returns
the program control back to the main program.
10
FUNCTION CALL
 Syntax:
 Example:
 sum(a,b);
 display();
Functionname(argname1,argname2,....argnamen);
11
FUNCTION DEFINITION
 A function definition consists of a function header and
a function body.
 Function header :- consist of return type,function name,
arguments
 Return Type −
 return_type is the data type of the value the function returns.
 Some functions perform the desired operations without returning
a value(use ‘void’)
 Function Name −
 The function name of user choice following identifier rules
 Parameters −
 Values passed to a function to do the desired task
 Function Body :- contains a set of statements that define
what the function does.
12
FUNCTION DEFINITION
 Syntax:
 Example:
int sum(int a, int b)
{
int c=0;
c=a+b;
printf(“%d”,c);
}
return_type function_name(argument list ) //function header
{
body of the function
return statement
}
13
PARAMETER & ARGUMENTS
 Parameters are temporary variable names within
functions.
 The argument can be thought of as the value that is
assigned to that temporary variable
 Within the function, parameters act as placeholders for
the argument it is passed
 The parameter number and type of declaration and
definition must match.
 Actual parameters :- parameters appearing in
function calls.
 Formal parameters :-parameters appear in function
declarations/definitions 14
SAMPLE PROGRAM
#include <stdio.h>
void sum(int a,int b);
int main()
{
int a,b;
printf("nEnter values for a and b");
scanf("%d%d",&a,&b);
sum(a,b);
return 0;
}
void sum(int a,int b)
{
int c=0;
c=a+b;
printf("n%d + %d = %d",a,b,c);
}
Function Call
Function Definition
Function Declaration
15
REWIND…
 Can you think of a real time example for function?
 Can you remember any 5 built in functions in C ?
 Can you recall the three parts of a function?
 Can you differentiate function declaration and function
definition?
 What is the syntax of a function call statement?
 What do you call the parameters in the call statement?
 You can omit the declaration statement if definition of
function is done at the global declaration section. True
or False?
16
COMING SOON…….
Category of functions
1. Functions with no arguments and no return values
2. Functions with arguments and no return values
3. Functions with arguments and one return values
4. Functions with no arguments and but return a value
5. Function that have multiple return values(work based
on condition)
17
THANKYOU
18

C FUNCTIONS

  • 1.
    C FUNCTIONS Prepared by TEENAGEORGE(CSE) Assistant Professor Adi Shankara Institute of Engineering & Technology, Kalady
  • 2.
    CONTENTS  Introduction tomodular programming/functions  Elements of functions  Parameter Vs Argument  Sample program 2
  • 3.
    WHAT IS AFUNCTION ???  A function is a block of code in C that performs a specific task.  Every C program has at least one function, which is main(), and can define additional functions. 3
  • 4.
    NEED FOR FUNCTIONS/BENEFITSOF FUNCTIONS  Modular Programming :  process of subdividing a computer program into separate sub-programs/modules.  Easy to built, use and test  Increase readability  Reuse the code  Easy to maintain  Easy to debug  The length of a source program can be reduced 4
  • 5.
    TYPES OF CFUNCTION  Standard library functions  built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc  For example, printf() is a standard library function to send formatted output to the screen defined in "stdio.h" header file  Some other examples : scanf(), strlen(), getchar() , sqrt(), toupper()  User defined function  Functions that we define ourselves to do certain specific task 5
  • 6.
    ELEMENTS OF AFUNCTION 6
  • 7.
    C FUNCTIONS  FUNCTIONDECLARATION OR FUNCTION PROTOTYPING  FUNCTION CALL  FUNCTION DEFINITION 7
  • 8.
    FUNCTION DECLARATION ORPROTOTYPE  This statement informs compiler about the function name, function parameters and return value’s data type.  Prototype declaration always ends with semicolon.  If function definition is written above the main function then ,no need to write prototype declaration 8
  • 9.
    FUNCTION DECLARATION ORPROTOTYPE  Syntax:  Example:  int sum(int a, int b);  int sum(int, int);  void display(); returntype functioname(argtype name1,....,argtype namen); 9
  • 10.
    FUNCTION CALL  Whena program calls a function, the program control is transferred to the called function.  A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. 10
  • 11.
    FUNCTION CALL  Syntax: Example:  sum(a,b);  display(); Functionname(argname1,argname2,....argnamen); 11
  • 12.
    FUNCTION DEFINITION  Afunction definition consists of a function header and a function body.  Function header :- consist of return type,function name, arguments  Return Type −  return_type is the data type of the value the function returns.  Some functions perform the desired operations without returning a value(use ‘void’)  Function Name −  The function name of user choice following identifier rules  Parameters −  Values passed to a function to do the desired task  Function Body :- contains a set of statements that define what the function does. 12
  • 13.
    FUNCTION DEFINITION  Syntax: Example: int sum(int a, int b) { int c=0; c=a+b; printf(“%d”,c); } return_type function_name(argument list ) //function header { body of the function return statement } 13
  • 14.
    PARAMETER & ARGUMENTS Parameters are temporary variable names within functions.  The argument can be thought of as the value that is assigned to that temporary variable  Within the function, parameters act as placeholders for the argument it is passed  The parameter number and type of declaration and definition must match.  Actual parameters :- parameters appearing in function calls.  Formal parameters :-parameters appear in function declarations/definitions 14
  • 15.
    SAMPLE PROGRAM #include <stdio.h> voidsum(int a,int b); int main() { int a,b; printf("nEnter values for a and b"); scanf("%d%d",&a,&b); sum(a,b); return 0; } void sum(int a,int b) { int c=0; c=a+b; printf("n%d + %d = %d",a,b,c); } Function Call Function Definition Function Declaration 15
  • 16.
    REWIND…  Can youthink of a real time example for function?  Can you remember any 5 built in functions in C ?  Can you recall the three parts of a function?  Can you differentiate function declaration and function definition?  What is the syntax of a function call statement?  What do you call the parameters in the call statement?  You can omit the declaration statement if definition of function is done at the global declaration section. True or False? 16
  • 17.
    COMING SOON……. Category offunctions 1. Functions with no arguments and no return values 2. Functions with arguments and no return values 3. Functions with arguments and one return values 4. Functions with no arguments and but return a value 5. Function that have multiple return values(work based on condition) 17
  • 18.