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

bookDeclaration, Type and Return

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

main.c

main.c

copy
123456
func_type func_name(arguments_placeholder) { // The core functionality of the function // The resulting outcome after function execution return function_output; }

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.

main.c

main.c

copy
12345678
int sumFunction() { int number1 = 6; int number2 = 10; int result = number1 + number2; return result; }

A mismatch between the data type and the return type might render the function (or even the whole program) malfunctioning.

main.c

main.c

copy
12345678
int sumFunction() { double number1 = 6.985; double number2 = 231.465; double result = number1 + number2; return result; // This will lead to an incorrect result }

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

main.c

main.c

copy
1234567891011121314151617
#include <stdio.h> int func() { double number1 = 6.985; // Not `int` nubmer double number2 = 231.765; // Not `int` number double result = number1 + number2; 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:

main.c

main.c

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

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
Note

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

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

question mark

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

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about function prototypes?

What happens if I use the wrong return type in my function?

Can you give an example of a function with multiple return statements?

Awesome!

Completion rate improved to 2.63

bookDeclaration, Type and Return

Swipe to show menu

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

main.c

main.c

copy
123456
func_type func_name(arguments_placeholder) { // The core functionality of the function // The resulting outcome after function execution return function_output; }

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.

main.c

main.c

copy
12345678
int sumFunction() { int number1 = 6; int number2 = 10; int result = number1 + number2; return result; }

A mismatch between the data type and the return type might render the function (or even the whole program) malfunctioning.

main.c

main.c

copy
12345678
int sumFunction() { double number1 = 6.985; double number2 = 231.465; double result = number1 + number2; return result; // This will lead to an incorrect result }

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

main.c

main.c

copy
1234567891011121314151617
#include <stdio.h> int func() { double number1 = 6.985; // Not `int` nubmer double number2 = 231.765; // Not `int` number double result = number1 + number2; 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:

main.c

main.c

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

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
Note

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

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

question mark

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
some-alt