Uploaded by Clanguage 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:
Tags:
License:
Standard YouTube License
47:11Functions in C Programmingby ignousocis17,137 views
9:28C Programming Tutorial # 23 - Functions - Part ...by Learnorama8,271 views
10:47C Programming Beginners Tamil Lesson 4.1 [For V...by tamilkrishnan1,553 views
9:24Learning C Programming Lesson 2: Downloading C ...by umicom21,071 views
9:48Learning C Programming Lesson 5: Scanf functionby umicom10,178 views
6:30Programing in C (Functions)by kille65251,232 views
9:23C++ Dersleri - 15 - 1.Kısımby DrTaKacan3,707 views
8:58Introduction to programming with C++: part 1by PetarMarendic105,001 views
5:46Programming in C : Lesson 1.1by hhemanth54,484 views
9:20C Programming Tutorial 3- Strings and Char Dat...by LearnToProgramDotTV57,631 views
3:56Programming in C : Lesson 1by hhemanth30,412 views
0:23programming C basic 1 aby pasca1,887 views
8:47Learning C Programming Lesson 20: Function Argu...by umicom6,132 views
0:06C Programming #2 - Sample Programsby Clanguage29,710 views
8:07C Programming Tutorial - 11 - Into to Functionsby thenewboston75,155 views
8:04C Programming Tutorial - 2 - Intro to Variablesby thenewboston158,970 views
17:26Lecture 1 | Programming Paradigms (Stanford)by StanfordUniversity284,283 views
5:56C Programming Tutorial - 8 - Switch Statementby thenewboston63,576 views
5:55C++ Tutorial - 6 - Beginning Functionsby thenewboston110,640 views
- Loading more suggestions...
All Comments