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

Зміст курсу

C Basics

Declaration, Type and ReturnDeclaration, 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.c

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

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?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 2
course content

Зміст курсу

C Basics

Declaration, Type and ReturnDeclaration, 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.c

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

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?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 2
some-alt