KEMBAR78
Programming in C FUNCTION Basic concepts.pptx
FUNCTION
in
C Programming
What is function?
 A function is a block of code that performs a
specific task.
 Dividing a complex problem into smaller chunks
makes our program easy to understand and
reuse.
 The function is also known as procedure
or subroutine in other programming languages.
Advantage of functions in C
 Reusability is the main achievement of C functions.
By using functions, we can avoid rewriting same
logic/code again and again in a program.
 Abstraction without knowing the internal structure
we can call C functions any number of times in a
program and from any place in a program.
 Modularity We can track a large C program easily
when it is divided into multiple functions.
Function prototype
 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.
Syntax:
return_type function_name (argument list);
 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 arguments as it is declared
in the function declaration.
Syntax:
function_name (argument_list);
 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.
Syntax:
return_type function_name(data_type parameter...){
//code to be executed
}
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");
}
 If you want to return any value from the
function, you need to use any data type such as
int, long, char, etc. The return type depends on
the value to be returned from the function.
Example with return value:
int get(){
return 10;
}
Types of Functions
There are two types of functions in C programming:
 Library Functions: are the functions which are
declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
 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.
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else result = num2;
return result;
}
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
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;
}
Output
Max value is : 200
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
Thank You

Programming in C FUNCTION Basic concepts.pptx

  • 1.
  • 2.
    What is function? A function is a block of code that performs a specific task.  Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.  The function is also known as procedure or subroutine in other programming languages.
  • 3.
    Advantage of functionsin C  Reusability is the main achievement of C functions. By using functions, we can avoid rewriting same logic/code again and again in a program.  Abstraction without knowing the internal structure we can call C functions any number of times in a program and from any place in a program.  Modularity We can track a large C program easily when it is divided into multiple functions.
  • 4.
    Function prototype  Thereare 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. Syntax: return_type function_name (argument list);
  • 5.
     Function call Functioncan 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 arguments as it is declared in the function declaration. Syntax: function_name (argument_list);
  • 6.
     Function definition Itcontains 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. Syntax: return_type function_name(data_type parameter...){ //code to be executed }
  • 7.
    Return Value  AC 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"); }
  • 8.
     If youwant to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function. Example with return value: int get(){ return 10; }
  • 9.
    Types of Functions Thereare two types of functions in C programming:  Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.  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.
  • 10.
    int max(int num1,int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 11.
    #include <stdio.h> /* functiondeclaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; 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; } Output Max value is : 200
  • 12.
    Different aspects offunction 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
  • 13.