-->

11. C-Functions Naming Convention

C-Functions: Previous                                                                          Next: C-Pointers

Naming convention for writing a function name in C Programming: 

Function name follows the same convention which is followed for variable names:
  • First character must be an alphabet not a digit.
  • Spaces are not allowed.
  • Keywords can`t be taken as a function name.
  • Length of name is <= 40 characters.
  • No special characters are allowed expect underscore.

Recursion:

In recursion, a function calls itself until the condition will not become false. The function which is called again and again is called recursive function and this calling process is known as Recursion.
Recursion is used in game “Tower of Hanoi”.
Eg:           Write a program to calculate Factorial of given number using Recursion.

Solution:  #include<stdio.h>
                                                #include<conio.h>
                                                void main( )
                                                {
                                                                int num, fact = 0;
                                                                clrscr();
                                                                printf(“Enter number : ”);
                                                                scanf(“%d”, &num);
                                                                fact = factorial(num);
                                                                printf(“The Factorial is %d”, fact);
                                                                getch();
                                                }
                                                int factorial(int a)
                                                {
                                                                if(a==1)
                                                                                return 1;
                                                                return a * factorial(a-1);
                                                }
Output:

Passing Arguments to a Function in C Programming: -
Parameter passing is a process for communication of data between the function.
Parameter can be passed by two ways:
  1. Call by Value.
  2. Call by Reference.
  1. Call by Value: - 

When arguments are passed by value that passing of method is called “Call by Value”. When we passed argument by call by value it means we passing a copy of arguments not the actual value of arguments. The value of copied argument can be changed. In other way we can say that the formal arguments are copy of actual arguments.

Eg:           Write a program to swap two number using function [Call by Value]
Solution:  #include<stdio.h>
                                                                #include<conio.h>
                                                                void main( )
                                                                {
                                                                                int num1, num2;
                                                                                clrscr();
                                                                                printf(“Enter 2 numbers : ”);
                                                                                scanf(“%d %d”, &num1, &num2);
                                                                                swap(num1,num2);
                                                                                printf(“After swapping Number1 is %d  Number2 is %d”, num1, num2);
                                                                                getch();
                                                                }
                                                                void swap (int a, int b)
                                                                {
                                                                                int temp;
                                                                                temp = a;
                                                                                a = b;
                                                                                b = temp; 
                                                                                printf(“After swapping Number1 is %d  Number2 is %d”, a, b);                                                                                                             }             
Output:

(B) Call by Reference:-
When we pass arguments by address, the method is called “Call by Reference”. The both (actual and formal arguments) share the same memory location. The address of(&) operator is used with variable name at the time of passing arguments.
Eg:           Write a program to swap two number using function [Call by Reference]
Solution:  #include<stdio.h>
                                                                #include<conio.h>          
                                                                void main( )
                                                                {
                                                                                int num1, num2;
                                                                                clrscr();
                                                                                printf(“Enter 2 numbers : ”);
                                                                                scanf(“%d %d”, &num1, &num2);
                                                                                swap(&num1, &num2);
                                                                                printf(“After swapping Number1 is %d \n Number2 is %d”, num1, num2);
                                                                                getch();
                                                                }
                                                                void swap (int *a, int *b)
                                                                {
                                                                                int temp;
                                                                                temp = *a;
                                                                                *a = *b;
                                                                                *b = temp; 
                                                                                printf(“After swapping Number1 is %d \n Number2 is %d”, *a, *b);
                                                                }     
Output:
       

C-Functions: Previous                                                                          Next: C-Pointers