Lecture 16 – Functions


A Function is a block of code which is reusable. Let’s try to understand a function and a use of a function with a real life example. Consider a scenario where I say “Dear Student! Bring me a glass of water, please”.

Statement breakup

·         Dear Student – it is analogous to function name

·         Bring me – it is analogous to a function call

·         A glass of water – it is analogous to a parameter passed

When the student brings a glass of water and gives it to me, then he/she has returned me a glass of water. Now the returned glass of water is a return type.

Syntax

return-type function-name (arguments-list)

{

//function body

}


How do we write the program using functions? The answer to this question is available in this document. Firstly, let’s categorize the functions we would be dealing with.

 

S.No.
Accepts
Returns
1
Nothing (No Parameter)
Nothing
2
Nothing (No Parameter)
A value
3
A value
Nothing
4
A value
A value

Table 12- Functions we would be dealing with

 

 

Let’s start with the first type of function which would accept no parameter and shall not return anything.

Type 1 – Functions that take NO argument and returns nothing

Writing a function

For writing a function we need to provide its declaration (return-type, function name, and parameters if any), and definition (function body).




Output

There would be no output as the program has no main function

 
void name()




{

printf(“\nWelcome to a function. This is the body of the function”);

}

Example 29 - Writing a function with NO Parameter and NO Return Type

 

Now writing the main function

Calling a function

Calling a function means to use the function.  You need to call a function in main() before you can use it. The function which makes the call is called a caller and the function which is called is called callee or called function. In our example the main() is caller and name() is callee.

#include <stdio.h>

#include <conio.h>




Output

There would be output as this program would not compile. There’s a syntax error i.e. “Function prototype missing”

 

 

 

 


void main()

{

clrscr();                 // Function Call

name();                                // Function Call to my function Name

getch();                                // Function Call

}

 

void name()

{

printf(“\nWelcome to a function. This is the body of the function”);

}

Example 30 - Calling a function

 


Function prototype

Function prototype is a name given to the function declaration followed by semicolon. It is required because the compiler stops processing lines when it reaches the end of the main function. We need to tell explicitly to the compiler to look for some piece of code after the body of main function.

 

#include <stdio.h>

#include <conio.h>

 

void name();




Output

Welcome to a function.

This is the body of the function

 


void main()

{

clrscr();                 // Function Call

name();                                // Function Call to my function Name

getch();                                // Function Call

}

 

void name()

{

printf(“\nWelcome to a function.\nThis is the body of the function”);

}

Example 31 - Using Function Prototype

Using Function without a prototype

We can use a function without specifying its prototype. To achieve this we need to write the function declaration and its body before the main function.

#include <stdio.h>

#include <conio.h>

 

void name()

{

printf(“\nWelcome to a function.\nThis is the body of the function”);

}




Output

Welcome to a function.

This is the body of the function

void main()

{

clrscr();                 // Function Call

name();                                // Function Call to my function Name

getch();                                // Function Call

}

Example 32 - Using function without a prototype

 

 

 

Comments

Popular posts from this blog

Lecture 17 – Functions (continued)

Lecture 6 – Operators in C (continued)

Lecture 10 – Decisions (if-else-if)