R
R
Rufix2018-12-16 01:03:11
C++ / C#
Rufix, 2018-12-16 01:03:11

What is return for?

I may be an incredibly stupid person (although I don’t consider myself one), but for the life of me I can’t understand what the record “return 0”, “return *some variable*”, etc. does.
I watch a video tutorial and there a person wrote this code

#include <stdio.h>

void print (float number){
    printf ("Your number is %.2f\n", number);
}

float del (float a, float b){
    float res;
    if (b != 0)
        res = a / b;
    else
        res = 0;

        return res;
}

int main (){
    int num1, num2;
    scanf ("%d", &num1);
    scanf ("%d", &num2);
    float result = del (num1, num2);
    print (result);

    return 0;
}

I've read other people's questions and still can't figure it out. As I understand it, one of the return writing functions is to stop this function (this is if you write 0).
But what does "return res" do in the second piece of code? That is, the division of the number A and B is written to the res variable. And then it returns somewhere there .. (I'm still a teapot, so if you can explain, then as clearly as possible). Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Shvedov, 2018-12-16
@Rufix

but all you need to know is English level 5 class, return means to return, it’s easy to guess that this construction indicates what exactly the function will return as a result of its work, and * res * there or 0 does not matter: in the case of C + + the main thing is that the compiler approves this value for compliance with the specified return type

J
jcmvbkbc, 2018-12-16
@jcmvbkbc

float del (float a, float b)
{
    float res;
    ...
    res = ...;
    ....
    return res;
}
....
float result = del (num1, num2);

This is the value that will be returned from the function to the caller. In the example above, it will be assigned to the result variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question