If a function does not return a value, by default, it returns

The result of a function is called its return value and the data type of the return value is called the return type.

Every function declaration and definition must specify a return type, whether or not it actually returns a value.

If a function declaration does not specify a return type, the compiler assumes an implicit return type of int. However, for conformance to C99, you should specify a return type for every function declaration and definition, whether or not the function returns int.

A function may be defined to return any type of value, except an array type or a function type; these exclusions must be handled by returning a pointer to the array or function. When a function does not return a value, void is the type specifier in the function declaration and definition.

A function cannot be declared as returning a data object having a volatile or const type, but it can return a pointer to a volatile or const object.

There's one last essential concept about functions for us to discuss — return values. Some functions don't return a significant value, but others do. It's important to understand what their values are, how to use them in your code, and how to make functions return useful values. We'll cover all of these below.

Prerequisites:

Basic computer literacy, a basic understanding of HTML and CSS, JavaScript first steps, Functions — reusable blocks of code.

Objective:To understand function return values, and how to make use of them.

Return values are just what they sound like — the values that a function returns when it completes. You've already met return values several times, although you may not have thought about them explicitly.

Let's return to a familiar example [from a in this series]:

const myText = "The weather is cold";
const newString = myText.replace["cold", "warm"];
console.log[newString]; // Should print "The weather is warm"
// the replace[] string function takes a string,
// replaces one substring with another, and returns
// a new string with the replacement made

The replace[] function is invoked on the myText string, and is passed two parameters:

  1. the substring to find ['cold'].
  2. the string to replace it with ['warm'].

When the function completes [finishes running], it returns a value, which is a new string with the replacement made. In the code above, the result of this return value is saved in the variable

function draw[] {
  ctx.clearRect[0, 0, WIDTH, HEIGHT];
  for [let i = 0; i  {
  const num = parseFloat[input.value];
  if [isNaN[num]] {
    para.textContent = "You need to enter a number!";
  } else {
    para.textContent = `${num} squared is ${squared[num]}. `;
    para.textContent += `${num} cubed is ${cubed[num]}. `;
    para.textContent += `${num} factorial is ${factorial[num]}. `;
  }
}];

Here we are adding a listener to the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
1 event. It runs whenever the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
1 event fires on the text input — that is, when a new value is entered into the text
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
3, and submitted [e.g., enter a value, then un-focus the input by pressing Tab or Return]. When this anonymous function runs, the value in the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
3 is stored in the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
5 constant. Next, we do a conditional test. If the entered value is not a number, an error message is printed to the paragraph. The test looks at whether the expression
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
6 returns
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
7. The
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
8 function tests whether the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
5 value is not a number — if so, it returns
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
7, and if not, it returns
ctx.arc[random[WIDTH], random[HEIGHT], random[50], 0, 2 * Math.PI];
1. If the test returns
ctx.arc[random[WIDTH], random[HEIGHT], random[50], 0, 2 * Math.PI];
1, the
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
5 value is a number. Therefore, a sentence is printed out inside the paragraph element that states the square, cube, and factorial values of the number. The sentence calls the
function random[number] {
  return Math.floor[Math.random[] * number];
}
8,
function random[number] {
  return Math.floor[Math.random[] * number];
}
9, and
function random[number] {
  const result = Math.floor[Math.random[] * number];
  return result;
}
0 functions to calculate the required values.
  • Save your code, load it in a browser, and try it out.
  • Note: If you have trouble getting the example to work, feel free to check your code against the finished version on GitHub [see it running live also], or ask us for help.

    At this point, we'd like you to have a go at writing out a couple of functions of your own and adding them to the library. How about the square or cube root of the number? Or the circumference of a circle with a given radius?

    Some extra function-related tips:

    • Look at another example of writing error handling into functions. It is generally a good idea to check that any necessary parameters are validated, and that any optional parameters have some kind of default value provided. This way, your program will be less likely to throw errors.
    • Think about the idea of creating a function library. As you go further into your programming career, you'll start doing the same kinds of things over and over again. It is a good idea to create your own library of utility functions to do these sorts of things. You can copy them over to new code, or even just apply them to HTML pages wherever you need them.

    You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: Functions.

    So there we have it — functions are fun, very useful, and although there's a lot to talk about in regards to their syntax and functionality, they are fairly understandable.

    If there is anything you didn't understand, feel free to read through the article again, or to ask for help.

    What will be the default return value if a function does not return anything?

    A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple.

    What function does not return a value?

    Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

    When a function does not return any value what is the by default return value for that function in Python?

    So, if you don't explicitly use a return value in a return statement, or if you totally omit the return statement, then Python will implicitly return a default value for you. That default return value will always be None .

    What happens if a function does not return anything?

    Not using return statement in void return type function: When a function does not return anything, the void return type is used. So if there is a void return type in the function definition, then there will be no return statement inside that function [generally].

    Chủ Đề