Posts

Lecture 18 – Functions (continued)

Let’s see how we can write a function which takes an argument but does not return a value. Type 3 – Functions that take argument and returns nothing Let’s work with another type in which 1.        Main function will take input from the user 2.        Pass that value to the called function 3.        The called function will calculate its square 4.        The called function will display the calculated value on screen   #include <stdio.h> #include <conio.h>   void square(int number) { Output Please enter a number: 5 The square is 25 number = number * number; printf(”\nThe square   is %d ”,number); } void main() { int result, input_value; clrscr();       ...

Lecture 17 – Functions (continued)

Let’s see how we can write a function which takes no argument but returns a value. Type 2 – Functions that take NO argument and returns A value I would write a function which would 1.        Ask the user to input a number 2.        Calculate its square 3.        Return the calculated value to main function 4.        Main function will display calculated result   #include <stdio.h> #include <conio.h>   void square() { int number; printf(“\nPlease enter a number :”); Output There would be no output as it has a syntax error. The error is LVALUE REQUIRED scanf(“%d”, &number); number = number * number; return number; } void main() { clrscr();         ...

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 w...