Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

5.3 Ξεχωριστή δήλωση και ορισμός συνάρτησης στην C

Μόλις μάθατε από τα προηγούμενα κεφάλαια ότι μπορείτε να δημιουργήσετε και να καλέσετε μια συνάρτηση με τον εξής τρόπο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
void myFunction() {
printf("Hello World!\n");
}
int main() {
myFunction();
return 0;
}
#include <stdio.h> void myFunction() { printf("Hello World!\n"); } int main() { myFunction(); return 0; }
#include <stdio.h>

void myFunction() {
    printf("Hello World!\n");
}

int main() {
    myFunction();
    return 0;
}

Αλλά μπορείτε επίσης να δηλώσετε τη συνάρτηση χωριστά από την κύρια συνάρτηση, στην αρχή του προγράμματος.

Η δήλωση της συνάρτησης πρέπει να γίνει πριν από τη χρήση της.

Ορίζοντας τη συνάρτηση myFunction() στην αρχή του προγράμματος, μπορείτε να την καλέσετε στη συνάρτηση main(), χωρίς να χρειάζεται να προηγηθεί κάποια δήλωση.

Η δήλωση της συνάρτησης περιλαμβάνει τον τύπο της επιστροφής, το όνομα της συνάρτησης και τον τύπο και τα ονόματα των παραμέτρων (εάν υπάρχουν).

Η οριστική υλοποίηση της συνάρτησης παραμένει η ίδια, δηλαδή η συνάρτηση ορίζεται στην ίδια μορφή όπως πριν:

Syntax

Function declaration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);

Function definition:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) {
// function body
return return_value;
}
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) { // function body return return_value; }
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) {
    // function body
    return return_value;
}

Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function declaration
void myFunction();
int main() {
// Function call
myFunction();
return 0;
}
// Function definition
void myFunction() {
printf("Hello World!\n");
}
#include <stdio.h> // Function declaration void myFunction(); int main() { // Function call myFunction(); return 0; } // Function definition void myFunction() { printf("Hello World!\n"); }
#include <stdio.h>

// Function declaration
void myFunction();

int main() {
    // Function call
    myFunction();
    return 0;
}

// Function definition
void myFunction() {
    printf("Hello World!\n");
}

Μπορείτε επίσης να δηλώσετε τη συνάρτηση και να την ορίσετε χρησιμοποιώντας παραμέτρους, στην οριστική υλοποίηση της συνάρτησης:

[adinserter block=”2″]

Syntax

Function declaration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...);

Function definition:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) {
// function body
return return_value;
}
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) { // function body return return_value; }
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) {
    // function body
    return return_value;
}

Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function declaration
void myFunction(int number);
int main() {
// Function call
myFunction(42);
return 0;
}
// Function definition
void myFunction(int number) {
printf("The number is %d\n", number);
}
#include <stdio.h> // Function declaration void myFunction(int number); int main() { // Function call myFunction(42); return 0; } // Function definition void myFunction(int number) { printf("The number is %d\n", number); }
#include <stdio.h>

// Function declaration
void myFunction(int number);

int main() {
    // Function call
    myFunction(42);
    return 0;
}

// Function definition
void myFunction(int number) {
    printf("The number is %d\n", number);
}

Στο παραπάνω παράδειγμα, η συνάρτηση myFunction() δηλώνεται με έναν ακέραιο αριθμό ως παράμετρο. Στην οριστική υλοποίηση της συνάρτησης, η παράμετρος αυτή ορίζεται ως int number. Στο κύριο πρόγραμμα, η συνάρτηση καλείται με τον ακέραιο αριθμό 42 ως παράμετρο. Η συνάρτηση τυπώνει τον ακέραιο αριθμό στην οθόνη.

Μπορείτε να δηλώσετε και να ορίσετε συναρτήσεις που επιστρέφουν τιμές, στην ίδια μορφή όπως παραπάνω, αλλά με μια επιπλέον δήλωση του τύπου επιστροφής.

Συγκεκριμένα, αντί να χρησιμοποιήσετε το void για να δηλώσετε μια συνάρτηση που δεν επιστρέφει τίποτα, μπορείτε να δηλώσετε τον τύπο της τιμής που η συνάρτηση θα επιστρέψει. Παρακάτω έχουμε ένα παράδειγμα που δηλώνει και ορίζει μια συνάρτηση που επιστρέφει έναν ακέραιο αριθμό:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function declaration
int addNumbers(int x, int y);
int main() {
int result = addNumbers(10, 5);
printf("The result is %d\n", result);
return 0;
}
// Function definition
int addNumbers(int x, int y) {
int sum = x + y;
return sum;
}
#include <stdio.h> // Function declaration int addNumbers(int x, int y); int main() { int result = addNumbers(10, 5); printf("The result is %d\n", result); return 0; } // Function definition int addNumbers(int x, int y) { int sum = x + y; return sum; }
#include <stdio.h>

// Function declaration
int addNumbers(int x, int y);

int main() {
    int result = addNumbers(10, 5);
    printf("The result is %d\n", result);
    return 0;
}

// Function definition
int addNumbers(int x, int y) {
    int sum = x + y;
    return sum;
}

Στο παραπάνω παράδειγμα, η συνάρτηση addNumbers() δηλώνεται ως int ως τύπος επιστροφής. Η συνάρτηση δέχεται δύο ακέραιες τιμές ως παραμέτρους, και επιστρέφει το άθροισμα των δύο τιμών.

Στο κύριο πρόγραμμα, η συνάρτηση καλείται με τις τιμές 10 και 5 ως παραμέτρους. Η συνάρτηση επιστρέφει το άθροισμα των δύο τιμών, το οποίο αποθηκεύεται στη μεταβλητή result. Στη συνέχεια, η συνάρτηση printf() χρησιμοποιείται για να τυπώσει το αποτέλεσμα στην οθόνη.

Σημειώστε ότι η δήλωση της συνάρτησης πρέπει να προηγείται της κλήσης της συνάρτησης στο κύριο πρόγραμμα. Αυτό είναι απαραίτητο για να ενημερώσετε το μεταγλωττιστή για την ύπαρξη της συνάρτησης.

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων