Function / Methods In C Programming Language

In today’s video tutorial lets learn the basics of function or methods in C programming language.

Function / Method: is a group of statements that together perform certain task.

Note: Every C program should have 1 main function. No 2 functions / methods should be named as main.

Video Tutorial: Function / Methods In C Programming Language



YouTube Link: https://www.youtube.com/watch?v=X7auVd3QpTM [Watch the Video In Full Screen.]

Advantages of Function/Methods

1. It provides modularity to the program structure.
2. You can reuse your code. You can write function definition once and call the function any number of times in your program.
3. Easy to read, understand, edit and debug your code.

Types of Functions/Methods

1. Built in functions/methods.
2. User defined functions/methods.

1. Built in functions/methods: So far we’ve used a lot of builtin methods like pow(), sin(), cos(), tan(), sqrt() etc which are all present in library / header file math.h

If you open math.h header file and check the code, it’ll have function prototype and function definition for pow(), sin(), cos(), tan(), sqrt() etc.

Functions like printf(), scanf() and other input output functions/methods are present in stdio.h library file.

Similarly, if we want to write user defined function, we must specify the function prototype and function definition ourselves.

2. User defined functions/methods: We can define and use our own functions. We can have 4 types of user defined functions/methods.
a. No return type, no arguments.
b. No return type, with arguments.
c. With return type, no arguments.
d. With return type, with arguments.

Stay subscribed to our YouTube channel and blog for video tutorials explaining all types of user defined functions.

General Syntax of user defined Funtion/Method

1. Function prototype.
return_type funtion_name(argument_data_type_list);

2. Function definition.
return_type funtion_name(argument_data_type_list)
{
//instructions
}

3. Function Call.
funtion_name(argument_data_type_list);

Source Code: Function / Methods In C Programming Language: With Return Type and With Arguments

#include<stdio.h>

int add(int, int); // function prototype

int main()
{
    int a = 10, b = 20, c;

    c = add(a, b); // function call

    printf("Addition of %d and %d is %d\n", a, b, c);

    return 0;
}

//function definition
int add(int i, int j)
{
    return(i+j);
}

Output
Addition of 10 and 20 is 30

Source Code: Function / Methods In C Programming Language: No Return Type and No Arguments

#include<stdio.h>

void companies(); // function prototype

int main()
{
    companies(); // function call

    return 0;
}

//function definition
void companies()
{
    printf("1. IBM\n");
    printf("2. Apple\n");
    printf("3. Google\n");
    printf("4. Oracle\n");
    printf("5. Ripple\n");
}

Output
1. IBM
2. Apple
3. Google
4. Oracle
5. Ripple

Source Code: Function / Methods In C Programming Language: No Return Type and With Arguments

#include<stdio.h>

void multiply(int, int); // function prototype

int main()
{
    int a = 9, b = 12;

    multiply(a, b); // function call

    return 0;
}

//function definition
void multiply(int x, int y)
{
    printf("%d x %d = %d\n", x, y, (x*y));
}

Output
9 x 12 = 108

Source Code: Function / Methods In C Programming Language: With Return Type and No Arguments

#include<stdio.h>

int subtract(); // function prototype

int main()
{
    printf("Subtraction Result = %d\n", subtract() ); // function call

    return 0;
}

//function definition
int subtract()
{
    int a = 50, b = 25, c;

    c = a - b;

    return(c);
}

Output
Subtraction Result = 25

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of 2 Numbers Using Function: C++

Find the biggest of two numbers using function and ternary operator.

Basics of functions:

A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.

Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.

Function Prototype:

Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:

< return_type > < function_name >( arguments_type );

Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);

Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:

< return_type >< function_name >(parameters)
{
 
}

Example:

int findsum(int a, int b)
{
 int sum;
 
 sum = a + b;
 return(sum);
}

Function Call:
It is a technique used to invoke a function.
Syntax:

[variable] < function_name >([arguments]);

Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();

Full Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include< iostream.h >
#include< conio.h >
 
void main()
{
  int Big(int x,int y);  // Prototype
 
  int a, b;
  clrscr();
 
  cout< <"Enter 2 numbers\n";
  cin>>a>>b;
 
  int res = Big(a, b);   // Function call
 
  cout< <"Biggest = "<<res;
  getch();
}
 
int Big(int x, int y)   // Function Definition
{
  return( x>y?x:y );
}

You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++

Video Tutorial: Biggest of 2 Numbers Using Function



YouTube Link: https://www.youtube.com/watch?v=mZOaAXL4t9c [Watch the Video In Full Screen.]



Output:
Enter 2 numbers
420
305
Biggest = 420