Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Declaration, Type and Return | Functions
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

bookDeclaration, Type and Return

From our previous lesson, you learned the basic anatomy of functions:

Declaration

Before utilizing a function, it has to be declared. Calling a function before its declaration will result in an error.

You can either declare the entire function simultaneously with its definition (as illustrated above) or use a prototype, which we'll touch upon as a bonus at the end of this segment.

Function Types and Return Values

The function's type informs the compiler about the datatype of the function's outcome. For instance, if our function calculates the sum of two integers, then we anticipate an integer as the result:

Note

If a function type isn't specified, the compiler defaults to treating it as an int.

A mismatch between the data type and the return type might render the function (or even the whole program) malfunctioning. Here's an example: let's add two non-integer numbers, but set the function to return an integer type:

When summing non-integer values, you'd expect a precise outcome. But due to the type inconsistency, the result is erroneous.

c

Main

copy
123456789101112131415161718
#include <stdio.h> int func() { double number1 = 6.985; // not int nubmer double number2 = 231.765; // not int number double result = number1 + number2; // not int number too return result; // output of function } int main() { // Call and immediately printing the result of our function printf("Expected Result is 238.75, but result is %d\n", func()); return 0; }

Two primary factors contribute to this error:

  • An incorrect return type (should be double);
  • Using the wrong format specifier (%d) in the printf() function (should be %f).

The correct version would be:

c

Main

copy
123456789101112131415161718
#include <stdio.h> double func() { double number1 = 6.985; // int nubmer double number2 = 231.765; // int number double result = number1 + number2; // int number too return result; // output of function } int main() { // We immediately print the result of our function printf("Expected Result is 238.75, and result is %f\n", func()); return 0; }

Note

The outcome of any function will typically be a numeric or string value.

This implies that we can directly assign the function's result to variables/arrays or even pass it to other functions. An example of this is how we used the result with the printf function in the scenarios above.

Note

%f is the format specifier for floating-point numbers.

It's also worth mentioning that a function can house multiple return statements, with each activating under distinct conditions.

You've constructed the `count` function, how would you invoke it within the `main` function?

You've constructed the count function, how would you invoke it within the main function?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt