munotes®

Practical 4(a): The Area of a Square, Using a Function

Chapter Thirteen

Syllabus topic Module 1, Practical 4(a): "Write a program to print area of square using function."

Pages 44 to 46 of 206

Aim

To print the area of a square using a function.

What a function is, and why a program has any

A function is a named piece of a program that does one job. You give it some values, it does its work, and it hands a value back.

main is a function. printf is a function. A program is nothing but functions, and until now yours have had exactly one.

Three reasons to write your own, and only the third is the real one.

It is written once and used many times. The area calculation appears in one place instead of four.

It has a name, so the program reads like a description of itself. area(side) says what is happening; s * s buried inside a printf does not.

It can be reasoned about on its own. You can be sure area is right by looking at three lines, without reading the rest of the program. That is the reason large programs are possible at all.

The six words, kept apart

This vocabulary is asked in vivas constantly, and students mix up the last two.

WordWhat it isIn this program
Declaration, or prototypetells the compiler the name, the return type and the parameter typesfloat area(float side);
Definitionthe declaration plus the body that does the workfloat area(float side) { ... }, with its body
Callthe place where the function is usedarea(s)
Return typethe type of the value handed backfloat
Parameterthe variable named in the definition, which receives the valueside
Argumentthe value actually passed at the calls, or 4.5

The last two in one line: a parameter is written in the function, an argument is written at the call. The argument's value is copied into the parameter when the call happens.

The program

#include <stdio.h>

float area(float side);

int main(void)
{
    float s, a;

    printf("Enter the side of the square: ");
    scanf("%f", &s);

    a = area(s);

    printf("Side = %.2f\n", s);
    printf("Area = %.2f\n", a);
    printf("A square of side 3 has area %.2f\n", area(3));

    return 0;
}

float area(float side)
{
    return side * side;
}
4.5
Enter the side of the square: Side = 4.50
Area = 20.25
A square of side 3 has area 9.00

Check it: 4.5 by 4.5 is 20.25.

Reading the program in the order the compiler does

float area(float side); near the top is the declaration. It ends in a semicolon and has no body. It exists so that when the compiler reaches area(s) inside main, it already knows that area takes one float and gives back a float.

Leave it out and put the definition after main, and C17 refuses to compile: calling a function the compiler has not seen is not allowed in modern C. Put the whole definition above main instead and no declaration is needed, because the definition is also a declaration. Both arrangements are correct; this book declares first and defines last, because that keeps main at the top where a reader looks for it.

munotes.in44

Practical 4(a): The Area of a Square, Using a Function

a = area(s); is the call. The value in s is copied into the parameter side, the body runs, and the value that return produces becomes the value of the expression area(s), which is then assigned to a.

return side * side; computes and hands back. return does two things at once: it supplies the value, and it ends the function immediately. Any statement after it in the same block never runs.

area(3) inside the last printf shows that a call is an expression. It has a value, so it can go anywhere a value can go. The argument here is a constant rather than a variable, which is allowed: the argument is a value, not a place.

What void means, in both positions

void print_line(void);

Before the name, void means the function returns nothing. Such a function is called for what it does, not for what it gives back, and it ends either at its closing brace or at a bare return; with no value.

Inside the brackets, void means the function takes nothing. That is why main is written int main(void).

Empty brackets, float area(), are not the same thing in C as (void), and C23 finally made them mean the same. Write (void) and the question never arises.

Where the parameter lives

The parameter side exists only while the function is running. It is created when the call happens, it holds a copy of the argument, and it is destroyed when the function returns.

#include <stdio.h>

void spoil(float side);

int main(void)
{
    float s = 4.5;

    spoil(s);
    printf("Back in main, s is still %.2f\n", s);

    return 0;
}

void spoil(float side)
{
    side = 99;
    printf("Inside the function, side is %.2f\n", side);
}
Inside the function, side is 99.00
Back in main, s is still 4.50

The function changed its own copy and main never noticed. That is call by value, it is how every C function call works unless you go out of your way, and it is the subject of [Practical 7: Swapping Two Numbers, by Value and by Reference].

What beginners get wrong

Putting a semicolon after the definition's head. float area(float side); followed by a body is a declaration and then a stray block. The definition's head has no semicolon.

munotes.in45

Practical 4(a): The Area of a Square, Using a Function

Forgetting the return type. In C17 a function with no stated return type does not compile.

Declaring the function inside main. It is legal but pointless and confusing. Declare at file scope, above main.

Writing return; in a function that promises a value, or return x; in a void function. Both are errors.

Expecting a changed parameter to reach the caller. It does not. The parameter is a copy.

Calling the function without using its value. area(s); on a line of its own computes the area and throws it away.

Quick revision

  • Declaration: name, return type, parameter types, ends in a semicolon, no body.
  • Definition: the same head with a body, and no semicolon after the head.
  • Call: area(s), an expression with a value.
  • Parameter is in the function; argument is at the call.
  • return supplies the value and ends the function at once.
  • void before the name means no value returned; void in the brackets means no parameters taken.
  • Arguments are passed by value: the function works on a copy.

What goes in your journal

Aim, an algorithm that names the function as a step ("call area with s and store the result"), a flowchart, the program with the declaration, the definition and the call all visible, and the output. In the conclusion define parameter and argument in one sentence each; it is the viva question this practical always carries.

Test yourself

1. What is the difference between a parameter and an argument? The parameter is the variable named in the function definition; the argument is the value supplied at the call and copied into the parameter.

2. Why is a declaration needed when the definition comes after main? Because the compiler reads the file from the top, and in C17 a call to a function it has not yet seen is an error.

3. What does return do, apart from supplying a value? It ends the function immediately. Statements after it in the same block are never reached.

4. What does void mean in int main(void)? That main takes no parameters.

5. A function changed its parameter. Does the caller's variable change? No. The argument's value was copied into the parameter, so the function changed only its own copy.

6. Is area(3) legal when the parameter is a float? Yes. The declaration tells the compiler a float is expected, so the constant 3 is converted to 3.0 before the call.

munotes.in46

The rest of this subject

These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.

Report or request
Done!