Function in C





Function ?

  • A function is a group of statements that together perform a task. Every C program has at least one function which that is main( ).

  • Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc.


Types of Functions :

1. Library Function: The C standard library provides numerous built-in functions that your program can call.
Example: printf()

2. User Define Function: If a set of statements are needed to execute more than one times then user can define their own function and call when needed.
Example: myfun()

Function's Components:

  1. Function's Prototype/Declaration .
  2. Function's Definition.
  3. Function's Calling.
1. Function Prototype/Declaration: Function prototype/declaration tells the compiler, information about function name, type of arguments to be passed and return type.

Syntax

return_type function_Name (argument_list);
Example:
int add (int a , int b);
Parameter names are not important in function declaration, type is required, so following is also valid declaration
int add (int  , int );

On the basis of return_type and argument_list we can declare a function in following ways:-
  1. No return_type and no argument_list
    example:void myFunc();

  2. No return_type and with argument_list
    example:void myFunc(int x);

  3. with return_type and no argument_list
    example:int myFunc();

  4. with return_type and with argument_list
    example:int myFunc(int x);

2. Function Definition:A function definition in C programming language consists of a function header and a function body.
return_type function_Name(formal_arg_list)
{
code block
}

Example:

Below is the source code for a function called cube(). This function takes one parameter num and returns the cube of that number:
int cube(int num)
{
  int result = num*num*num;
  return result;
}                            

3. Function Calling: To use a function, you will have to call that function to perform the defined task. When a program calls a function, program control is transferred to the called function.
funName(actual_agr_list);

example:

#include<stdio.h>
 
// function declaration
int cube(int num);
 
int main () {
   // local variable declaration:
   int a = 3;
   int res;
   // calling a function .
   res = cube(a);
    printf("Cube is : %d",res);
   return 0;
}
 
// function definition
int cube(int num)  {
   // local variable declaration
   int result;
   result = num * num * num;
   return result; 
}                           
OUTPUT:
Cube is :9


Way of Function Calling :

In C there are following ways to call a function:-
Calling TypeDescription
call by value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
call by address This method copies the address of an argument into the formal parameter. Inside the function, the address is used actual argument used in the call. This means that changes made to the parameter affect the argument.
By Default, C uses call by value to pass arguments.

1. Call by value

  • The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function.
  • This is the usual method to call a function in which only the value of the variable is passed as an argument.
  • Memory location occupied by formal and actual arguments is different.
  • Since actual and formal parameters have different memory location, changes made to the formal parameter have no effect on the actual argument.
  • Since a new location is created, this method is slow.

Example :

#include<stdio.h>
void swap(int , int);
int main() {
 int x = 20, y = 30;
 printf("BEFORE FUNCTION CALL\n");
 printf("X=%d and Y=%d",x,y)
 swap(x,y);
 printf("AFTER FUNCTION CALL\n");
 printf("X=%d and Y=%d",x,y)
 return 0;
}
void swap(int a, int b){
  int temp = a;
  a = b;
  b = temp;
}                         
OUTPUT:
BEFORE FUNCTION CALL
X=20 and Y=30
AFTER FUNCTION CALL
X=20 and Y=30

2. Call by address

  • In this method, the address of the variable is passed as an argument.
  • Memory location occupied by formal and actual arguments is same, changes made to the formal parameter have effect on the actual argument.
  • Since the existing memory location is used through its address, this method is fast.

example:

#include<stdio.h>
void swap(int * , int *);
int main() {
 int x = 20, y = 30;
 printf("BEFORE FUNCTION CALL\n");
 printf("X=%d and Y=%d",x,y)
 swap(&x,&y);
 printf("AFTER FUNCTION CALL\n");
 printf("X=%d and Y=%d",x,y)
 return 0;
}
void swap(int *a, int *b){
  int temp = *a;
  a = *b;
  *b = temp;
}                         
OUTPUT:
BEFORE FUNCTION CALL
X=20 and Y=30
AFTER FUNCTION CALL
X=30 and Y=20

Next topic is Pointers in C





 








Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved