Home > Back-end >  I need a clarification for custom functions in C
I need a clarification for custom functions in C

Time:08-25

I'm taking the CS50 class week 1 and I made the code work but I don't understand the custom functions part

void cat (int n)

I know that cat is your desired name for your function. In my own understanding the code inside the parenthesis is the input that your custom function is taking (please correct me if my understanding is wrong) but what I do not understand totally is the first word of the custom functions code like why is it void not int.

I would love it it you explained it in a much simpler English since English or programming vocabulary is pretty small atm.

CodePudding user response:

In your example, "void" is the return value of the function. In this case, the function is not defined to return a value and would therefor not have a "return" statement.

CodePudding user response:

void cat (int n); // note the semicolon

This is a forward declaration of a function named cat that takes one int named n as an input parameter. It's declared void which means that the function does not return anything.

The definition of cat could look like this:

#include <stdio.h> // needed for printf etc.

void cat (int n) {
    printf("the cat is %d years old\n", n);   // note: using `n` here
    // return; // not needed since the function is `void`
}

Forward declaration are made if you need to resolve circular dependencies:

void foo(int x) { if (x==1) bar(x 1); } // calls `bar`
void bar(int x) { if (x==1) foo(x 1); } // calls `foo`

This should not compile because when the compiler processes foo(), bar() is unknown. A forward declaration of bar() before foo() solves it:

void bar(int x); // forward declaration saves the situation
void foo(int x) { if (x==1) bar(x 1); }
void bar(int x) { if (x==1) foo(x 1); }

Forward declarations can also be used to declare all functions defined in the file at the top of the file and implement (define) them later. The list of functions then works as a sort of index of all the functions in the file. It's a matter of coding style. Example:

#include <stdio.h>

// forward declarations of all functions (except main):
void cat (int n);

// program entry point:
int main (void) {
    cat(12);       // calling cat with the argument 12 (an `int`)
    return 0;      // not strictly required when returning from main
}

// all the definitions follows:
void cat (int n) {
    printf("the cat is %d years old\n", n); // prints: the cat is 12 years old
}

CodePudding user response:

Function format:

xxxxx yyyyy(zzzzz)
{
    /* code */
    return something;
}

xxxxx is the function return type. void means that function does not return anything to the caller.

yyyyy is the function name.

zzzzz are the function parameters. void means that function does not take any parameters. Parameters are separated by a comma. Parameter definition consists of type and parameter name.

examples:

int add(int x, int y)
{
     return x   y;
}

the function add taking two integers as parameters and returning int

I do not understand totally is the first word of the custom functions code like why is it void not int.

Programmer decided that function cat will not return anything

  • Related