Methods without parameters and with parameters

Asked

Viewed 2,276 times

5

Declaration methods without parameters:

void exemploDeMetodo(){
    int i;
}

Calling methods without parameters:

exemploDeMetodo();

If I want to make a method with parameters it’s like?

That’s the way it is?

void exemplo(int i){
    faz algo com o i
}

Calling methods with parameters:

int inteiro = 1;
exemplo(inteiro);

If I want to return an integer, I have to put a parameter in the method?

  • 2

    This is Java or C?

  • 1

    Exactly, it would be good to specify which of the two languages is using, because there are some differences that should be considered.

  • 1

    example is in C.

  • 2

    @Josemaximilian always try to use the right tags that help define what your problem is about to make it easier for people to help you. See the edits made to understand that the questions should be as clean as possible, with nothing that does not matter to the question, so it can help other people in the future.

  • Sorry, I’ll do it next time. I’m Noob around here.

  • 1

    @Josemaximilian is normal, the staff takes a little getting used to a site of Questions and Answers. When you get the hang of it, you’ll see it’s for everyone’s benefit.

Show 1 more comment

2 answers

7


You are correct in all your initial assumptions.

To return an integer you do not need parameters. The return works separately. You can have parameter but it has no direct relation to the return. The return is a result you pass back to the caller.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int exemplo1() { return 1; }

int exemplo2(int i) { return i * 2; }

int exemplo3() {
    srand(time(0));
    return rand();
}

int exemplo4(int i) { return i > 0 ? 1 : 0; }

int main(void) {
    printf("%i\n", exemplo1());
    printf("%i\n", exemplo2(2));
    printf("%i\n", exemplo3());
    printf("%i\n", exemplo4(2));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that in C you should always declare the data type for everything: variables, vector elements, structure fields, parameters and of course, you can declare the type of functions, which are confused with the function return type. void means to return nothing. And of course, int means it will return an entire.

In C there is even how to return values through parameters, but this is advanced, don’t worry about it until you learn other things before. Still no use to you.

4

You hit almost everything, just missed the part of returning a value.

A function has the following format:

tipoDeRetorno nomeDoMetodo(tipoDoParametro valorDoParametro) {
    //se o tipoDeRetorno for void não precisa retornar nada
    //caso contrário deve-se usar a seguinte notação:
    return variavelCompativelComOTipoDeRetorno;
}

To make a function that returns an integer type value you must change the void that you put in your example, so:

int exemplo(int i) {
    int j = i*2;
    return j;
}

Notice that you don’t necessarily need to return i, you can return another variable, the result of an expression or a value, as long as it matches with the type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.