Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

C Programming #3 - Functions

Loading...

Sign in or sign up now!
16,474
Loading...
Alert icon
Sign in or sign up now!
Alert icon
Ratings have been disabled for this video.

Uploaded by on Jan 1, 2007

A basic discussion of functions.

A function is the name given to a specific group of computer code. You might say that the C language is based on functions.

In the last video, you have actually encountered
two functions. The main function, or main(), and the printf() meaning "the printf function".

The main()is the default and required first function that is ran by the computer. It usually comes first in the computer code/source code that you type in, but it could actually come anywhere in the source code.

An entire computer program can be contained in the main(). A return statement signals that the function or program (such as from within the main function) is to be terminated.

Functions are either premade code you can insert into your program and/or code that you want to reuse over and over.

The other function that was encountered in vid #2 was the printf function or printf().

To use a function, you must "call it" with a function call. The basic format/syntax for a function call is:

function_name(arguments);




arguments is arbitrary data that can be "passed" to the function and be used in that function. Multiple arguments are seperated using a comma. If there are no arguments, the (declaration) format is:

format_name(void);

Functions can also return data. For example, here is the declaration of a function that does not have/receive any arguments, but returns an integer:

int fun_identifier(void);

If the function does not return any data, the void keyword is used, as was seen in vid #2 with the main().

With some functions, the returned data or return value is a possible error or success code. You can set a variable equal to the return value with the assignment operator (=).

number1=myfunction(4,1);

The C source code, your computer program, is followed in a "top to down" manner, where after processing one statement, the next statement will be executed/ran. When a function is called, program control or exection will go to that function and the corresponding statements within it will be executed. Once all the statements are executed, or the function perhaps terminated early, control will go right back to the next statement after the call to that function. In a sence, the program will jump to a function and then jump back to where it was called from.
-------------------------------------------------

Here is a basic example of using a function. This function will do two things. First it will print some text on the screen, and then it will make a beep.

/* function1.c
This is a program to test my text_and_beep().
The functions displays some text and makes
a beep. And to show that a function is reusable code, it will be called twice.
Jan 2, 2007
*/

#include "stdio.h"

/* Its good to first declare the function before using it. This declaration will show the (data) type (ie. type of computer data, and this essentially resolves to how much computer space-memory to use for the data) of the return value, and the function's parameters and their (data) types. (parameters are essentially a formal declaration or identifier for the actuall arguments used during a function call) */

void text_and_beep(void);




void main(void)
{

printf("\nWelcome");

text_and_beep(); /* lets do it again: */
text_and_beep();

return;
}

/*---------------------------*/

void text_and_beep(void)
{

printf("\nYou are now in the other function.");
printf("\a");

return;
}
/*-----------------------------*/

Notice that a call to a "void function", or one that does not take any arguments, that the syntax (programming grammer) does not contain the void keyword:

function_name();

keywords are predefined identifiers by the compiler, and in general, should not be used as (data) identifiers that you would create. For example, you would not create a function called printf() because the system already has it, and you would confuse the system as to what printf() is being called. An error is usually displayed during compilation.

Before the C language, to call/reuse code over and over or to "jump" to some code, the keyword "goto" was utilized, especially for the BASIC programming language. Here, program execution would continue at a certain line number (ie - actually an identifier which essentially resolves to a memory location of the next machine instruction to execute). For machine/assembly language, the keword is jmp or jump. The goto keyword is actually included in the C syntax. It can only be used within a function. The format of the goto keyword is:

goto identifier;

For example:
.
.

goto display_the_result;
.
.
display_the_result:
printf(7);
.
.

However, due to availability to call functions with C, the goto keyword and concept are frowned upon, but there might be an instance where it is necessary or somewhat good to use.

Category:

Howto & Style

Tags:

License:

Standard YouTube License

All Comments

Adding comments has been disabled for this video.

0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more