ankitweblogic logo
W e b t u t o r i a l s

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if , switch

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C - Function

A function is a block of statements that perform a task. Instead of writing a block of code again and again, we can create a function, write the code inside that function and call that function as many times as required. When the function is call the control passes to that function, execute the function statements and return back from where it is called.

Features of a function:

  • Function reduces the size of a program.
  • A function can return only one value at a time.

A function consists of 3 parts:

1. Prototype or declaration - A function declaration tells the compiler about a function name, return type, and its parameter. For eg: return_datatype function_name(datatype1 , datatype2);

2. Calling – To use a function, you have to call that function to perform the defined task. while calling a function mention the function name with its parameter. For eg: variable_name function_name(variable_name1, variable_name2).

3. Body or definition – The function body contains a collection of statements that define the logic to solve a particular problem. For eg: return_datatype function_name(datatype1, datatype2) { statement 1; statement 2; }


Return datatype: A function may or may not return a value, but it cannot return more than one value at a time. The return data type returns the result back from where a function is called. If the function is not returning any value in that case you must prefix void keyword before function name. If no datatype is specified, default return datatype is integer.

Function_name: This is the actual name of the function.

Parameter: A parameter is like a placeholder. When a function is called, you can pass a value to that function via parameter. This value is referred to as actual parameter or argument. The parameter list refers to the datatype, argument order, and number of the arguments in a function. Parameters are optional or you can provide n number of arguments there is no maximum limit.

Note: Main is also a function. It can be called from other functions.


Example: Print a message "Hello World" using function

void fun();
void main()
{
   printf("I am in main\n");
   fun();
   printf ("I am back in main\n");
   getch();
}
void fun()
{
   printf ("Hello\n");
}

Advantages of function:

  • Avoid writing the same code again and again.
  • Function make easier to write programs and keep track what they are doing.
Updated: 2-Mar-19