before learning concept of 'Function call' ,lets see types of parameters

  • Parameters are two types:
  • Actual Parameters
    • Actual parameters used in function call.
  • Formal Parameters
    • Formal Parameters are used in function definition
  • Function call has actual arguments which are assigned to formal arguments in function definition.
  • Both the arguments should match in number ,type and order.
  • While learning 'function call' we will understand this concept by an examples.

Function call

  • Till now we have learned how to declare a function , and how to define function.
  • But ,how to use that function which is defined, soon we will learn it.
  • we will have to call or invoke a function to use it.
  • As you know, when function completes its task control goes back from where it was call, so if function returns some value than you should store that value for further need.
  • So, While calling function if it is returning some value, then you should assign it to a variable so you can make use of returned value.
  • lets understand it by an example.
Q:Make function which can calculate sum of two variable and should return the sum of two variable , And use that function by calling it by passing (7,8) as an argument.

Solution:
#include<stdio.h>

int add(int a ,int b);//Formal parameters.
//declering function 'add'.
//function will return integer value
//and it will take two values.

int add(int a ,int b)
{//defination of function.

    int result;

    result = a + b;
    return result;
}
//main() function
void main()
{
    int sums;
    //using add function in main()
    sums = add(7,8);//Actual parameter
    printf("sum of 7 and 8 is %d",sums);
}

Output:
Sum of 7 and 8 is 15.

Q:Make function which can check whether number is odd or even by taking one number as argument and should return 1 if number is even and should return 0 if number is odd, And use that function by calling it by passing (7) as an argument.

Solution:
#include<stdio.h>

int check(int a) //Formal parameters.
{
    int result;
    if(a % 2==0)
       result = 1;
    else
      result = 0;
//if result is 1 then number is even.
//if result is 0 then number is odd.
  return result;
}
void main()
{
    int result;
    //using 'check' function in main()
    result = check(7); //Actual parameter
 //return value will assign to varaible result
    if(result == 1)
      printf("Number is even number.");
    else
       printf("Number is a odd number.");
}

Output:
Number is a odd number.

Q:declare a function 'give_ascii' which will take An character as an argument and it should return nothing just print the ascii value for that character ?  test the function by calling it ,by passing 'A' as the argument.

Solution:
#include<stdio.h>
void give_ascii(char a); //declaration of function.

void give_ascii(char a)      //Formal parameters.
{   //if function will  return nothing ,then
    //return data type will be void.
    printf("\nAscii value of character %c is %d",a,a);
        //as you know '%d' specifier give the ascii value
        //for any character
}
void main()
{
    //calling 'give_ascii function
    //by passing character 'A'.
    give_ascii('A');    //Actual parameter
}

Output:
Ascii value of character A is 65
Note: If function is returning nothing, than the return datatype of function will be void.

Q:declare a function 'give_next' which will take An character 'a' as an argument and it should return a character which will the next character of 'a'?test the function by calling it ,by passing 'G' as the argument.

Solution:
#include<stdio.h>
char give_next(char a); //declaration of function.

char give_next(char a)  //Formal parameter.
{
    char next_char; //variable
    next_char = a+1;//As,we know adding 1 to a character
                     //gives the next character.
    return next_char;
    //returning the next_Char variable.
}

void main()
{
    //calling 'give_next function
    //by passing character 'G'.
    
    char new_char = give_next('G');//actual parameter
    printf("next character is %c",new_char);
    
}

Output:
next character is F

Methods of Passing Parameter.

  • There are two ways of passing argument to a function.
  • Call by value
    • Even if we are changing the argument while calling Function,argument will remain unchange.
    • As the content of actual parameters get copied into the corresponding formal parameters.
  • Call by Reference
    • In this method ,Instead of passing value of variable we have to pass memory address of variable to the function.
    • Calling function by ,Passing memory address to the function as parameter is known as Call by reference.
    • In this method content of argument in th calling function get changed.
  • For example:If you want make function that can changed the arguments than this thing will not work if you will use call by value method for passing parameter,Instead of that you have to pass argument by call by reference method.
  • Lets see example:Make function which will take two integer value as an argument and it should swap that arguments?

    by using call by value:
    #include<stdio.h>
    void  swap(int a,int b); //decleration of function.
    
    void swap(int a ,int b)
    {
        int temp;
    
        temp = a;
        a  = b;
        b  = temp;
    }
    void main()
    {
        //calling 'give_next function
        //by passing character 'G'.
    
      int a=10,b=12;
      swap(a,b);
      printf("Value of a = %d and Value of b= %d",a,b);
    
    }
    

    Ouput:
    Value of a = 10 and Value of b= 12
    As you saw content did not changed.
    In case you are thinking that it did't  change because we did not return value, it is because we cant return multiple values.
    So how to solve this problem, This problem can solve by using call of reference method for passing arguments.


    by using call by reference:
    #include<stdio.h>
    void  swap(int  *a,int  *b); //declaration of function.
    
    void swap(int *a ,int *b)
    { //this function will take memory location as argument.
        int temp;
    //this function is swapping variable at one location with variable at other location.
        temp = *a; //'*' operator gives  value of the variable  at that address.
        *a  = *b;
        *b  = temp;
    }
    void main()
    {
    
      int a=10,b=12;
      swap(&a,&b);//call by reference.
       //passing the address of both the variables.
      printf("Value of a = %d and Value of b= %d",a,b);
    
    }
    

    Output:
    Value of a = 12 and Value of b= 10
    Now the arguments are interchange. This is how Call by Reference methods works.
  • Concept of Memory locations ,you can learn it here Pointers




Further Concepts:


People Also Searched: